def test_single(self): exactly_b = Exactly(self.B) self.assertEqual((self.B,), exactly_b.types) self.assertFalse(exactly_b.satisfied_by(self.A())) self.assertTrue(exactly_b.satisfied_by(self.B())) self.assertFalse(exactly_b.satisfied_by(self.BPrime())) self.assertFalse(exactly_b.satisfied_by(self.C()))
def test_multiple(self): exactly_a_or_b = Exactly(self.A, self.B) self.assertEqual((self.A, self.B), exactly_a_or_b.types) self.assertTrue(exactly_a_or_b.satisfied_by(self.A())) self.assertTrue(exactly_a_or_b.satisfied_by(self.B())) self.assertFalse(exactly_a_or_b.satisfied_by(self.BPrime())) self.assertFalse(exactly_a_or_b.satisfied_by(self.C()))
def __init__(self, default_repo, repos, name=None, **kwargs): super(PublishConfiguration, self).__init__(name=name, default_repo=addressable(Exactly(Configuration), default_repo), repos=addressable_mapping(Exactly(Configuration), repos), **kwargs)
class PublishConfiguration(Struct): # An example of addressable and addressable_mapping field wrappers. def __init__(self, default_repo, repos, name=None, **kwargs): super(PublishConfiguration, self).__init__(name=name, **kwargs) self.default_repo = default_repo self.repos = repos @addressable(Exactly(Struct)) def default_repo(self): """""" @addressable_dict(Exactly(Struct)) def repos(self): """"""
class Person(SimpleSerializable): def __init__(self, age): super(AddressableTest.Person, self).__init__() self.age = age @addressable(Exactly(int)) def age(self): """Return the person's age in years.
class Series(SimpleSerializable): def __init__(self, values): super(AddressableListTest.Series, self).__init__() self.values = values @addressable_list(Exactly(int, float)) def values(self): """Return this series' values.
class Varz(SimpleSerializable): def __init__(self, varz): super(AddressableDictTest.Varz, self).__init__() self.varz = varz @addressable_dict(Exactly(int, float)) def varz(self): """Return a snapshot of the current /varz.
class NotSerializable(object): def __init__(self, count): super(NotSerializable, self).__init__() self.count = count @addressable(Exactly(int)) def count(self): pass
def test_single(self): exactly_b = Exactly(self.B) self.assertEqual((self.B, ), exactly_b.types) self.assertFalse(exactly_b.satisfied_by(self.A())) self.assertTrue(exactly_b.satisfied_by(self.B())) self.assertFalse(exactly_b.satisfied_by(self.BPrime())) self.assertFalse(exactly_b.satisfied_by(self.C()))
def test(self): self.assertFalse(Exactly(self.B).satisfied_by(self.A())) self.assertTrue(Exactly(self.B).satisfied_by(self.B())) self.assertFalse(Exactly(self.B).satisfied_by(self.BPrime())) self.assertFalse(Exactly(self.B).satisfied_by(self.C()))
def test_none(self): self.assertIsNone(addressable(Exactly(int), None))
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_value(self): self.assertEqual(42, addressable(Exactly(int), 42))
def test_none(self): with self.assertRaises(ValueError): Exactly()
def test_type_mismatch(self): with self.assertRaises(AddressedError): addressable(Exactly(int), 42.0)
@property def path_globs(self): """Creates a `PathGlobs` object for the files held by these Sources. This field may be projected to request the content of the files for this Sources object. """ return PathGlobs.create(self.spec_path, files=self.files, globs=self.globs, rglobs=self.rglobs, zglobs=self.zglobs) @abstractproperty def extensions(self): """A collection of file extensions collected by this Sources instance. An empty collection indicates that any extension will be accepted. """ @property def excludes(self): """The sources to exclude. :rtype: :class:`Sources` """ # Since Sources.excludes is recursive on the Sources type, we need to post-class-definition # re-define excludes in this way. Sources.excludes = addressable(Exactly(Sources))(Sources.excludes)
def test_type_mismatch(self): with self.assertRaises(AddressedError): addressable_mapping( Exactly(int), dict(meaning_of_life=42, fine_structure_constant=1 / 137.0))
def test_none(self): self.assertEqual({}, addressable_mapping(Exactly(int), None))
def test_type_mismatch(self): with self.assertRaises(AddressedError): addressables(Exactly(int), [42, 1 / 137.0])
def test_mixed(self): self.assertEqual( [42, Addressed(Exactly(int), '//:meaning-of-life')], addressables(Exactly(int), [42, '//:meaning-of-life']))
def test_pointers(self): self.assertEqual([Addressed(Exactly(int), '//:meaning-of-life')], addressables(Exactly(int), ['//:meaning-of-life']))
def test_none(self): self.assertEqual([], addressables(Exactly(int), None))
def test_pointers(self): self.assertEqual( dict( meaning_of_life=Addressed(Exactly(int), '//:meaning-of-life')), addressable_mapping(Exactly(int), dict(meaning_of_life='//:meaning-of-life')))
def test_pointer(self): self.assertEqual(Addressed(Exactly(int), '//:meaning-of-life'), addressable(Exactly(int), '//:meaning-of-life'))