From f4ba16b31ea282d0787a40be3f37b951584143a1 Mon Sep 17 00:00:00 2001 From: Justus Winter Date: Tue, 10 May 2016 13:19:26 +0200 Subject: python: Rename bindings. -- Signed-off-by: Justus Winter --- lang/python/pyme/util.py | 72 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 lang/python/pyme/util.py (limited to 'lang/python/pyme/util.py') diff --git a/lang/python/pyme/util.py b/lang/python/pyme/util.py new file mode 100644 index 00000000..c5f02a96 --- /dev/null +++ b/lang/python/pyme/util.py @@ -0,0 +1,72 @@ +# $Id$ +# Copyright (C) 2004,2008 Igor Belyi +# Copyright (C) 2002 John Goerzen +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# This library 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 library; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +from . import pygpgme +from .errors import errorcheck + +def process_constants(starttext, dict): + """Called by the constant libraries to load up the appropriate constants + from the C library.""" + index = len(starttext) + for identifier in dir(pygpgme): + if not identifier.startswith(starttext): + continue + name = identifier[index:] + dict[name] = getattr(pygpgme, identifier) + +class GpgmeWrapper(object): + """Base class all Pyme wrappers for GPGME functionality. Not to be + instantiated directly.""" + def __repr__(self): + return '' % \ + (__name__, self.__class__.__name__, + self.wrapped) + + def __str__(self): + return repr(self) + + def __hash__(self): + return hash(repr(self.wrapped)) + + def __eq__(self, other): + if other == None: + return False + else: + return repr(self.wrapped) == repr(other.wrapped) + + def _getctype(self): + raise NotImplementedException + + def __getattr__(self, name): + """On-the-fly function generation.""" + if name[0] == '_' or self._getnameprepend() == None: + return None + name = self._getnameprepend() + name + if self._errorcheck(name): + def _funcwrap(*args, **kwargs): + args = [self.wrapped] + list(args) + return errorcheck(getattr(pygpgme, name)(*args, **kwargs), + "Invocation of " + name) + else: + def _funcwrap(*args, **kwargs): + args = [self.wrapped] + list(args) + return getattr(pygpgme, name)(*args, **kwargs) + + _funcwrap.__doc__ = getattr(getattr(pygpgme, name), "__doc__") + return _funcwrap + -- cgit v1.2.3 From aade53a12b9716997684b872bc2ac87229f73fb3 Mon Sep 17 00:00:00 2001 From: Justus Winter Date: Tue, 10 May 2016 13:30:30 +0200 Subject: python: Delete trailing whitespace. -- Signed-off-by: Justus Winter --- lang/python/pyme/util.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lang/python/pyme/util.py') diff --git a/lang/python/pyme/util.py b/lang/python/pyme/util.py index c5f02a96..32e2f556 100644 --- a/lang/python/pyme/util.py +++ b/lang/python/pyme/util.py @@ -28,7 +28,7 @@ def process_constants(starttext, dict): continue name = identifier[index:] dict[name] = getattr(pygpgme, identifier) - + class GpgmeWrapper(object): """Base class all Pyme wrappers for GPGME functionality. Not to be instantiated directly.""" @@ -51,7 +51,7 @@ class GpgmeWrapper(object): def _getctype(self): raise NotImplementedException - + def __getattr__(self, name): """On-the-fly function generation.""" if name[0] == '_' or self._getnameprepend() == None: -- cgit v1.2.3 From af9371eb63664c92fb67e8e7e03cc984e7d38a7f Mon Sep 17 00:00:00 2001 From: Justus Winter Date: Thu, 12 May 2016 12:51:30 +0200 Subject: python: Fix name of exception, make slot methods explicit. * lang/python/pyme/util.py (GpgmeWrapper._getctype): Fix exception, add docstring. (GpgmeWrapper._getnameprepend): New function. (GpgmeWrapper._errorcheck): Likewise. Signed-off-by: Justus Winter --- lang/python/pyme/util.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) (limited to 'lang/python/pyme/util.py') diff --git a/lang/python/pyme/util.py b/lang/python/pyme/util.py index 32e2f556..19bbb7f6 100644 --- a/lang/python/pyme/util.py +++ b/lang/python/pyme/util.py @@ -50,7 +50,24 @@ class GpgmeWrapper(object): return repr(self.wrapped) == repr(other.wrapped) def _getctype(self): - raise NotImplementedException + """Must be implemented by child classes. + + Must return the name of the c type.""" + raise NotImplementedError() + + def _getnameprepend(self): + """Must be implemented by child classes. + + Must return the prefix of all c functions mapped to methods of + this class.""" + raise NotImplementedError() + + def _errorcheck(self, name): + """Must be implemented by child classes. + + This function must return a trueish value for all c functions + returning gpgme_error_t.""" + raise NotImplementedError() def __getattr__(self, name): """On-the-fly function generation.""" -- cgit v1.2.3 From ed0ce84fbd2904bf59ac66ae7422716db3624efa Mon Sep 17 00:00:00 2001 From: Justus Winter Date: Thu, 12 May 2016 14:57:42 +0200 Subject: python: Cache generated wrapper functions. * lang/python/util.py (GpgmeWrap.__getattr__): Cache generated wrapper functions. Signed-off-by: Justus Winter --- lang/python/pyme/util.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'lang/python/pyme/util.py') diff --git a/lang/python/pyme/util.py b/lang/python/pyme/util.py index 19bbb7f6..a8560992 100644 --- a/lang/python/pyme/util.py +++ b/lang/python/pyme/util.py @@ -69,11 +69,11 @@ class GpgmeWrapper(object): returning gpgme_error_t.""" raise NotImplementedError() - def __getattr__(self, name): + def __getattr__(self, key): """On-the-fly function generation.""" - if name[0] == '_' or self._getnameprepend() == None: + if key[0] == '_' or self._getnameprepend() == None: return None - name = self._getnameprepend() + name + name = self._getnameprepend() + key if self._errorcheck(name): def _funcwrap(*args, **kwargs): args = [self.wrapped] + list(args) @@ -85,5 +85,8 @@ class GpgmeWrapper(object): return getattr(pygpgme, name)(*args, **kwargs) _funcwrap.__doc__ = getattr(getattr(pygpgme, name), "__doc__") + + # Cache the wrapper function. + setattr(self, key, _funcwrap) return _funcwrap -- cgit v1.2.3 From 11314f0db6e57597e3f56351a86fdb36a7a17dd7 Mon Sep 17 00:00:00 2001 From: Justus Winter Date: Thu, 12 May 2016 18:29:04 +0200 Subject: python: Share generated methods between objects. * lang/python/pyme/util.py (GpgmeWrapper.__getattr__): Monkey-patch the class. * lang/python/tests/t-wrapper.py: Demonstrate the sharing. Signed-off-by: Justus Winter --- lang/python/pyme/util.py | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) (limited to 'lang/python/pyme/util.py') diff --git a/lang/python/pyme/util.py b/lang/python/pyme/util.py index a8560992..a9fa19d2 100644 --- a/lang/python/pyme/util.py +++ b/lang/python/pyme/util.py @@ -74,19 +74,25 @@ class GpgmeWrapper(object): if key[0] == '_' or self._getnameprepend() == None: return None name = self._getnameprepend() + key + func = getattr(pygpgme, name) + if self._errorcheck(name): - def _funcwrap(*args, **kwargs): - args = [self.wrapped] + list(args) - return errorcheck(getattr(pygpgme, name)(*args, **kwargs), + def _funcwrap(slf, *args, **kwargs): + return errorcheck(func(slf.wrapped, *args, **kwargs), "Invocation of " + name) else: - def _funcwrap(*args, **kwargs): - args = [self.wrapped] + list(args) - return getattr(pygpgme, name)(*args, **kwargs) + def _funcwrap(slf, *args, **kwargs): + return func(slf.wrapped, *args, **kwargs) + + _funcwrap.__doc__ = getattr(func, "__doc__") + + # Monkey-patch the class. + setattr(self.__class__, key, _funcwrap) - _funcwrap.__doc__ = getattr(getattr(pygpgme, name), "__doc__") + # Bind the method to 'self'. + def wrapper(*args, **kwargs): + return _funcwrap(self, *args, **kwargs) + _funcwrap.__doc__ = getattr(func, "__doc__") - # Cache the wrapper function. - setattr(self, key, _funcwrap) - return _funcwrap + return wrapper -- cgit v1.2.3