def test_multiple(self): subclasses_of_b_or_c = SubclassesOf(self.B, self.C) self.assertEqual((self.B, self.C), subclasses_of_b_or_c.types) self.assertTrue(subclasses_of_b_or_c.satisfied_by(self.B())) self.assertTrue(subclasses_of_b_or_c.satisfied_by(self.C())) self.assertFalse(subclasses_of_b_or_c.satisfied_by(self.BPrime())) self.assertFalse(subclasses_of_b_or_c.satisfied_by(self.A()))
def test_single(self): subclasses_of_b = SubclassesOf(self.B) self.assertEqual((self.B,), subclasses_of_b.types) self.assertFalse(subclasses_of_b.satisfied_by(self.A())) self.assertTrue(subclasses_of_b.satisfied_by(self.B())) self.assertFalse(subclasses_of_b.satisfied_by(self.BPrime())) self.assertTrue(subclasses_of_b.satisfied_by(self.C()))
def test_mixed(self): self.assertEqual( dict(meaning_of_life=42, fine_structure_constant=Addressed(SubclassesOf((int, float)), '//:fsc')), addressable_mapping( SubclassesOf((int, float)), dict(meaning_of_life=42, fine_structure_constant='//:fsc')))
def __init__(self, name=None, configurations=None, dependencies=None, **kwargs): super(Target, self).__init__(name=name, configurations=addressables( SubclassesOf(Configuration), configurations), dependencies=addressables(SubclassesOf(Target), dependencies), **kwargs)
class Target(Struct, HasStructs): collection_field = 'configurations' def __init__(self, name=None, configurations=None, **kwargs): super(Target, self).__init__(name=name, **kwargs) self.configurations = configurations @addressable_list(SubclassesOf(Struct)) def configurations(self): pass
def test_single(self): subclasses_of_b = SubclassesOf(self.B) self.assertEqual((self.B, ), subclasses_of_b.types) self.assertFalse(subclasses_of_b.satisfied_by(self.A())) self.assertTrue(subclasses_of_b.satisfied_by(self.B())) self.assertFalse(subclasses_of_b.satisfied_by(self.BPrime())) self.assertTrue(subclasses_of_b.satisfied_by(self.C()))
class StructWithDeps(Struct): """A subclass of Struct with dependencies.""" def __init__(self, dependencies=None, **kwargs): """ :param list dependencies: The direct dependencies of this struct. """ # TODO: enforce the type of variants using the Addressable framework. super(StructWithDeps, self).__init__(**kwargs) self.dependencies = dependencies @addressable_list(SubclassesOf(Struct)) def dependencies(self): """The direct dependencies of this target.
class ThriftConfiguration(Configuration): def __init__(self, deps=None, **kwargs): """ :param deps: An optional list of dependencies needed by the generated code. :type deps: list of jars """ super(ThriftConfiguration, self).__init__(**kwargs) self.deps = deps # Could be Jars, PythonRequirements, ... we simply don't know a-priori - depends on --gen lang. @addressable_list(SubclassesOf(Configuration)) def deps(self): """Return a list of the dependencies needed by the generated code."""
class Target(Struct, HasStructs): """A placeholder for the most-numerous Struct subclass. This particular implementation holds a collection of other Structs in a `configurations` field. """ collection_field = 'configurations' def __init__(self, name=None, configurations=None, **kwargs): """ :param string name: The name of this target which forms its address in its namespace. :param list configurations: The configurations that apply to this target in various contexts. """ super(Target, self).__init__(name=name, **kwargs) self.configurations = configurations @addressable_list(SubclassesOf(Struct)) def configurations(self): """The configurations that apply to this target in various contexts.
class Target(Configuration): """TODO(John Sirois): XXX DOCME""" class ConfigurationNotFound(Exception): """Indicates a requested configuration of a target could not be found.""" def __init__(self, name=None, sources=None, configurations=None, dependencies=None, **kwargs): """ :param string name: The name of this target which forms its address in its namespace. :param sources: The relative source file paths of sources this target owns. :type sources: :class:`Sources` :param list configurations: The configurations that apply to this target in various contexts. :param list dependencies: The direct dependencies of this target. """ super(Target, self).__init__(name=name, **kwargs) self.configurations = configurations self.dependencies = dependencies self.sources = sources @addressable_list(SubclassesOf(Configuration)) def configurations(self): """The configurations that apply to this target in various contexts. :rtype list of :class:`pants.engine.exp.configuration.Configuration` """ @addressable_list(SubclassesOf(Configuration)) def dependencies(self): """The direct dependencies of this target. :rtype: list """ @addressable(Exactly(Sources)) def sources(self): """Return the sources this target owns. :rtype: :class:`Sources` """ def select_configuration(self, name): """Selects a named configuration of this target. :param string name: The name of the configuration to select. :returns: The configuration with the given name. :rtype: :class:`pants.engine.exp.configuration.Configuration` :raises: :class:`Target.ConfigurationNotFound` if the configuration was not found. """ configs = tuple(config for config in self.configurations if config.name == name) if len(configs) != 1: configurations = ('{} -> {!r}'.format( repr(c.name) if c.name else '<anonymous>', c) for c in configs) raise self.ConfigurationNotFound( 'Failed to find a single configuration named {!r} these ' 'configurations in {!r}:\n\t{}'.format( name, self, '\n\t'.join(configurations))) return configs[0] def walk_targets(self, postorder=True): """Performs a depth first walk of this target, visiting all reachable targets exactly once. :param bool postorder: When ``True``, the traversal order is postorder (children before parents), else it is preorder (parents before children). """ visited = set() def walk(target): if target not in visited: visited.add(target) if not postorder: yield target for dep in self.dependencies: if isinstance(dep, Target): for t in walk(dep): yield t if postorder: yield target for target in walk(self): yield target
def test_none(self): with self.assertRaises(ValueError): SubclassesOf()
class Target(Struct): """TODO(John Sirois): XXX DOCME""" class ConfigurationNotFound(Exception): """Indicates a requested configuration of a target could not be found.""" def __init__(self, name=None, configurations=None, **kwargs): """ :param string name: The name of this target which forms its address in its namespace. :param list configurations: The configurations that apply to this target in various contexts. """ super(Target, self).__init__(name=name, **kwargs) self.configurations = configurations @addressable_list(SubclassesOf(Struct)) def configurations(self): """The configurations that apply to this target in various contexts. :rtype list of :class:`pants.engine.exp.configuration.Struct` """ def select_configuration(self, name): """Selects a named configuration of this target. :param string name: The name of the configuration to select. :returns: The configuration with the given name. :rtype: :class:`pants.engine.exp.configuration.Struct` :raises: :class:`Target.ConfigurationNotFound` if the configuration was not found. """ configs = tuple(config for config in self.configurations if config.name == name) if len(configs) != 1: configurations = ('{} -> {!r}'.format( repr(c.name) if c.name else '<anonymous>', c) for c in configs) raise self.ConfigurationNotFound( 'Failed to find a single configuration named {!r} for these ' 'configurations in {!r}:\n\t{}'.format( name, self, '\n\t'.join(configurations))) return configs[0] def select_configuration_type(self, tpe): """Selects configurations of the given type on this target. :param type tpe: The exact type of the configuration to select: subclasses will not match. :returns: The configurations with the given type. :rtype: :class:`pants.engine.exp.configuration.Configuration` """ return tuple(config for config in self.configurations if type(config) == tpe) def walk_targets(self, postorder=True): """Performs a depth first walk of this target, visiting all reachable targets exactly once. TODO: Walking a Target graph probably doesn't make sense; but walking an ExecutionGraph does. :param bool postorder: When ``True``, the traversal order is postorder (children before parents), else it is preorder (parents before children). """ visited = set() def walk(target): if target not in visited: visited.add(target) if not postorder: yield target for configuration in self.configurations: for dep in configuration.dependencies: if isinstance(dep, Target): for t in walk(dep): yield t if postorder: yield target for target in walk(self): yield target
def test_values(self): self.assertEqual( dict(meaning_of_life=42, fine_structure_constant=1 / 137.0), addressable_mapping( SubclassesOf((int, float)), dict(meaning_of_life=42, fine_structure_constant=1 / 137.0)))
def test_values(self): self.assertEqual([42, 1 / 137.0], addressables(SubclassesOf((int, float)), (42, 1 / 137.0)))
def test(self): self.assertFalse(SubclassesOf(self.B).satisfied_by(self.A())) self.assertTrue(SubclassesOf(self.B).satisfied_by(self.B())) self.assertFalse(SubclassesOf(self.B).satisfied_by(self.BPrime())) self.assertTrue(SubclassesOf(self.B).satisfied_by(self.C()))