[PATCH] Add an optional encoding arument to Deb822Dict.dump

John Wright john.wright at hp.com
Tue Sep 16 20:13:29 UTC 2008


In order to support dumping Deb822 objects with unicode objects as values=
,
there is now an encoding argument to the dump method.  This way, you can =
do

    d =3D deb822.Deb822()
    d['foo'] =3D u'=FCber'
    f =3D open('/tmp/testdeb822.txt', 'w')
    d.dump(f, encoding=3D'utf-8')

This should work for any encoding, as long as the encoding can represent =
all
the characters in your Deb822 object.
---
 debian_bundle/deb822.py |   13 ++++++++++---
 1 files changed, 10 insertions(+), 3 deletions(-)

diff --git a/debian_bundle/deb822.py b/debian_bundle/deb822.py
index 2e41173..10e37c1 100644
--- a/debian_bundle/deb822.py
+++ b/debian_bundle/deb822.py
@@ -254,10 +254,13 @@ class Deb822(Deb822Dict):
=20
     # __repr__ is handled by Deb822Dict
=20
-    def dump(self, fd=3DNone):
+    def dump(self, fd=3DNone, encoding=3DNone):
         """Dump the the contents in the original format
=20
         If fd is None, return a string.
+
+        If encoding is not None, attempt to encode the output to encodin=
g.
+        This is useful if the object contains values that are unicode ob=
jects.
         """
=20
         if fd is None:
@@ -270,9 +273,13 @@ class Deb822(Deb822Dict):
                 # Avoid trailing whitespace after "Field:" if it's on it=
s own
                 # line or the value is empty
                 # XXX Uh, really print value if value =3D=3D '\n'?
-                fd.write('%s:%s\n' % (key, value))
+                entry =3D '%s:%s\n' % (key, value)
+            else:
+                entry =3D '%s: %s\n' % (key, value)
+            if encoding:
+                fd.write(entry.encode(encoding))
             else:
-                fd.write('%s: %s\n' % (key, value))
+                fd.write(entry)
         if return_string:
             return fd.getvalue()
=20
--=20
1.5.6.5


--J2SCkAp4GZ/dPZZf--





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