def test_object_init(self): class kls(object): pass conf = basics.ConfigType(kls) self.assertEqual(conf.types, {}) self.assertEqual(conf.required, ())
def collapse_section(self, sections, _name=None): """Collapse a ConfigSection to a :obj:`CollapsedConfig`.""" if self._section_is_inherit_only(sections[0]): if sections[0].render_value(self, 'inherit-only', 'bool'): raise errors.CollapseInheritOnly( 'cannot collapse inherit-only section') relevant_sections = self._get_inherited_sections(_name, sections) config_stack = _ConfigStack() for data in relevant_sections: for key in data.section.keys(): config_stack[key].append(data) kls = config_stack.render_val(self, 'class', 'callable') if kls is None: raise errors.ConfigurationError('no class specified') type_obj = basics.ConfigType(kls) is_default = bool(config_stack.render_val(self, 'default', 'bool')) for key in ('inherit', 'inherit-only', 'class', 'default'): config_stack.pop(key, None) collapsed = CollapsedConfig(type_obj, self._render_config_stack( type_obj, config_stack), self, default=is_default, debug=self.debug) return collapsed
def test_builtin_full_override(self): # check our assumptions... # and yes, the signatures below are for file rather than # dict; we need a cpy class for the test, the ConfigHint doesn't # have to be accurate however class cls(dict): __slots__ = () self.assertRaises(TypeError, basics.ConfigType, cls) raw_hint = ConfigHint(types={ "filename": "str", "mode": "r", "buffering": "int" }, typename='file', required=['filename'], positional=['filename']) # make sure it still tries to introspect, and throws typeerror. # introspection is generally wanted- if it must be skipped, the # ConfigHint must make it explicit cls.pkgcore_config_type = raw_hint self.assertRaises(TypeError, basics.ConfigType, cls) cls.pkgcore_config_type = raw_hint.clone(authorative=True) conf = basics.ConfigType(cls) self.assertEqual(conf.name, 'file') self.assertEqual(list(conf.required), ['filename']) self.assertEqual(list(conf.positional), ['filename']) self.assertEqual(sorted(conf.types), ['buffering', 'filename', 'mode'])
def test_default_types(self): test_type = basics.ConfigType(alltypes) self.assertEqual( test_type.types, {'alist': 'list', 'astr': 'str', 'abool': 'bool', 'anint': 'int', 'along': 'int'}) self.assertEqual(test_type.required, ())
def test_config_hint(self): class Class(NewStyleClass): pkgcore_config_type = ConfigHint(types={'two': 'bool'}, doc='interesting') self._test_basics(Class, 'Class', two_override='bool') self.assertEqual('interesting', basics.ConfigType(Class).doc)
def describe_class_main(options, out, err): """Describe the arguments a class needs.""" try: type_obj = basics.ConfigType(options.target_class) except errors.TypeDefinitionError: err.write('Not a valid type!') return 1 write_type(out, type_obj)
def test_configurable(self): @configurable(typename='spork', types={'foon': 'str'}) def stuff(*args, **kwargs): return args, kwargs self.assertEqual('spork', stuff.pkgcore_config_type.typename) self.assertEqual('str', basics.ConfigType(stuff).types['foon']) self.assertEqual((('spork', ), {}), stuff('spork'))
def test_basic(self): nonopt_type = basics.ConfigType(nonopt) self.assertEqual(nonopt_type.name, 'nonopt') self.assertEqual( nonopt_type.types, {'one': 'str', 'two': 'str'}) self.assertEqual(nonopt_type.required, ('one', 'two')) self.assertEqual(nonopt_type.positional, ('one', 'two'))
def test_alias(self): def spoon(): """Noop.""" foon = central.CollapsedConfig(basics.ConfigType(spoon), {}, None) class MockManager: def collapse_named_section(self, name): if name == 'foon': return foon return object() manager = MockManager() alias = basics.section_alias('foon', 'spoon') type_obj = basics.ConfigType(alias.render_value(manager, 'class', 'callable')) self.assertEqual('spoon', type_obj.name) self.assertIdentical( foon, alias.render_value(manager, 'target', 'ref:spoon').collapse())
def _test_basics(self, klass, name, two_override=None): test_type = basics.ConfigType(klass) self.assertEqual(test_type.name, name) self.assertEqual(sorted(test_type.required), ['one']) target_types = {'one': 'str'} if two_override is not None: target_types['two'] = two_override self.assertEqual(target_types, test_type.types) self.assertEqual(test_type.name, name)
def test_section_refs(self): def spoon(): """Noop.""" config1 = central.CollapsedConfig(basics.ConfigType(spoon), {}, None) config2 = central.CollapsedConfig(basics.ConfigType(spoon), {}, None) class TestCentral(object): def collapse_named_section(self, section): try: return {'1': config1, '2': config2}[section] except KeyError: raise errors.ConfigurationError(section) self.assertEqual( list(ref.collapse() for ref in basics.convert_string( TestCentral(), '1 2', 'refs:spoon')), [config1, config2]) lazy_refs = basics.convert_string(TestCentral(), '2 3', 'refs:spoon') self.assertEqual(2, len(lazy_refs)) self.assertRaises(errors.ConfigurationError, lazy_refs[1].collapse)
def configurables_main(options, out, err): """List registered configurables.""" # try and sort this beast. def key_func(obj): return "%s.%s" % (getattr(obj, '__module__', ''), getattr(obj, '__name__', '')) for configurable in sorted(get_plugins('configurable'), key=key_func): type_obj = basics.ConfigType(configurable) if options.typename is not None and type_obj.name != options.typename: continue out.write(out.bold, f'{configurable.__module__}.{configurable.__name__}') write_type(out, type_obj) out.write() out.write()
def test_section_ref(self): def spoon(): """Noop.""" target_config = central.CollapsedConfig( basics.ConfigType(spoon), {}, None) class TestCentral: def collapse_named_section(self, section): try: return {'target': target_config}[section] except KeyError: raise errors.ConfigurationError(section) self.assertEqual( basics.convert_string( TestCentral(), 'target', 'ref:spoon').collapse(), target_config) self.assertRaises( errors.ConfigurationError, basics.convert_string( TestCentral(), 'missing', 'ref:spoon').instantiate)
def _test_class_member(self, func): test_type = basics.ConfigType(func) self.assertEqual(test_type.name, 'member') self.assertEqual(test_type.required, ('one', ))