Add a signature subpacket parsing/display framework to PGPPacket
authorW. Trevor King <wking@tremily.us>
Fri, 20 Dec 2013 21:26:48 +0000 (13:26 -0800)
committerW. Trevor King <wking@tremily.us>
Mon, 23 Dec 2013 02:32:11 +0000 (18:32 -0800)
Along the lines of the existing packet-parsing framework in
PGPPacket.from_bytes.

gpg-migrate.py

index 515760b4a5cd29de2e68059cf95b55cbbb81705e..b5eafaa7737ea7eeb17ca0aec677dac21efb1aee 100755 (executable)
@@ -268,7 +268,15 @@ class PGPPacket (dict):
         if self['unhashed-subpackets']:
             lines.append('  unhashed subpackets:')
             for subpacket in self['unhashed-subpackets']:
-                lines.append('    {}'.format(subpacket['type']))
+                method_name = '_str_{}_signature_subpacket'.format(
+                    self._clean_type(type=subpacket['type']))
+                method = getattr(self, method_name, None)
+                if method:
+                    lines.append('    {}: {}'.format(
+                        subpacket['type'],
+                        method(subpacket=subpacket)))
+                else:
+                    lines.append('    {}'.format(subpacket['type']))
         return '\n'.join(lines)
 
     def _str_user_id_packet(self):
@@ -481,8 +489,16 @@ class PGPPacket (dict):
             offset += 4
         subpacket['type'] = self._signature_subpacket_types[data[offset]]
         offset += 1
-        subpacket['data'] = data[offset: offset + length - 1]
-        offset += len(subpacket['data'])
+        subpacket_data = data[offset: offset + length - 1]
+        offset += len(subpacket_data)
+        method_name = '_parse_{}_signature_subpacket'.format(
+            self._clean_type(type=subpacket['type']))
+        method = getattr(self, method_name, None)
+        if not method:
+            raise NotImplementedError(
+                'cannot parse signature subpacket type {!r}'.format(
+                    subpacket['type']))
+        method(data=subpacket_data, subpacket=subpacket)
         return (offset, subpacket)
 
     def _parse_signature_packet(self, data):