[Pkg-gnupg-commit] [gpgme] 65/132: python: Extend SWIG gpgme_{sub, }key with a __repr__ method.

Daniel Kahn Gillmor dkg at fifthhorseman.net
Wed Apr 26 01:01:26 UTC 2017


This is an automated email from the git hooks/post-receive script.

dkg pushed a commit to branch experimental
in repository gpgme.

commit 01d5c17587578c729bbbb60f8a65635975e35592
Author: Tobias Mueller <muelli at cryptobitch.de>
Date:   Tue Dec 20 18:02:36 2016 +0100

    python: Extend SWIG gpgme_{sub,}key with a __repr__ method.
    
    * lang/python/gpgme.i: Added a genericrepr macro and use it for
    gpgme_key, gpgme_subkey, and gpgme_key_sig.
    --
    
    To look nicer in Python's REPL.
    
    We define a generic __repr__ as a SWIG macro and use that to extend some
    defined SWIG objects.
    
    The alternative would have been to write a custom __repr__ function for
    each class but that would need to be changed everytime the object's
    structure changes. The bindings should be easy to maintain, I guess.
    This comes at the expense that the reprs are now relatively long and
    contain, for example, both keyid and fingerprint.
    
    Signed-off-by: Tobias Mueller <muelli at cryptobitch.de>
---
 lang/python/gpgme.i | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)

diff --git a/lang/python/gpgme.i b/lang/python/gpgme.i
index e21aa6c..c0993ee 100644
--- a/lang/python/gpgme.i
+++ b/lang/python/gpgme.i
@@ -649,3 +649,30 @@ _gpg_unwrap_gpgme_ctx_t(PyObject *wrapped)
 /* ... but only the public definitions here.  They will be exposed to
    the Python world, so let's be careful.  */
 %include "helpers.h"
+
+
+%define genericrepr(cls)
+%pythoncode %{
+    def __repr__(self):
+        names = [name for name in dir(self)
+            if not name.startswith("_") and name != "this"]
+        props = ", ".join(("{}={!r}".format(name, getattr(self, name))
+            for name in names)
+        )
+        return "cls({})".format(props)
+%}
+
+%enddef
+
+%extend _gpgme_key {
+  genericrepr(Key)
+};
+
+
+%extend _gpgme_subkey {
+  genericrepr(SubKey)
+};
+
+%extend _gpgme_key_sig {
+  genericrepr(KeySig)
+};

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-gnupg/gpgme.git



More information about the Pkg-gnupg-commit mailing list