Exemple #1
0
    def test_load_attribute(self):
        # already imported
        self.assertIdentical(modules.load_attribute('sys.path'), sys.path)
        # unimported
        myfoo = modules.load_attribute('mod_testpack.mod_test2.foo')

        # "Unable to import"
        # pylint: disable=F0401

        from mod_testpack.mod_test2 import foo
        self.assertIdentical(foo, myfoo)
        # nonexisting attribute
        self.assertRaises(
            modules.FailedImport,
            modules.load_attribute, 'snakeoil.froznicator')
        # nonexisting top-level
        self.assertRaises(
            modules.FailedImport, modules.load_attribute,
            'spork_does_not_exist.foo')
        # not an attr
        self.assertRaises(
            modules.FailedImport, modules.load_attribute, 'sys')
        # not imported yet
        self.assertRaises(
            modules.FailedImport,
            modules.load_attribute, 'mod_testpack.mod_test3')
Exemple #2
0
def convert_string(central, value, arg_type):
    """Conversion func for a string-based DictConfigSection."""
    if not isinstance(value, str):
        raise ValueError(
            'convert_string invoked with non str instance: '
            f'val({value!r}), arg_type({arg_type!r})'
        )
    if arg_type == 'callable':
        try:
            func = modules.load_attribute(value)
        except modules.FailedImport as e:
            raise errors.ConfigurationError(f'cannot import {value!r}') from e
        if not callable(func):
            raise errors.ConfigurationError(f'{value!r} is not callable')
        return func
    elif arg_type.startswith('refs:'):
        return list(LazyNamedSectionRef(central, arg_type, ref)
                    for ref in str_to_list(value))
    elif arg_type.startswith('ref:'):
        return LazyNamedSectionRef(central, arg_type, str_to_str(value))
    elif arg_type == 'repr':
        return 'str', value
    func = _str_converters.get(arg_type)
    if func is None:
        raise errors.ConfigurationError(f'unknown type {arg_type!r}')
    return func(value)
Exemple #3
0
    def test_load_attribute(self):
        # already imported
        self.assertIdentical(modules.load_attribute('sys.path'), sys.path)
        # unimported
        myfoo = modules.load_attribute('mod_testpack.mod_test2.foo')

        # "Unable to import"
        # pylint: disable=F0401

        from mod_testpack.mod_test2 import foo
        self.assertIdentical(foo, myfoo)
        # nonexisting attribute
        self.assertRaises(modules.FailedImport, modules.load_attribute,
                          'snakeoil.froznicator')
        # nonexisting top-level
        self.assertRaises(modules.FailedImport, modules.load_attribute,
                          'spork_does_not_exist.foo')
        # not an attr
        self.assertRaises(modules.FailedImport, modules.load_attribute, 'sys')
        # not imported yet
        self.assertRaises(modules.FailedImport, modules.load_attribute,
                          'mod_testpack.mod_test3')
Exemple #4
0
    def test_load_attribute(self):
        # already imported
        assert modules.load_attribute('sys.path') is sys.path
        # unimported
        myfoo = modules.load_attribute('mod_testpack.mod_test2.foo')

        # "Unable to import"
        # pylint: disable=F0401

        from mod_testpack.mod_test2 import foo
        assert foo is myfoo
        # nonexisting attribute
        with pytest.raises(modules.FailedImport):
            modules.load_attribute('snakeoil.froznicator')
        # nonexisting top-level
        with pytest.raises(modules.FailedImport):
            modules.load_attribute('spork_does_not_exist.foo')
        # not an attr
        with pytest.raises(modules.FailedImport):
            modules.load_attribute('sys')
        # not imported yet
        with pytest.raises(modules.FailedImport):
            modules.load_attribute('mod_testpack.mod_test3')
