[git] GPGME - branch, gsoc/jacob-key-import, created. gpgme-1.11.1-60-g5a80e75

[email protected] (by Ben McGinnes)
Newsgroups gmane.comp.encryption.gpg.cvs
Message-ID <[email protected]>
This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "GnuPG Made Easy".

The branch, gsoc/jacob-key-import has been created
        at  5a80e755008bbb3f4c7f91ffccd38f26cd8b3960 (commit)

- Log -----------------------------------------------------------------
commit 5a80e755008bbb3f4c7f91ffccd38f26cd8b3960
Author: Ben McGinnes <[email protected]>
Date:   Sun Jun 17 14:35:20 2018 +1000

    python bindings: core - key import
    
    * Wrapped the key import function in the try/exception statements
      needed to catch at least the most likely unsuccessful import attempt
      errors.
    * Mostly draws on the file error and no data import statuses for
      errors, with a couple of exceptions.
    
    Signed-off-by: Ben McGinnes <[email protected]>

diff --git a/lang/python/src/core.py b/lang/python/src/core.py
index 1b83a5d..06be7f7 100644
--- a/lang/python/src/core.py
+++ b/lang/python/src/core.py
@@ -515,18 +515,46 @@ class Context(GpgmeWrapper):
         Imports the given data into the Context.
 
         Returns:
-        result -- information about the imported data
+                -- an object describing the results of imported or updated 
+                   keys
 
         Raises:
-        GPGMEError      -- as signaled by the underlying library
-        ValueError      -- Raised if no keys are present in the data
-
+        TypeError      -- Very rarely.
+        GPGMEError     -- as signaled by the underlying library:
+
+                          Import status errors, when they occur, will usually 
+                          be of NODATA.  NO_PUBKEY indicates something
+                          managed to run the function without any
+                          arguments, while an argument of None triggers
+                          the first NODATA of errors.GPGME in the
+                           exception.
         """
-        self.op_import(data)
-        result = self.op_import_result()
-        if result.considered == 0:
-            raise ValueError
-        return result
+        try:
+            self.op_import(data)
+            result = self.op_import_result()
+            if result.considered == 0:
+                status = constants.STATUS_IMPORT_PROBLEM
+            else:
+                status = constants.STATUS_KEY_CONSIDERED
+        except Exception as e:
+            if e == errors.GPGMEError:
+                if e.code_str == "No data":
+                    status = constants.STATUS_NODATA
+                else:
+                    status = constants.STATUS_FILE_ERROR
+            elif e == TypeError and hasattr(data, "decode") is True:
+                status = constants.STATUS_NO_PUBKEY
+            elif e == TypeError and hasattr(data, "encode") is True:
+                status = constants.STATUS_FILE_ERROR
+            else:
+                status = constants.STATUS_ERROR
+
+        if status == constants.STATUS_KEY_CONSIDERED:
+            import_result = result
+        else:
+            import_result = status
+
+        return import_result
 
     def keylist(self, pattern=None, secret=False,
                 mode=constants.keylist.mode.LOCAL,

commit 0e762608ef5a598030b8d0e56261a830e1b7b724
Author: Ben McGinnes <[email protected]>
Date:   Sun Jun 17 09:28:30 2018 +1000

    python bindings: core key import
    
    * 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 <[email protected]>

diff --git a/AUTHORS b/AUTHORS
index 1bd3209..e0136ff 100644
--- a/AUTHORS
+++ b/AUTHORS
@@ -62,6 +62,9 @@ Tobias Mueller <[email protected]>
 Ben McGinnes <[email protected]>
 2017-12-16:[email protected]:
 
+Jacob Adams <[email protected]>
+2018-06-03:[email protected]:
+
 
  Copyright 2001, 2002, 2012, 2013 g10 Code GmbH
 
diff --git a/lang/python/src/core.py b/lang/python/src/core.py
index bd95d23..1b83a5d 100644
--- a/lang/python/src/core.py
+++ b/lang/python/src/core.py
@@ -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):
diff --git a/lang/python/tests/t-import.py b/lang/python/tests/t-import.py
index e2edf5a..44dc360 100755
--- a/lang/python/tests/t-import.py
+++ b/lang/python/tests/t-import.py
@@ -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

-----------------------------------------------------------------------


hooks/post-receive
-- 
GnuPG Made Easy
http://git.gnupg.org
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.