diff options
author | Ben McGinnes <[email protected]> | 2018-06-17 04:35:20 +0000 |
---|---|---|
committer | Ben McGinnes <[email protected]> | 2018-06-17 04:35:20 +0000 |
commit | 5a80e755008bbb3f4c7f91ffccd38f26cd8b3960 (patch) | |
tree | 072d01e49f2038e064259fe176b5e6abf37f5376 | |
parent | python bindings: core key import (diff) | |
download | gpgme-gsoc/jacob-key-import.tar.gz gpgme-gsoc/jacob-key-import.zip |
python bindings: core - key importgsoc/jacob-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]>
-rw-r--r-- | lang/python/src/core.py | 46 |
1 files changed, 37 insertions, 9 deletions
diff --git a/lang/python/src/core.py b/lang/python/src/core.py index 1b83a5d4..06be7f79 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, |