python: Simplify wrapping glue.
authorJustus Winter <justus@g10code.com>
Tue, 14 Jun 2016 10:37:26 +0000 (12:37 +0200)
committerJustus Winter <justus@g10code.com>
Thu, 16 Jun 2016 12:19:17 +0000 (14:19 +0200)
* 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>
lang/python/helpers.c
lang/python/pyme/core.py

index 5380ff2..6de2b8d 100644 (file)
@@ -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,
index e5a5061..c090331 100644 (file)
@@ -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 be set by child classes.
 
-        Must return the name of the c type."""
+        """
         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 be set by child classes.
 
-        Must return the prefix of all c functions mapped to methods of
-        this class."""
+        """
         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"""