Page Menu
Home
GnuPG
Search
Configure Global Search
Log In
Files
F348592
python-importexport.patch
tookmund (Jacob Adams)
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Authored By
tookmund
May 29 2018, 5:07 PM
2018-05-29 17:07:54 (UTC+2)
Size
3 KB
Subscribers
None
python-importexport.patch
View Options
diff --git a/lang/python/src/core.py b/lang/python/src/core.py
index bd95d231..514c3781 100644
--- a/lang/python/src/core.py
+++ b/lang/python/src/core.py
@@ -509,6 +509,45 @@ class Context(GpgmeWrapper):
return results
+ def import_(self, bytes):
+ """Import data
+
+ Imports given bytes into the Context.
+
+ Returns:
+ result -- information about the imported data
+
+ Raises:
+ GPGMEError -- as signaled by the underlying library
+
+ """
+ self.op_import(bytes)
+ import_result = self.op_import_result()
+ return import_result
+
+ # Hrm. A constants.EXPORT_MODE_NORMAL does not seem to exist
+ def export(self, pattern, mode=0, sink=None):
+ """Export data
+
+ Exports key for the given pattern under the given mode.
+
+ Keyword arguments:
+ mode -- export mode. See constants.EXPORT_MODE.
+ (default: 0 which should be "normal")
+ sink -- write result to sink instead of returning it
+
+ Returns:
+ exported_data -- the bytes of your key(s) of your desire
+
+ Raises:
+ GPGMEError -- as signaled by the underlying library
+
+ """
+ data = sink if sink else Data()
+ self.op_export(pattern, mode, data)
+ return self.__read__(sink, data)
+
+
def keylist(self, pattern=None, secret=False,
mode=constants.keylist.mode.LOCAL,
source=None):
diff --git a/lang/python/tests/t-export.py b/lang/python/tests/t-export.py
old mode 100755
new mode 100644
index b9d52048..deb41e17
--- a/lang/python/tests/t-export.py
+++ b/lang/python/tests/t-export.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.
#
@@ -20,20 +20,32 @@
from __future__ import absolute_import, print_function, unicode_literals
del absolute_import, print_function, unicode_literals
+import os
+
import gpg
-import support
c = gpg.Context()
-c.set_armor(True)
+# We just want to get any existing key
+fpr = next(c.keylist()).fpr
-sink = gpg.Data()
-c.op_export_ext(['Alpha', 'Bob'], 0, sink)
-support.print_data(sink)
+# We test the export() function for a pattern
+bytes = c.export(fpr)
+assert bytes
-# Again. Now using a key array.
-keys = []
-keys.append(c.get_key("0x68697734", False)) # Alpha
-keys.append(c.get_key("0xA9E3B0B2", False)) # Bob
+# The export function also takes a mode argument
+minimal = c.export(fpr, mode=gpg.constants.EXPORT_MODE_MINIMAL)
+assert len(minimal) < len(bytes)
+
+# We can also provide a sink of our liking
sink = gpg.Data()
-c.op_export_keys(keys, 0, sink)
-support.print_data(sink)
+c.export(fpr, sink=sink)
+sink.seek(0, os.SEEK_SET)
+data = sink.read()
+assert data
+
+try:
+ nonexisting_mode = 9999
+ c.export(fpr, mode=nonexisting_mode)
+ assert False, "Export should raise!"
+except gpg.errors.GPGMEError as e:
+ pass
diff --git a/lang/python/tests/t-import.py b/lang/python/tests/t-import.py
index e2edf5a2..fbf0d98b 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,8 @@ 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.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.import_(open(support.make_filename("seckey-1.asc"), 'rb').read())
check_result(result, "ADAB7FCC1F4DE2616ECFA402AF82244F9CD9FD55", True)
File Metadata
Details
Attached
Mime Type
text/x-diff
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
196760
Attached To
T4001: Import and Export for python bindings
Event Timeline
Log In to Comment