From 6641c7814b30e3e8f18105b2636545cc1bd07552 Mon Sep 17 00:00:00 2001 From: Justus Winter Date: Tue, 14 Jun 2016 12:37:26 +0200 Subject: [PATCH] python: Simplify wrapping glue. * lang/python/pyme/core.py: Rename '_getctype' to '_ctype' and turn it into a string. Likewise rename '_getnameprepend' to '_cprefix'. * lang/python/helpers.c: Adapt accordingly. Signed-off-by: Justus Winter --- lang/python/helpers.c | 4 ++-- lang/python/pyme/core.py | 41 ++++++++++++++++++++-------------------- 2 files changed, 22 insertions(+), 23 deletions(-) diff --git a/lang/python/helpers.c b/lang/python/helpers.c index 5380ff26..6de2b8de 100644 --- a/lang/python/helpers.c +++ b/lang/python/helpers.c @@ -171,7 +171,7 @@ PyObject * object_to_gpgme_t(PyObject *input, const char *objtype, int argnum) { PyObject *pyname = NULL, *pypointer = NULL; - pyname = PyObject_CallMethod(input, "_getctype", NULL); + pyname = PyObject_GetAttrString(input, "_ctype"); if (pyname && PyUnicode_Check(pyname)) { if (strcmp(PyUnicode_AsUTF8(pyname), objtype) != 0) @@ -261,7 +261,7 @@ object_to_gpgme_data_t(PyObject *input, int argnum, gpgme_data_t *wrapper, } /* As last resort we assume it is a wrapped data object. */ - if (PyObject_HasAttrString(data, "_getctype")) + if (PyObject_HasAttrString(data, "_ctype")) return object_to_gpgme_t(data, "gpgme_data_t", argnum); return PyErr_Format(PyExc_TypeError, diff --git a/lang/python/pyme/core.py b/lang/python/pyme/core.py index e5a50617..c0903312 100644 --- a/lang/python/pyme/core.py +++ b/lang/python/pyme/core.py @@ -61,17 +61,22 @@ class GpgmeWrapper(object): else: return repr(self.wrapped) == repr(other.wrapped) - def _getctype(self): - """Must be implemented by child classes. + @property + def _ctype(self): + """The name of the c type wrapped by this class - Must return the name of the c type.""" + Must be set by child classes. + + """ raise NotImplementedError() - def _getnameprepend(self): - """Must be implemented by child classes. + @property + def _cprefix(self): + """The common prefix of c functions wrapped by this class - Must return the prefix of all c functions mapped to methods of - this class.""" + Must be set by child classes. + + """ raise NotImplementedError() def _errorcheck(self, name): @@ -86,9 +91,9 @@ class GpgmeWrapper(object): def __wrap_boolean_property(self, key, do_set=False, value=None): get_func = getattr(pygpgme, - "{}get_{}".format(self._getnameprepend(), key)) + "{}get_{}".format(self._cprefix, key)) set_func = getattr(pygpgme, - "{}set_{}".format(self._getnameprepend(), key)) + "{}set_{}".format(self._cprefix, key)) def get(slf): return bool(get_func(slf.wrapped)) def set_(slf, value): @@ -104,13 +109,13 @@ class GpgmeWrapper(object): def __getattr__(self, key): """On-the-fly generation of wrapper methods and properties""" - if key[0] == '_' or self._getnameprepend() == None: + if key[0] == '_' or self._cprefix == None: return None if key in self._boolean_properties: return self.__wrap_boolean_property(key) - name = self._getnameprepend() + key + name = self._cprefix + key func = getattr(pygpgme, name) if self._errorcheck(name): @@ -181,11 +186,8 @@ class Context(GpgmeWrapper): def pinentry_mode(self, value): self.set_pinentry_mode(value) - def _getctype(self): - return 'gpgme_ctx_t' - - def _getnameprepend(self): - return 'gpgme_' + _ctype = 'gpgme_ctx_t' + _cprefix = 'gpgme_' def _errorcheck(self, name): """This function should list all functions returning gpgme_error_t""" @@ -432,11 +434,8 @@ class Data(GpgmeWrapper): """ - def _getctype(self): - return 'gpgme_data_t' - - def _getnameprepend(self): - return 'gpgme_data_' + _ctype = 'gpgme_data_t' + _cprefix = 'gpgme_data_' def _errorcheck(self, name): """This function should list all functions returning gpgme_error_t"""