diff options
| author | Justus Winter <[email protected]> | 2016-07-28 10:40:54 +0000 | 
|---|---|---|
| committer | Justus Winter <[email protected]> | 2016-07-28 12:23:07 +0000 | 
| commit | de69fa496c09386d5e99747670d6887cf52dd09e (patch) | |
| tree | 4debb7586a9b18b62eba374a67020e1f1ae69c1e /lang/python/pyme | |
| parent | python: Improve engine information handling. (diff) | |
| download | gpgme-de69fa496c09386d5e99747670d6887cf52dd09e.tar.gz gpgme-de69fa496c09386d5e99747670d6887cf52dd09e.zip | |
python: Support the Assuan engine.
* lang/python/gpgme.i: Add typemaps for the Assuan protocol callbacks.
* lang/python/helpers.c (_pyme_assuan_{data,inquire,status}_cb): New
functions.
* lang/python/private.h (_pyme_assuan_{data,inquire,status}_cb): New
prototypes.
* lang/python/pyme/core.py (Context.assuan_transact): New method.
* lang/python/pyme/util.py (percent_escape): New function.
* lang/python/tests/Makefile.am (py_tests): Add new test.
* lang/python/tests/t-protocol-assuan.py: New file.
Signed-off-by: Justus Winter <[email protected]>
Diffstat (limited to '')
| -rw-r--r-- | lang/python/pyme/core.py | 50 | ||||
| -rw-r--r-- | lang/python/pyme/util.py | 6 | 
2 files changed, 56 insertions, 0 deletions
| diff --git a/lang/python/pyme/core.py b/lang/python/pyme/core.py index 216e26fd..b25808dc 100644 --- a/lang/python/pyme/core.py +++ b/lang/python/pyme/core.py @@ -31,6 +31,7 @@ from . import pygpgme  from .errors import errorcheck, GPGMEError  from . import constants  from . import errors +from . import util  class GpgmeWrapper(object):      """Base wrapper class @@ -467,6 +468,55 @@ class Context(GpgmeWrapper):              plainbytes = data.read()          return plainbytes, result +    def assuan_transact(self, command, +                        data_cb=None, inquire_cb=None, status_cb=None): +        """Issue a raw assuan command + +        This function can be used to issue a raw assuan command to the +        engine. + +        If command is a string or bytes, it will be used as-is.  If it +        is an iterable of strings, it will be properly escaped and +        joined into an well-formed assuan command. + +        Keyword arguments: +        data_cb		-- a callback receiving data lines +        inquire_cb	-- a callback providing more information +        status_cb	-- a callback receiving status lines + +        Returns: +        result		-- the result of command as GPGMEError + +        Raises: +        GPGMEError	-- as signaled by the underlying library + +        """ + +        if isinstance(command, (str, bytes)): +            cmd = command +        else: +            cmd = " ".join(util.percent_escape(f) for f in command) + +        errptr = pygpgme.new_gpgme_error_t_p() + +        err = pygpgme.gpgme_op_assuan_transact_ext( +            self.wrapped, +            cmd, +            (weakref.ref(self), data_cb) if data_cb else None, +            (weakref.ref(self), inquire_cb) if inquire_cb else None, +            (weakref.ref(self), status_cb) if status_cb else None, +            errptr) + +        if self._callback_excinfo: +            pygpgme.pygpgme_raise_callback_exception(self) + +        errorcheck(err) + +        status = pygpgme.gpgme_error_t_p_value(errptr) +        pygpgme.delete_gpgme_error_t_p(errptr) + +        return GPGMEError(status) if status != 0 else None +      @property      def signers(self):          """Keys used for signing""" diff --git a/lang/python/pyme/util.py b/lang/python/pyme/util.py index bbd28fe7..7eb6353f 100644 --- a/lang/python/pyme/util.py +++ b/lang/python/pyme/util.py @@ -31,3 +31,9 @@ def process_constants(prefix, scope):                   if identifier.startswith(prefix)}      scope.update(constants)      return list(constants.keys()) + +def percent_escape(s): +    return ''.join( +        '%{0:2x}'.format(ord(c)) +        if c == '+' or c == '"' or c == '%' or ord(c) <= 0x20 else c +        for c in s) | 
