[PATCH] copyright: add all_paragraphs iterator

Stefano Zacchiroli zack at upsilon.cc
Thu Sep 11 12:21:34 UTC 2014


---
 lib/debian/copyright.py | 10 ++++++++++
 tests/test_copyright.py | 12 +++++++++---
 2 files changed, 19 insertions(+), 3 deletions(-)

diff --git a/lib/debian/copyright.py b/lib/debian/copyright.py
index 04a6ad4..708569f 100644
--- a/lib/debian/copyright.py
+++ b/lib/debian/copyright.py
@@ -138,6 +138,16 @@ class Copyright(object):
             raise TypeError('value must be a Header object')
         self.__header = hdr
 
+    def all_paragraphs(self):
+        """Returns an iterator over all paragraphs (header, Files, License).
+
+        The header (returned first) will be returned as a Header object; file
+        paragraphs as FilesParagraph objects; license paragraphs as
+        LicenseParagraph objects.
+
+        """
+        return itertools.chain([self.header], (p for p in self.__paragraphs))
+
     def all_files_paragraphs(self):
         """Returns an iterator over the contained FilesParagraph objects."""
         return (p for p in self.__paragraphs if isinstance(p, FilesParagraph))
diff --git a/tests/test_copyright.py b/tests/test_copyright.py
index ed9cdda..1174db5 100755
--- a/tests/test_copyright.py
+++ b/tests/test_copyright.py
@@ -99,9 +99,6 @@ Copyright: Copyright 2000 Company A
            Copyright 2001 Company B
 License: ABC
 
-License: ABC
- [ABC TEXT]
-
 Files: debian/*
 Copyright: Copyright 2003 Debian Developer <someone at debian.org>
 License: 123
@@ -111,6 +108,9 @@ Copyright: Copyright 2003 Debian Developer <someone at debian.org>
            Copyright 2004 Someone Else <foo at bar.com>
 License: 123
 
+License: ABC
+ [ABC TEXT]
+
 License: 123
  [123 TEXT]
 """
@@ -265,6 +265,12 @@ class CopyrightTest(unittest.TestCase):
         dumped = c.dump()
         self.assertEqual(SIMPLE, dumped)
 
+    def test_all_paragraphs(self):
+        c = copyright.Copyright(MULTI_LICENSE.splitlines())
+        expected = [c.header] + list(c.all_files_paragraphs()) + \
+            list(c.all_license_paragraphs())
+        self.assertEqual(expected, list(c.all_paragraphs()))
+
     def test_all_files_paragraphs(self):
         c = copyright.Copyright(sequence=SIMPLE.splitlines())
         self.assertEqual(
-- 
2.1.0




More information about the pkg-python-debian-maint mailing list