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 <justus@g10code.com>
This commit is contained in:
Justus Winter 2016-06-14 12:37:26 +02:00
parent f3618bc615
commit 6641c7814b
2 changed files with 22 additions and 23 deletions

View File

@ -171,7 +171,7 @@ PyObject *
object_to_gpgme_t(PyObject *input, const char *objtype, int argnum) object_to_gpgme_t(PyObject *input, const char *objtype, int argnum)
{ {
PyObject *pyname = NULL, *pypointer = NULL; PyObject *pyname = NULL, *pypointer = NULL;
pyname = PyObject_CallMethod(input, "_getctype", NULL); pyname = PyObject_GetAttrString(input, "_ctype");
if (pyname && PyUnicode_Check(pyname)) if (pyname && PyUnicode_Check(pyname))
{ {
if (strcmp(PyUnicode_AsUTF8(pyname), objtype) != 0) 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. */ /* 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 object_to_gpgme_t(data, "gpgme_data_t", argnum);
return PyErr_Format(PyExc_TypeError, return PyErr_Format(PyExc_TypeError,

View File

@ -61,17 +61,22 @@ class GpgmeWrapper(object):
else: else:
return repr(self.wrapped) == repr(other.wrapped) return repr(self.wrapped) == repr(other.wrapped)
def _getctype(self): @property
"""Must be implemented by child classes. 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() raise NotImplementedError()
def _getnameprepend(self): @property
"""Must be implemented by child classes. 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 Must be set by child classes.
this class."""
"""
raise NotImplementedError() raise NotImplementedError()
def _errorcheck(self, name): def _errorcheck(self, name):
@ -86,9 +91,9 @@ class GpgmeWrapper(object):
def __wrap_boolean_property(self, key, do_set=False, value=None): def __wrap_boolean_property(self, key, do_set=False, value=None):
get_func = getattr(pygpgme, get_func = getattr(pygpgme,
"{}get_{}".format(self._getnameprepend(), key)) "{}get_{}".format(self._cprefix, key))
set_func = getattr(pygpgme, set_func = getattr(pygpgme,
"{}set_{}".format(self._getnameprepend(), key)) "{}set_{}".format(self._cprefix, key))
def get(slf): def get(slf):
return bool(get_func(slf.wrapped)) return bool(get_func(slf.wrapped))
def set_(slf, value): def set_(slf, value):
@ -104,13 +109,13 @@ class GpgmeWrapper(object):
def __getattr__(self, key): def __getattr__(self, key):
"""On-the-fly generation of wrapper methods and properties""" """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 return None
if key in self._boolean_properties: if key in self._boolean_properties:
return self.__wrap_boolean_property(key) return self.__wrap_boolean_property(key)
name = self._getnameprepend() + key name = self._cprefix + key
func = getattr(pygpgme, name) func = getattr(pygpgme, name)
if self._errorcheck(name): if self._errorcheck(name):
@ -181,11 +186,8 @@ class Context(GpgmeWrapper):
def pinentry_mode(self, value): def pinentry_mode(self, value):
self.set_pinentry_mode(value) self.set_pinentry_mode(value)
def _getctype(self): _ctype = 'gpgme_ctx_t'
return 'gpgme_ctx_t' _cprefix = 'gpgme_'
def _getnameprepend(self):
return 'gpgme_'
def _errorcheck(self, name): def _errorcheck(self, name):
"""This function should list all functions returning gpgme_error_t""" """This function should list all functions returning gpgme_error_t"""
@ -432,11 +434,8 @@ class Data(GpgmeWrapper):
""" """
def _getctype(self): _ctype = 'gpgme_data_t'
return 'gpgme_data_t' _cprefix = 'gpgme_data_'
def _getnameprepend(self):
return 'gpgme_data_'
def _errorcheck(self, name): def _errorcheck(self, name):
"""This function should list all functions returning gpgme_error_t""" """This function should list all functions returning gpgme_error_t"""