def _resolve(self, arg, assembler): """Return the resolved *arg*. :param arg: represents an argument (positional or keyword) to :attr:`factory` :param aglyph.assembly.Assembler assembler: the assembler that will be used to resolve *arg* :return: the resolved argument value that will actually be passed to :attr:`factory` """ if isinstance(arg, Reference): return assembler.assemble(arg) elif isinstance(arg, Evaluator): return arg(assembler) elif isinstance(arg, partial): return arg() elif isinstance(arg, dict): # either keys or values may themselves be References, partials, or # Evaluators resolve = self._resolve return dict( [(resolve(key, assembler), resolve(value, assembler)) for (key, value) in arg.items()]) elif hasattr(arg, "__iter__") and not is_string(arg): resolve = self._resolve # assumption: the iterable class supports initialization with # __init__(iterable) return arg.__class__([resolve(value, assembler) for value in arg]) else: return arg
def _identify(spec): """Determine the unique ID for *spec*. :arg spec: an **importable** class, function, or module; or a :obj:`str` :return: *spec* unchanged (if it is a :obj:`str`), else *spec*'s importable dotted name :rtype: :obj:`str` If *spec* is a string, it is assumed to already represent a unique ID and is returned unchanged. Otherwise, *spec* is assumed to be an **importable** class, function, or module, and its dotted name is returned (see :func:`format_dotted_name`). """ return spec if is_string(spec) else format_dotted_name(spec)
def _identify(spec): """Determine the unique ID for *spec*. :arg spec: an **importable** class, function, or module; or a :obj:`str` :return: *spec* unchanged (if it is a :obj:`str`), else *spec*'s importable dotted name :rtype: :obj:`str` If *spec* is a string, it is assumed to already represent a unique ID and is returned unchanged. Otherwise, *spec* is assumed to be an **importable** class, function, or module, and its dotted name is returned (see :func:`format_dotted_name`). """ return spec if is_string(spec) else format_dotted_name(spec)
def test_none_is_not_string(self): self.assertFalse(is_string(None))
def test_literal_string_is_string_type(self): # this is redundant w/r/t one of the previous two tests, depending on # the runtime Python version self.assertTrue(is_string(""))
def test_data_type_is_string_type(self): self.assertTrue(is_string(DataType()))
def test_user_class(self): ref = Reference(dummy.ModuleClass) self.assertTrue(isinstance(ref, Reference)) self.assertTrue(is_string(ref)) self.assertEqual("test.dummy.ModuleClass", ref)
def test_builtin_function(self): ref = Reference(hex) self.assertTrue(isinstance(ref, Reference)) self.assertTrue(is_string(ref)) self.assertEqual("%s.hex" % hex.__module__, ref)
def test_user_function(self): ref = Reference(dummy.factory_function) self.assertTrue(isinstance(ref, Reference)) self.assertTrue(is_string(ref)) self.assertEqual("test.dummy.factory_function", ref)
def test_none_is_not_string(self): self.assertFalse(is_string(None))
def test_data_type_is_string_type(self): self.assertTrue(is_string(DataType()))
def test_literal_string_is_string_type(self): # this is redundant w/r/t one of the previous two tests, depending on # the runtime Python version self.assertTrue(is_string(""))
def test_text_type_is_string_type(self): self.assertTrue(is_string(TextType()))
def test_module(self): ref = Reference(dummy) self.assertTrue(isinstance(ref, Reference)) self.assertTrue(is_string(ref)) self.assertEqual("test.dummy", ref)
def test_builtin_function(self): ref = Reference(hex) self.assertTrue(isinstance(ref, Reference)) self.assertTrue(is_string(ref)) self.assertEqual("%s.hex" % hex.__module__, ref)
def test_number_is_not_string(self): self.assertFalse(is_string(79))
def test_string(self): ref = Reference("test-component-id") self.assertTrue(isinstance(ref, Reference)) self.assertTrue(is_string(ref)) self.assertEqual("test-component-id", ref)
def test_reference_is_string(self): self.assertTrue(is_string(Reference("test")))
def test_builtin_class(self): ref = Reference(float) self.assertTrue(isinstance(ref, Reference)) self.assertTrue(is_string(ref)) self.assertEqual("%s.float" % float.__module__, ref)
def test_number_is_not_string(self): self.assertFalse(is_string(79))
def test_builtin_class(self): ref = Reference(float) self.assertTrue(isinstance(ref, Reference)) self.assertTrue(is_string(ref)) self.assertEqual("%s.float" % float.__module__, ref)
def test_user_class(self): ref = Reference(dummy.ModuleClass) self.assertTrue(isinstance(ref, Reference)) self.assertTrue(is_string(ref)) self.assertEqual("test.dummy.ModuleClass", ref)
def test_reference_is_string(self): self.assertTrue(is_string(Reference("test")))
def test_user_function(self): ref = Reference(dummy.factory_function) self.assertTrue(isinstance(ref, Reference)) self.assertTrue(is_string(ref)) self.assertEqual("test.dummy.factory_function", ref)
def test_text_type_is_string_type(self): self.assertTrue(is_string(TextType()))
def test_module(self): ref = Reference(dummy) self.assertTrue(isinstance(ref, Reference)) self.assertTrue(is_string(ref)) self.assertEqual("test.dummy", ref)
def test_string(self): ref = Reference("test-component-id") self.assertTrue(isinstance(ref, Reference)) self.assertTrue(is_string(ref)) self.assertEqual("test-component-id", ref)