Blame lang/python/src/util.py

Packit d7e8d0
# Copyright (C) 2016 g10 Code GmbH
Packit d7e8d0
# Copyright (C) 2004,2008 Igor Belyi <belyi@users.sourceforge.net>
Packit d7e8d0
# Copyright (C) 2002 John Goerzen <jgoerzen@complete.org>
Packit d7e8d0
#
Packit d7e8d0
#    This library is free software; you can redistribute it and/or
Packit d7e8d0
#    modify it under the terms of the GNU Lesser General Public
Packit d7e8d0
#    License as published by the Free Software Foundation; either
Packit d7e8d0
#    version 2.1 of the License, or (at your option) any later version.
Packit d7e8d0
#
Packit d7e8d0
#    This library is distributed in the hope that it will be useful,
Packit d7e8d0
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit d7e8d0
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit d7e8d0
#    Lesser General Public License for more details.
Packit d7e8d0
#
Packit d7e8d0
#    You should have received a copy of the GNU Lesser General Public
Packit d7e8d0
#    License along with this library; if not, write to the Free Software
Packit d7e8d0
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
Packit d7e8d0
Packit d7e8d0
from __future__ import absolute_import, print_function, unicode_literals
Packit d7e8d0
del absolute_import, print_function, unicode_literals
Packit d7e8d0
Packit d7e8d0
import sys
Packit d7e8d0
Packit d7e8d0
def process_constants(prefix, scope):
Packit d7e8d0
    """Called by the constant modules to load up the constants from the C
Packit d7e8d0
    library starting with PREFIX.  Matching constants will be inserted
Packit d7e8d0
    into SCOPE with PREFIX stripped from the names.  Returns the names
Packit d7e8d0
    of inserted constants.
Packit d7e8d0
Packit d7e8d0
    """
Packit d7e8d0
    from . import gpgme
Packit d7e8d0
    index = len(prefix)
Packit d7e8d0
    constants = {identifier[index:]: getattr(gpgme, identifier)
Packit d7e8d0
                 for identifier in dir(gpgme)
Packit d7e8d0
                 if identifier.startswith(prefix)}
Packit d7e8d0
    scope.update(constants)
Packit d7e8d0
    return list(constants.keys())
Packit d7e8d0
Packit d7e8d0
def percent_escape(s):
Packit d7e8d0
    return ''.join(
Packit d7e8d0
        '%{0:2x}'.format(ord(c))
Packit d7e8d0
        if c == '+' or c == '"' or c == '%' or ord(c) <= 0x20 else c
Packit d7e8d0
        for c in s)
Packit d7e8d0
Packit d7e8d0
# Python2/3 compatibility
Packit d7e8d0
if sys.version_info[0] == 3:
Packit d7e8d0
    # Python3
Packit d7e8d0
    def is_a_string(x):
Packit d7e8d0
        return isinstance(x, str)
Packit d7e8d0
else:
Packit d7e8d0
    # Python2
Packit d7e8d0
    def is_a_string(x):
Packit d7e8d0
        return isinstance(x, basestring)