Exemple #5
0
    def test_load_attribute(self):
        # already imported
        assert modules.load_attribute('sys.path') is sys.path
        # unimported
        myfoo = modules.load_attribute('mod_testpack.mod_test2.foo')

        # "Unable to import"
        # pylint: disable=F0401

        from mod_testpack.mod_test2 import foo
        assert foo is myfoo
        # nonexisting attribute
        with pytest.raises(modules.FailedImport):
            modules.load_attribute('snakeoil.froznicator')
        # nonexisting top-level
        with pytest.raises(modules.FailedImport):
            modules.load_attribute('spork_does_not_exist.foo')
        # not an attr
        with pytest.raises(modules.FailedImport):
            modules.load_attribute('sys')
        # not imported yet
        with pytest.raises(modules.FailedImport):
            modules.load_attribute('mod_testpack.mod_test3')
Exemple #6
0
def python_namespace_type(value, module=False, attribute=False):
    """
    return the object from python namespace that value specifies

    :param value: python namespace, snakeoil.modules for example
    :param module: if true, the object must be a module
    :param attribute: if true, the object must be a non-module
    :raises ValueError: if the conditions aren't met, or import fails
    """
    try:
        if module:
            return import_module(value)
        elif attribute:
            return modules.load_attribute(value)
        return modules.load_any(value)
    except (ImportError, modules.FailedImport) as err:
        compatibility.raise_from(argparse.ArgumentTypeError(str(err)))
Exemple #7
0
def python_namespace_type(value, module=False, attribute=False):
    """
    return the object from python namespace that value specifies

    :param value: python namespace, snakeoil.modules for example
    :param module: if true, the object must be a module
    :param attribute: if true, the object must be a non-module
    :raises ValueError: if the conditions aren't met, or import fails
    """
    try:
        if module:
            return import_module(value)
        elif attribute:
            return modules.load_attribute(value)
        return modules.load_any(value)
    except (ImportError, modules.FailedImport) as err:
        compatibility.raise_from(argparse.ArgumentTypeError(str(err)))
Exemple #8
0
def format_version(project, file_in_the_repo, api_version):
    from snakeoil import modules
    cwd = os.path.dirname(os.path.abspath(file_in_the_repo))
    try:
        version_info = modules.load_attribute(
            '%s._verinfo.version_info' % (project,))
    except modules.FailedImport:
        version_info = get_git_version(cwd)

    if not version_info:
        s = "extend version info unavailable"
    elif version_info['tag'] == api_version:
        s = 'released %s' % (version_info['date'],)
    else:
        s = ('vcs version %s, date %s' %
             (version_info['rev'], version_info['date']))

    return '%s %s\n%s' % (project, api_version, s)
Exemple #9
0
def format_version(project, file_in_the_repo, api_version):
    from snakeoil import modules
    cwd = os.path.dirname(os.path.abspath(file_in_the_repo))
    try:
        version_info = modules.load_attribute('%s._verinfo.version_info' %
                                              (project, ))
    except modules.FailedImport:
        version_info = get_git_version(cwd)

    if not version_info:
        s = "extend version info unavailable"
    elif version_info['tag'] == api_version:
        s = 'released %s' % (version_info['date'], )
    else:
        s = ('vcs version %s, date %s' %
             (version_info['rev'], version_info['date']))

    return '%s %s\n%s' % (project, api_version, s)
Exemple #10
0
                if isinstance(filename, basestring) and fchksum is not None:
                    return long(fchksum.fmd5t(filename)[0], 16)
                return chksum_loop_over_file(filename, md5.new)[0]

        chksum_types["md5"] = MD5Chksummer()


# expand this to load all available at some point
for k, v, str_size in (("sha1", "SHA", sha1_size),
    ("sha256", "SHA256", sha256_size),
    ("sha512", "SHA512", sha512_size),
    ("rmd160", "RIPEMD", rmd160_size)):
    if k in chksum_types:
        continue
    try:
        chksum_types[k] = Chksummer(k, modules.load_attribute(
            "Crypto.Hash.%s.new" % v), str_size)
    except modules.FailedImport:
        pass
del k, v


for modulename, chksumname, size in [
    ('sha', 'sha1', sha1_size),
    ('md5', 'md5', md5_size),
    ]:
    if chksumname not in chksum_types:
        chksum_types[chksumname] = Chksummer(chksumname,
            modules.load_attribute('%s.new' % (modulename,)), size)
