python bindings: core key import
authorBen McGinnes <ben@adversary.org>
Sat, 16 Jun 2018 23:28:30 +0000 (09:28 +1000)
committerBen McGinnes <ben@adversary.org>
Sat, 16 Jun 2018 23:28:30 +0000 (09:28 +1000)
* The foundation of a pythonic key import function authored by Jacob
  Adams.
* A unit testing script for the same function originally authored by
  Tobias Mueller
* Added DCO reference for Jacob Adams to the GPGME AUTHORS file.
* Additional details regarding this patch are available here:
  https://dev.gnupg.org/T4001

Signed-off-by: Ben McGinnes <ben@adversary.org>
AUTHORS
lang/python/src/core.py
lang/python/tests/t-import.py

diff --git a/AUTHORS b/AUTHORS
index 1bd3209..e0136ff 100644 (file)
--- a/AUTHORS
+++ b/AUTHORS
@@ -62,6 +62,9 @@ Tobias Mueller <muelli@cryptobitch.de>
 Ben McGinnes <ben@adversary.org>
 2017-12-16:20171216002102.l6aejk5xdp6xhtfi@adversary.org:
 
+Jacob Adams <tookmund@gmail.com>
+2018-06-03:ad5141df-b6cc-6c2a-59df-b2f18f7160fd@gmail.com:
+
 
  Copyright 2001, 2002, 2012, 2013 g10 Code GmbH
 
index bd95d23..1b83a5d 100644 (file)
@@ -509,6 +509,25 @@ class Context(GpgmeWrapper):
 
         return results
 
+    def key_import(self, data):
+        """Import data
+
+        Imports the given data into the Context.
+
+        Returns:
+        result -- information about the imported data
+
+        Raises:
+        GPGMEError      -- as signaled by the underlying library
+        ValueError      -- Raised if no keys are present in the data
+
+        """
+        self.op_import(data)
+        result = self.op_import_result()
+        if result.considered == 0:
+            raise ValueError
+        return result
+
     def keylist(self, pattern=None, secret=False,
                 mode=constants.keylist.mode.LOCAL,
                 source=None):
index e2edf5a..44dc360 100755 (executable)
@@ -1,6 +1,6 @@
 #!/usr/bin/env python
 
-# Copyright (C) 2016 g10 Code GmbH
+# Copyright (C) 2016 Tobias Mueller <muelli at cryptobitch.de>
 #
 # This file is part of GPGME.
 #
@@ -69,10 +69,14 @@ def check_result(result, fpr, secret):
 
 c = gpg.Context()
 
-c.op_import(gpg.Data(file=support.make_filename("pubkey-1.asc")))
-result = c.op_import_result()
+result = c.key_import(open(support.make_filename("pubkey-1.asc"), 'rb').read())
 check_result(result, "ADAB7FCC1F4DE2616ECFA402AF82244F9CD9FD55", False)
 
-c.op_import(gpg.Data(file=support.make_filename("seckey-1.asc")))
-result = c.op_import_result()
+result = c.key_import(open(support.make_filename("seckey-1.asc"), 'rb').read())
 check_result(result, "ADAB7FCC1F4DE2616ECFA402AF82244F9CD9FD55", True)
+
+try:
+    result = c.key_import(b"thisisnotakey")
+except ValueError:
+    pass
+assert result.considered == 0