On Sun, Jul 26, 2009 at 03:06:58PM +0200, John Wright wrote:
> I really wish it were possible to ensure the apt_pkg parser gave the
> same results as the native parser, though. :(

Looks like we can actually do this, with a small patch to python-apt [1]
and the attached patch.

 [1]: http://bugs.debian.org/538723

-- 
John Wright <j...@debian.org>
commit a26b007a91c9723d33690e7e0b897c70c2006ee5
Author: John Wright <j...@johnwright.org>
Date:   Sun Jul 26 17:20:20 2009 +0200

    Don't ignore leading newlines on field data with apt_pkg
    
    This depends on the patch to python-apt in #538723 being accepted.

diff --git a/debian_bundle/deb822.py b/debian_bundle/deb822.py
index fb1ed7c..a5662af 100644
--- a/debian_bundle/deb822.py
+++ b/debian_bundle/deb822.py
@@ -37,6 +37,31 @@ import sys
 import StringIO
 import UserDict
 
+class TagSectionWrapper(object, UserDict.DictMixin):
+    """Wrap a TagSection object, using the FindRaw method instead of Find
+
+    This allows us to pick which whitespace to strip off the beginning and end
+    of the data, so we don't lose leading newlines.
+    """
+
+    def __init__(self, parser):
+        self.parser = parser
+
+    def keys(self):
+        return self.parser.keys()
+
+    def __getitem__(self, key):
+        s = self.parser.FindRaw(key)
+
+        if s is None:
+            raise KeyError(key)
+
+        data = s.partition(':')[2]
+
+        # Get rid of spaces and tabs after the :, but not newlines, and strip
+        # off any newline at the end of the data.
+        return data.lstrip(' \t').rstrip('\n')
+
 class OrderedSet(object):
     """A set-like object that preserves order when iterating over it
 
@@ -242,16 +267,12 @@ class Deb822(Deb822Dict):
 
         if _have_apt_pkg and use_apt_pkg and isinstance(sequence, file):
             parser = apt_pkg.ParseTagFile(sequence)
+            section = TagSectionWrapper(parser.Section)
             while parser.Step() == 1:
                 if shared_storage:
-                    parsed = parser.Section
+                    parsed = section
                 else:
-                    # Since parser.Section doesn't have an items method, we
-                    # need to imitate that method here and make a Deb822Dict
-                    # from the result in order to preserve order.
-                    items = [(key, parser.Section[key])
-                             for key in parser.Section.keys()]
-                    parsed = Deb822Dict(items)
+                    parsed = Deb822Dict(section)
                 yield cls(fields=fields, _parsed=parsed)
 
         else:

Reply via email to