[PATCH] python: Add more python import and export functions.

Tobias Mueller muelli at cryptobitch.de
Tue Aug 22 17:57:12 CEST 2017


* lang/python/gpg/core.py (Context): Added import_ and export
* lang/python/tests/t-export.py: New test for export function
* lang/python/tests/t-import.py: New test for import function
* lang/python/tests/t-op-export.py: Renamed from t-export.py
* lang/python/tests/t-op-import.py: Renamed from t-import.py

--

gpgme's op_import and op_export functions return an error code rather
than a result. In Python, exceptions are thrown if any of those
functions returned an error code. This change gives a more natural
import and export function which return the result object or throw on
error.

Signed-off-by: Tobias Mueller <muelli at cryptobitch.de>
---
 lang/python/src/core.py          | 44 ++++++++++++++++++++++
 lang/python/tests/t-export.py    | 36 ++++++++++++------
 lang/python/tests/t-import.py    |  8 ++--
 lang/python/tests/t-op-export.py | 40 ++++++++++++++++++++
 lang/python/tests/t-op-import.py | 79 ++++++++++++++++++++++++++++++++++++++++
 5 files changed, 190 insertions(+), 17 deletions(-)
 mode change 100755 => 100644 lang/python/tests/t-export.py
 create mode 100755 lang/python/tests/t-op-export.py
 create mode 100644 lang/python/tests/t-op-import.py

diff --git a/lang/python/src/core.py b/lang/python/src/core.py
index bd95d23..453f12e 100644
--- a/lang/python/src/core.py
+++ b/lang/python/src/core.py
@@ -509,6 +509,50 @@ 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)
+        if data and not sink:
+            data.seek(0, os.SEEK_SET)
+            exported_bytes = data.read()
+        else:
+            exported_bytes = None
+        return exported_bytes
+
+
     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 b9d5204..5aa2e32
--- 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 e2edf5a..1520e94 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)
diff --git a/lang/python/tests/t-op-export.py b/lang/python/tests/t-op-export.py
new file mode 100755
index 0000000..4927beb
--- /dev/null
+++ b/lang/python/tests/t-op-export.py
@@ -0,0 +1,40 @@
+#!/usr/bin/env python
+
+# Copyright (C) 2016 g10 Code GmbH
+#
+# This file is part of GPGME.
+#
+# GPGME is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# GPGME is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General
+# Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this program; if not, see <http://www.gnu.org/licenses/>.
+
+from __future__ import absolute_import, print_function, unicode_literals
+del absolute_import, print_function, unicode_literals
+
+import gpg
+import support
+
+support.init_gpgme(gpg.constants.protocol.OpenPGP)
+c = gpg.Context()
+c.set_armor(True)
+
+sink = gpg.Data()
+c.op_export_ext(['Alpha', 'Bob'], 0, sink)
+support.print_data(sink)
+
+# Again. Now using a key array.
+keys = []
+keys.append(c.get_key("0x68697734", False)) # Alpha
+keys.append(c.get_key("0xA9E3B0B2", False)) # Bob
+sink = gpg.Data()
+c.op_export_keys(keys, 0, sink)
+support.print_data(sink)
diff --git a/lang/python/tests/t-op-import.py b/lang/python/tests/t-op-import.py
new file mode 100644
index 0000000..5b0576f
--- /dev/null
+++ b/lang/python/tests/t-op-import.py
@@ -0,0 +1,79 @@
+#!/usr/bin/env python
+
+# Copyright (C) 2016 g10 Code GmbH
+#
+# This file is part of GPGME.
+#
+# GPGME is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# GPGME is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General
+# Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this program; if not, see <http://www.gnu.org/licenses/>.
+
+from __future__ import absolute_import, print_function, unicode_literals
+del absolute_import, print_function, unicode_literals
+
+import gpg
+import support
+
+def check_result(result, fpr, secret):
+    assert result.considered == 1 or (secret and result.considered == 3)
+    assert result.no_user_id == 0
+    assert not ((secret and result.imported != 0)
+                or (not secret and (result.imported != 0
+                                    and result.imported != 1)))
+    assert result.imported_rsa == 0
+    assert not ((secret and (result.unchanged != 0 and result.unchanged != 1))
+                or (not secret and ((result.imported == 0
+                                     and result.unchanged != 1)
+                                 or (result.imported == 1
+                                     and result.unchanged != 0))))
+    assert result.new_user_ids == 0
+    assert result.new_sub_keys == 0
+    assert not ((secret
+                 and ((result.secret_imported == 0
+                       and result.new_signatures != 0)
+                      or (result.secret_imported == 1
+                          and result.new_signatures > 1)))
+                or (not secret and result.new_signatures != 0))
+    assert result.new_revocations == 0
+    assert not ((secret and result.secret_read != 1 and result.secret_read != 3)
+                or (not secret and result.secret_read != 0))
+    assert not ((secret and result.secret_imported != 0
+                 and result.secret_imported != 1
+                 and result.secret_imported != 2)
+                or (not secret and result.secret_imported != 0))
+    assert not ((secret
+                 and ((result.secret_imported == 0
+                       and result.secret_unchanged != 1
+                       and result.secret_unchanged != 2)
+                      or (result.secret_imported == 1
+                          and result.secret_unchanged != 0)))
+                or (not secret and result.secret_unchanged != 0))
+    assert result.not_imported == 0
+    if secret:
+        assert not (len(result.imports) in (0, 3))
+    else:
+        assert not (len(result.imports) in (0, 2))
+
+    assert fpr == result.imports[0].fpr
+    assert len(result.imports) == 1 or fpr == result.imports[1].fpr
+    assert result.imports[0].result == 0
+
+support.init_gpgme(gpg.constants.protocol.OpenPGP)
+c = gpg.Context()
+
+c.op_import(gpg.Data(file=support.make_filename("pubkey-1.asc")))
+result = c.op_import_result()
+check_result(result, "ADAB7FCC1F4DE2616ECFA402AF82244F9CD9FD55", False)
+
+c.op_import(gpg.Data(file=support.make_filename("seckey-1.asc")))
+result = c.op_import_result()
+check_result(result, "ADAB7FCC1F4DE2616ECFA402AF82244F9CD9FD55", True)
-- 
2.7.4




More information about the Gnupg-devel mailing list