del modulename, chksumname

try:
Exemple #11
0
 def render_value(self, central, name, arg_type):
     value = self.section[name]
     if arg_type == 'callable':
         if len(value) != 1:
             raise errors.ConfigurationError('only one argument required')
         value = value[0]
         if not isinstance(value, basestring):
             raise errors.ConfigurationError(
                 'need a callable, not a section')
         try:
             value = modules.load_attribute(value)
         except modules.FailedImport:
             raise errors.ConfigurationError('cannot import %r' % (value,))
         if not callable(value):
             raise errors.ConfigurationError('%r is not callable' % value)
         return value
     elif arg_type.startswith('ref:'):
         if len(value) != 1:
             raise errors.ConfigurationError('only one argument required')
         value = value[0]
         if isinstance(value, basestring):
             # it's a section ref
             return basics.LazyNamedSectionRef(central, arg_type, value)
         else:
             # it's an anonymous inline section
             return basics.LazyUnnamedSectionRef(central, arg_type,
                                                 ConfigSection(value))
     elif arg_type.startswith('refs:'):
         result = []
         for ref in value:
             if isinstance(ref, basestring):
                 # it's a section ref
                 result.append(basics.LazyNamedSectionRef(
                         central, arg_type, ref))
             else:
                 # it's an anonymous inline section
                 result.append(basics.LazyUnnamedSectionRef(
                         central, arg_type, ConfigSection(ref)))
         return None, result, None
     elif arg_type == 'list':
         if not isinstance(value, basestring):
             # sequence
             value = ' '.join(value)
         return None, basics.str_to_list(value), None
     elif arg_type == 'repr':
         if len(value) == 1:
             value = value[0]
             if isinstance(value, basestring):
                 return 'str', value
             return 'ref', ConfigSection(value)
         if all(isinstance(v, basestring) for v in value):
             return 'list', list(value)
         result = []
         for v in value:
             if not isinstance(v, basestring):
                 v = ConfigSection(v)
             result.append(v)
         return 'refs', result
     else:
         if len(value) != 1:
             raise errors.ConfigurationError('only one argument required')
         if not isinstance(value[0], basestring):
             raise errors.ConfigurationError(
                 '%r should be a string' % value)
         if arg_type == 'str':
             return [None, basics.str_to_str(value[0]), None]
         elif arg_type == 'bool':
             return basics.str_to_bool(value[0])
         else:
             raise errors.ConfigurationError(
                 'unsupported type %r' % (arg_type,))
Exemple #12
0
    else:
        chksum_types[chksumname] = Chksummer(
            chksumname, partial(hashlib.new, hashlibname), size)
del hashlibname, chksumname

# expand this to load all available at some point
for k, v, str_size in (
        ("sha1", "SHA", sha1_size),
        ("sha256", "SHA256", sha256_size),
        ("sha512", "SHA512", sha512_size),
        ("rmd160", "RIPEMD", rmd160_size),
    ):
    if k in chksum_types:
        continue
    try:
        chksum_types[k] = Chksummer(k, modules.load_attribute(
            "Crypto.Hash.%s.new" % v), str_size)
    except modules.FailedImport:
        pass
del k, v


class SizeUpdater(object):

    def __init__(self):
        self.count = 0

    def update(self, data):
        self.count += len(data)

    def hexdigest(self):
        return "%x" % self.count
