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:
parent
0e762608ef
commit
5a80e75500
@ -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.
|
||||||
"""
|
"""
|
||||||
|
try:
|
||||||
self.op_import(data)
|
self.op_import(data)
|
||||||
result = self.op_import_result()
|
result = self.op_import_result()
|
||||||
if result.considered == 0:
|
if result.considered == 0:
|
||||||
raise ValueError
|
status = constants.STATUS_IMPORT_PROBLEM
|
||||||
return result
|
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,
|
||||||
|
Loading…
Reference in New Issue
Block a user