Make packets_from_bytes a PGPKey method
authorW. Trevor King <wking@tremily.us>
Sun, 22 Dec 2013 22:00:47 +0000 (14:00 -0800)
committerW. Trevor King <wking@tremily.us>
Mon, 23 Dec 2013 21:41:06 +0000 (13:41 -0800)
There's no need to leave this a stand-alone function if it's only used
from PGPKey.

gpg-migrate.py

index 04af59759230a2fad3253080b615db48a621f2d1..aae79c78f30b72a6da14532e011684390411feed 100755 (executable)
@@ -938,14 +938,6 @@ class PGPPacket (dict):
         return plaintext
 
 
-def packets_from_bytes(data):
-    offset = 0
-    while offset < len(data):
-        packet = PGPPacket()
-        offset += packet.from_bytes(data=data[offset:])
-        yield packet
-
-
 class PGPKey (object):
     """An OpenPGP key with public and private parts.
 
@@ -1007,7 +999,7 @@ class PGPKey (object):
         key_export = _get_stdout(
             ['gpg', '--export', self.fingerprint])
         self.public_packets = list(
-            packets_from_bytes(data=key_export))
+            self._packets_from_bytes(data=key_export))
         if self.public_packets[0]['type'] != 'public-key packet':
             raise ValueError(
                 '{} does not start with a public-key packet'.format(
@@ -1015,12 +1007,19 @@ class PGPKey (object):
         key_secret_export = _get_stdout(
             ['gpg', '--export-secret-keys', self.fingerprint])
         self.secret_packets = list(
-            packets_from_bytes(data=key_secret_export))
+            self._packets_from_bytes(data=key_secret_export))
         if self.secret_packets[0]['type'] != 'secret-key packet':
             raise ValueError(
                 '{} does not start with a secret-key packet'.format(
                     self.fingerprint))
 
+    def _packets_from_bytes(self, data):
+        offset = 0
+        while offset < len(data):
+            packet = PGPPacket()
+            offset += packet.from_bytes(data=data[offset:])
+            yield packet
+
     def export_to_gpg(self):
         raise NotImplemetedError('export to gpg')