Blame lang/python/src/results.py

Packit d7e8d0
# Robust result objects
Packit d7e8d0
#
Packit d7e8d0
# Copyright (C) 2016 g10 Code GmbH
Packit d7e8d0
#
Packit d7e8d0
# This file is part of GPGME.
Packit d7e8d0
#
Packit d7e8d0
# GPGME is free software; you can redistribute it and/or modify it
Packit d7e8d0
# under the terms of the GNU Lesser General Public License as
Packit d7e8d0
# published by the Free Software Foundation; either version 2.1 of the
Packit d7e8d0
# License, or (at your option) any later version.
Packit d7e8d0
#
Packit d7e8d0
# GPGME is distributed in the hope that it will be useful, but WITHOUT
Packit d7e8d0
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
Packit d7e8d0
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General
Packit d7e8d0
# Public License for more details.
Packit d7e8d0
#
Packit d7e8d0
# You should have received a copy of the GNU Lesser General Public
Packit Service 30b792
# License along with this program; if not, see <https://www.gnu.org/licenses/>.
Packit d7e8d0
Packit d7e8d0
from __future__ import absolute_import, print_function, unicode_literals
Packit d7e8d0
del absolute_import, print_function, unicode_literals
Packit d7e8d0
"""Robust result objects
Packit d7e8d0
Packit d7e8d0
Results returned by the underlying library are fragile, i.e. they are
Packit d7e8d0
only valid until the next operation is performed in the context.
Packit d7e8d0
Packit d7e8d0
We cannot arbitrarily constrain the lifetime of Python objects, we
Packit d7e8d0
therefore create deep copies of the results.
Packit d7e8d0
Packit d7e8d0
"""
Packit d7e8d0
Packit Service 30b792
Packit d7e8d0
class Result(object):
Packit d7e8d0
    """Result object
Packit d7e8d0
Packit d7e8d0
    Describes the result of an operation.
Packit d7e8d0
Packit d7e8d0
    """
Packit d7e8d0
    """Convert to types"""
Packit d7e8d0
    _type = {}
Packit d7e8d0
    """Map functions over list attributes"""
Packit d7e8d0
    _map = {}
Packit d7e8d0
    """Automatically copy unless blacklisted"""
Packit d7e8d0
    _blacklist = {
Packit Service 30b792
        'acquire',
Packit Service 30b792
        'append',
Packit Service 30b792
        'disown',
Packit Service 30b792
        'next',
Packit Service 30b792
        'own',
Packit Service 30b792
        'this',
Packit Service 30b792
        'thisown',
Packit d7e8d0
    }
Packit Service 30b792
Packit d7e8d0
    def __init__(self, fragile):
Packit d7e8d0
        for key, func in self._type.items():
Packit d7e8d0
            if hasattr(fragile, key):
Packit d7e8d0
                setattr(self, key, func(getattr(fragile, key)))
Packit d7e8d0
Packit d7e8d0
        for key, func in self._map.items():
Packit d7e8d0
            if hasattr(fragile, key):
Packit d7e8d0
                setattr(self, key, list(map(func, getattr(fragile, key))))
Packit d7e8d0
Packit d7e8d0
        for key in dir(fragile):
Packit d7e8d0
            if key.startswith('_') or key in self._blacklist:
Packit d7e8d0
                continue
Packit d7e8d0
            if hasattr(self, key):
Packit d7e8d0
                continue
Packit d7e8d0
Packit d7e8d0
            setattr(self, key, getattr(fragile, key))
Packit d7e8d0
Packit d7e8d0
    def __repr__(self):
Packit d7e8d0
        return '{}({})'.format(
Packit d7e8d0
            self.__class__.__name__,
Packit Service 30b792
            ', '.join('{}={!r}'.format(k, getattr(self, k)) for k in dir(self)
Packit Service 30b792
                      if not k.startswith('_')))
Packit Service 30b792
Packit d7e8d0
Packit d7e8d0
class InvalidKey(Result):
Packit d7e8d0
    pass
Packit d7e8d0
Packit Service 30b792
Packit d7e8d0
class EncryptResult(Result):
Packit d7e8d0
    _map = dict(invalid_recipients=InvalidKey)
Packit d7e8d0
Packit Service 30b792
Packit d7e8d0
class Recipient(Result):
Packit d7e8d0
    pass
Packit d7e8d0
Packit Service 30b792
Packit d7e8d0
class DecryptResult(Result):
Packit d7e8d0
    _type = dict(wrong_key_usage=bool, is_de_vs=bool)
Packit d7e8d0
    _map = dict(recipients=Recipient)
Packit d7e8d0
Packit Service 30b792
Packit d7e8d0
class NewSignature(Result):
Packit d7e8d0
    pass
Packit d7e8d0
Packit Service 30b792
Packit d7e8d0
class SignResult(Result):
Packit d7e8d0
    _map = dict(invalid_signers=InvalidKey, signatures=NewSignature)
Packit d7e8d0
Packit Service 30b792
Packit d7e8d0
class Notation(Result):
Packit d7e8d0
    pass
Packit d7e8d0
Packit Service 30b792
Packit d7e8d0
class Signature(Result):
Packit d7e8d0
    _type = dict(wrong_key_usage=bool, chain_model=bool, is_de_vs=bool)
Packit d7e8d0
    _map = dict(notations=Notation)
Packit d7e8d0
Packit Service 30b792
Packit d7e8d0
class VerifyResult(Result):
Packit d7e8d0
    _map = dict(signatures=Signature)
Packit d7e8d0
Packit Service 30b792
Packit d7e8d0
class ImportStatus(Result):
Packit d7e8d0
    pass
Packit d7e8d0
Packit Service 30b792
Packit d7e8d0
class ImportResult(Result):
Packit d7e8d0
    _map = dict(imports=ImportStatus)
Packit d7e8d0
Packit Service 30b792
Packit d7e8d0
class GenkeyResult(Result):
Packit d7e8d0
    _type = dict(primary=bool, sub=bool)
Packit d7e8d0
Packit Service 30b792
Packit d7e8d0
class KeylistResult(Result):
Packit d7e8d0
    _type = dict(truncated=bool)
Packit d7e8d0
Packit Service 30b792
Packit d7e8d0
class VFSMountResult(Result):
Packit d7e8d0
    pass
Packit d7e8d0
Packit Service 30b792
Packit d7e8d0
class EngineInfo(Result):
Packit d7e8d0
    pass