Adjust PGPKey._packets_from_bytes to append on the fly
authorW. Trevor King <wking@tremily.us>
Mon, 23 Dec 2013 16:58:21 +0000 (08:58 -0800)
committerW. Trevor King <wking@tremily.us>
Mon, 23 Dec 2013 21:41:06 +0000 (13:41 -0800)
With the list(iterator) approach, signature packets in the stream did
not have access to already-parsed packets for target extraction.  Now
they do.

gpg-migrate.py

index f5a757c2aa84eb4432ce9ac1233c4d0ba0cbe0de..0a7d116b10278681af92cd9a4950fd8f38c230ab 100755 (executable)
@@ -1095,16 +1095,16 @@ class PGPKey (object):
     def import_from_gpg(self):
         key_export = _get_stdout(
             ['gpg', '--export', self.fingerprint])
-        self.public_packets = list(
-            self._packets_from_bytes(data=key_export))
+        self.public_packets = []
+        self._packets_from_bytes(list=self.public_packets, data=key_export)
         if self.public_packets[0]['type'] != 'public-key packet':
             raise ValueError(
                 '{} does not start with a public-key packet'.format(
                     self.fingerprint))
         key_secret_export = _get_stdout(
             ['gpg', '--export-secret-keys', self.fingerprint])
-        self.secret_packets = list(
-            self._packets_from_bytes(data=key_secret_export))
+        self.secret_packets = []
+        self._packets_from_bytes(list=self.secret_packets, data=key_secret_export)
         if self.secret_packets[0]['type'] != 'secret-key packet':
             raise ValueError(
                 '{} does not start with a secret-key packet'.format(
@@ -1112,12 +1112,12 @@ class PGPKey (object):
         for packet in self.public_packets + self.secret_packets:
             packet.check_roundtrip()
 
-    def _packets_from_bytes(self, data):
+    def _packets_from_bytes(self, list, data):
         offset = 0
         while offset < len(data):
             packet = PGPPacket(key=self)
             offset += packet.from_bytes(data=data[offset:])
-            yield packet
+            list.append(packet)
 
     def export_to_gpg(self):
         raise NotImplemetedError('export to gpg')