Exemple #13
0
 def render_value(self, central, name, arg_type):
     value = self.section[name]
     if arg_type == 'callable':
         if len(value) != 1:
             raise errors.ConfigurationError('only one argument required')
         value = value[0]
         if not isinstance(value, basestring):
             raise errors.ConfigurationError(
                 'need a callable, not a section')
         try:
             value = modules.load_attribute(value)
         except modules.FailedImport:
             raise errors.ConfigurationError('cannot import %r' % (value,))
         if not callable(value):
             raise errors.ConfigurationError('%r is not callable' % value)
         return value
     elif arg_type.startswith('ref:'):
         if len(value) != 1:
             raise errors.ConfigurationError('only one argument required')
         value = value[0]
         if isinstance(value, basestring):
             # it's a section ref
             return basics.LazyNamedSectionRef(central, arg_type, value)
         else:
             # it's an anonymous inline section
             return basics.LazyUnnamedSectionRef(central, arg_type,
                                                 ConfigSection(value))
     elif arg_type.startswith('refs:'):
         result = []
         for ref in value:
             if isinstance(ref, basestring):
                 # it's a section ref
                 result.append(basics.LazyNamedSectionRef(
                         central, arg_type, ref))
             else:
                 # it's an anonymous inline section
                 result.append(basics.LazyUnnamedSectionRef(
                         central, arg_type, ConfigSection(ref)))
         return None, result, None
     elif arg_type == 'list':
         if not isinstance(value, basestring):
             # sequence
             value = ' '.join(value)
         return None, basics.str_to_list(value), None
     elif arg_type == 'repr':
         if len(value) == 1:
             value = value[0]
             if isinstance(value, basestring):
                 return 'str', value
             return 'ref', ConfigSection(value)
         if all(isinstance(v, basestring) for v in value):
             return 'list', list(value)
         result = []
         for v in value:
             if not isinstance(v, basestring):
                 v = ConfigSection(v)
             result.append(v)
         return 'refs', result
     else:
         if len(value) != 1:
             raise errors.ConfigurationError('only one argument required')
         if not isinstance(value[0], basestring):
             raise errors.ConfigurationError(
                 '%r should be a string' % value)
         if arg_type == 'str':
             return [None, basics.str_to_str(value[0]), None]
         elif arg_type == 'bool':
             return basics.str_to_bool(value[0])
         else:
             raise errors.ConfigurationError(
                 'unsupported type %r' % (arg_type,))
Exemple #14
0
                if isinstance(filename, basestring) and fchksum is not None:
                    return long(fchksum.fmd5t(filename)[0], 16)
                return chksum_loop_over_file(filename, md5.new)[0]

        chksum_types["md5"] = MD5Chksummer()


# expand this to load all available at some point
for k, v, str_size in (("sha1", "SHA", sha1_size),
    ("sha256", "SHA256", sha256_size),
    ("sha512", "SHA512", sha512_size),
    ("rmd160", "RIPEMD", rmd160_size)):
    if k in chksum_types:
        continue
    try:
        chksum_types[k] = Chksummer(k, modules.load_attribute(
            "Crypto.Hash.%s.new" % v), str_size)
    except modules.FailedImport:
        pass
del k, v


for modulename, chksumname, size in [
    ('sha', 'sha1', sha1_size),
    ('md5', 'md5', md5_size),
    ]:
    if chksumname not in chksum_types:
        chksum_types[chksumname] = Chksummer(chksumname,
            modules.load_attribute('%s.new' % (modulename,)), size)
del modulename, chksumname

try:
Exemple #15
0
]:
    try:
        hashlib.new(hashlibname)
    except ValueError:
        pass  # This hash is not available.
    else:
        chksum_types[chksumname] = Chksummer(chksumname,
                                             partial(hashlib.new, hashlibname),
                                             size)
del hashlibname, chksumname

if 'whirlpool' not in chksum_types:
    # Fallback to the python implementation.
    chksum_types['whirlpool'] = Chksummer(
        'whirlpool',
        modules.load_attribute('snakeoil.chksum._whirlpool.Whirlpool'),
        whirlpool_size)

# expand this to load all available at some point
for k, v, str_size in (
    ("sha1", "SHA", sha1_size),
    ("sha256", "SHA256", sha256_size),
    ("sha512", "SHA512", sha512_size),
    ("rmd160", "RIPEMD", rmd160_size),
):
    if k in chksum_types:
        continue
    try:
        chksum_types[k] = Chksummer(
            k, modules.load_attribute("Crypto.Hash.%s.new" % v), str_size)
    except modules.FailedImport: