python: hkp key importer
authorBen McGinnes <ben@adversary.org>
Mon, 10 Dec 2018 22:55:15 +0000 (09:55 +1100)
committerBen McGinnes <ben@adversary.org>
Mon, 10 Dec 2018 22:55:15 +0000 (09:55 +1100)
* Tweaked it slightly to avoid repetition of key searches when there
  is only one search pattern to check (i.e. usually a single key ID or
  fingerprint).

lang/python/examples/howto/import-keys-hkp.py

index 1abd0fd..f5d9787 100755 (executable)
@@ -58,32 +58,37 @@ if pattern is not None:
         if keyed is True:
             try:
                 fob = server.search(pattern)
-            except:
+            except Exception as e:
                 fob = None
             if fob is not None:
                 keys.append(fob[0])
+            else:
+                pass
         else:
             pass
-    else:
-        pass
 
     for logrus in pattern.split():
-        try:
-            key = server.search(hex(int(logrus, 16)))
-            hexed = True
-        except ValueError as ve:
-            key = server.search(logrus)
-            hexed = False
-
-        if key is not None:
-            keys.append(key[0])
-            if hexed is True:
-                try:
-                    fob = server.search(logrus)
-                except:
-                    fob = None
-                if fob is not None:
-                    keys.append(fob[0])
+        if logrus != pattern:
+            try:
+                key = server.search(hex(int(logrus, 16)))
+                hexed = True
+            except ValueError as ve:
+                key = server.search(logrus)
+                hexed = False
+
+            if key is not None:
+                keys.append(key[0])
+                if hexed is True:
+                    try:
+                        fob = server.search(logrus)
+                    except Exception as e:
+                        fob = None
+                    if fob is not None:
+                        keys.append(fob[0])
+                    else:
+                        pass
+                else:
+                    pass
             else:
                 pass
         else: