[PATCH 2/6] Clean up the GpgInfo class

John Wright jsw at debian.org
Wed Jul 29 13:35:28 UTC 2009


From: John Wright <john at johnwright.org>

This patch turns GpgInfo's from_output method (which was implemented,
strangely, as a static method) into its __init__ method, and
re-implements from_output as a class method that's just a wrapper around
the initializer.

It also redefines from_sequence as a class method, and removes the
from_file method, which not only never worked, but whose purpose is
trivially implemented with the from_sequence method.
---
 debian_bundle/deb822.py |   59 ++++++++++++++++++++++------------------------
 1 files changed, 28 insertions(+), 31 deletions(-)

diff --git a/debian_bundle/deb822.py b/debian_bundle/deb822.py
index 485a948..2738b64 100644
--- a/debian_bundle/deb822.py
+++ b/debian_bundle/deb822.py
@@ -530,34 +530,22 @@ class GpgInfo(dict):
     # keys with format "key keyid uid"
     uidkeys = ('GOODSIG', 'EXPSIG', 'EXPKEYSIG', 'REVKEYSIG', 'BADSIG')
 
-    def valid(self):
-        """Is the signature valid?"""
-        return self.has_key('GOODSIG') or self.has_key('VALIDSIG')
-    
-# XXX implement as a property?
-# XXX handle utf-8 %-encoding
-    def uid(self):
-        """Return the primary ID of the signee key, None is not available"""
-        pass
+    def __init__(self, out, err=None):
+        """Create a new GpgInfo object from gpg(v) --status-fd output (out)
 
-    @staticmethod
-    def from_output(out, err=None):
-        """Create a new GpgInfo object from gpg(v) --status-fd output (out) and
-        optionally collect stderr as well (err).
+        Optionally collect stderr as well (err).
         
-        Both out and err can be lines in newline-terminated sequence or regular
-        strings.
+        Both out and err can be sequences of lines (including file objects) or
+        regular strings.
         """
 
-        n = GpgInfo()
-
         if isinstance(out, basestring):
             out = out.split('\n')
         if isinstance(err, basestring):
             err = err.split('\n')
 
-        n.out = out
-        n.err = err
+        self.out = out
+        self.err = err
         
         header = '[GNUPG:] '
         for l in out:
@@ -570,18 +558,21 @@ class GpgInfo(dict):
             # str.partition() would be better, 2.5 only though
             s = l.find(' ')
             key = l[:s]
-            if key in GpgInfo.uidkeys:
+            if key in self.uidkeys:
                 # value is "keyid UID", don't split UID
                 value = l[s+1:].split(' ', 1)
             else:
                 value = l[s+1:].split(' ')
 
-            n[key] = value
-        return n 
+            self[key] = value
 
-# XXX how to handle sequences of lines? file() returns \n-terminated
-    @staticmethod
-    def from_sequence(sequence,
+    @classmethod
+    def from_output(cls, out, err=None):
+        # For backwards compatability
+        return cls(out, err=err)
+
+    @classmethod
+    def from_sequence(cls, sequence,
                       keyrings=['/usr/share/keyrings/debian-keyring.gpg'],
                       executable=["/usr/bin/gpgv"]):
         """Create a new GpgInfo object from the given sequence.
@@ -610,15 +601,21 @@ class GpgInfo(dict):
         if isinstance(sequence, basestring):
             (out, err) = p.communicate(sequence)
         else:
-            (out, err) = p.communicate("\n".join(sequence))
+            # Make sure we give gpg single-newline-terminated-lines
+            lines = [line.rstrip('\n') for line in sequence]
+            (out, err) = p.communicate("\n".join(lines))
 
-        return GpgInfo.from_output(out, err)
+        return cls(out, err=err)
 
-    @staticmethod
-    def from_file(target, *args):
-        """Create a new GpgInfo object from the given file"""
-        return from_sequence(file(target), *args)
+    def valid(self):
+        """Is the signature valid?"""
+        return self.has_key('GOODSIG') or self.has_key('VALIDSIG')
 
+# XXX implement as a property?  (Uhh, implement at all?)
+# XXX handle utf-8 %-encoding
+    def uid(self):
+        """Return the primary ID of the signee key, None is not available"""
+        pass
 ###
 
 class PkgRelation(object):
-- 
1.6.3.3




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