From 870c317120e08d3f606c6c5675d559e1d457290d Mon Sep 17 00:00:00 2001 From: Ben McGinnes Date: Wed, 27 Jun 2018 18:51:09 +1000 Subject: [PATCH] python bindings: export public keys * Added functions for exporting public keys to gpg.core in both complete form and in minimised form. * Rather than letting people need to worry about the export modes we are simply separating the functions as people would be more familiar with from the command line usage anyway. * Functions added for Context are: ctx.key_export_minimal and ctx.key_export as the default or full export. Signed-off-by: Ben McGinnes --- lang/python/src/core.py | 59 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) diff --git a/lang/python/src/core.py b/lang/python/src/core.py index 8f2e9d8d..86a62b51 100644 --- a/lang/python/src/core.py +++ b/lang/python/src/core.py @@ -537,7 +537,7 @@ class Context(GpgmeWrapper): managed to run the function without any arguments, while an argument of None triggers the first NODATA of errors.GPGME in the - exception. + exception. """ try: self.op_import(data) @@ -566,6 +566,63 @@ class Context(GpgmeWrapper): return import_result + def key_export(self, pattern=None): + """Export keys. + + Exports public keys matching the pattern specified. If no + pattern is specified then exports all available keys. + + Keyword arguments: + pattern -- return keys matching pattern (default: all keys) + + Returns: + -- A key block containing one or more OpenPGP keys in + either ASCII armoured or binary format as determined + by the Context(). + + Raises: + GPGMEError -- as signaled by the underlying library. + """ + data = Data() + mode = 0 + try: + self.op_export(pattern, mode, data) + data.seek(0, os.SEEK_SET) + result = data.read() + except GPGMEError as e: + result = e + + return result + + def key_export_minimal(self, pattern=None): + """Export keys. + + Exports public keys matching the pattern specified in a + minimised format. If no pattern is specified then exports all + available keys. + + Keyword arguments: + pattern -- return keys matching pattern (default: all keys) + + Returns: + -- A key block containing one or more minimised OpenPGP + keys in either ASCII armoured or binary format as + determined by the Context(). + + Raises: + GPGMEError -- as signaled by the underlying library. +""" + data = Data() + mode = gpgme.GPGME_EXPORT_MODE_MINIMAL + try: + self.op_export(pattern, mode, data) + data.seek(0, os.SEEK_SET) + result = data.read() + except GPGMEError as e: + result = e + + return result + def keylist(self, pattern=None, secret=False, mode=constants.keylist.mode.LOCAL, source=None):