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 <ben@adversary.org>
This commit is contained in:
Ben McGinnes 2018-06-17 14:35:20 +10:00
parent 0e762608ef
commit 5a80e75500

View File

@ -515,18 +515,46 @@ class Context(GpgmeWrapper):
Imports the given data into the Context. Imports the given data into the Context.
Returns: Returns:
result -- information about the imported data -- an object describing the results of imported or updated
keys
Raises: Raises:
GPGMEError -- as signaled by the underlying library TypeError -- Very rarely.
ValueError -- Raised if no keys are present in the data 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) try:
result = self.op_import_result() self.op_import(data)
if result.considered == 0: result = self.op_import_result()
raise ValueError if result.considered == 0:
return result 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, def keylist(self, pattern=None, secret=False,
mode=constants.keylist.mode.LOCAL, mode=constants.keylist.mode.LOCAL,