def test_subset_exact(): """Plugin.match = api.Exact works as expected""" count = {"#": 0} class MyPlugin(api.InstancePlugin): families = ["a", "b"] match = api.Exact def process(self, instance): count["#"] += 1 context = api.Context() context.create_instance("not_included_1", families=["a"]) context.create_instance("not_included_1", families=["x"]) context.create_instance("not_included_3", families=["a", "b", "c"]) instance = context.create_instance("included_1", families=["a", "b"]) # Discard the solo-family member, which defaults to `default`. # # When using multiple families, it is common not to bother modifying `family`, # and in the future this member needn't be there at all and may/should be # removed. But till then, for complete clarity, it might be worth removing # this explicitly during the creation of instances if instead choosing to # use the `families` key. instance.data.pop("family") util.publish(context, plugins=[MyPlugin]) assert_equals(count["#"], 1) instances = logic.instances_by_plugin(context, MyPlugin) assert_equals(list(i.name for i in instances), ["included_1"])
def test_subset_match(): """Plugin.match = api.Subset works as expected""" count = {"#": 0} class MyPlugin(api.InstancePlugin): families = ["a", "b"] match = api.Subset def process(self, instance): count["#"] += 1 context = api.Context() context.create_instance("not_included_1", families=["a"]) context.create_instance("not_included_1", families=["x"]) context.create_instance("included_1", families=["a", "b"]) context.create_instance("included_2", families=["a", "b", "c"]) util.publish(context, plugins=[MyPlugin]) assert_equals(count["#"], 2) instances = logic.instances_by_plugin(context, MyPlugin) assert_equals(list(i.name for i in instances), ["included_1", "included_2"])
def test_environment_host_registration(): """Host registration from PYBLISH_HOSTS works""" count = {"#": 0} hosts = ["test1", "test2"] # Test single hosts class SingleHostCollector(api.ContextPlugin): order = api.CollectorOrder host = hosts[0] def process(self, context): count["#"] += 1 api.register_plugin(SingleHostCollector) context = api.Context() os.environ["PYBLISH_HOSTS"] = "test1" util.collect(context) assert count["#"] == 1, count # Test multiple hosts api.deregister_all_plugins() class MultipleHostsCollector(api.ContextPlugin): order = api.CollectorOrder host = hosts def process(self, context): count["#"] += 10 api.register_plugin(MultipleHostsCollector) context = api.Context() os.environ["PYBLISH_HOSTS"] = os.pathsep.join(hosts) util.collect(context) assert count["#"] == 11, count
def test_modify_context_during_CVEI(): """Custom logic made possible via convenience members""" count = {"#": 0} class MyCollector(api.ContextPlugin): order = api.CollectorOrder def process(self, context): camera = context.create_instance("MyCamera") model = context.create_instance("MyModel") camera.data["family"] = "camera" model.data["family"] = "model" count["#"] += 1 class MyValidator(api.InstancePlugin): order = api.ValidatorOrder def process(self, instance): count["#"] += 10 api.register_plugin(MyCollector) api.register_plugin(MyValidator) context = api.Context() assert count["#"] == 0, count util.collect(context) assert count["#"] == 1, count context[:] = filter(lambda i: i.data["family"] == "camera", context) util.validate(context) # Only model remains assert count["#"] == 11, count # No further processing occurs. util.extract(context) util.integrate(context) assert count["#"] == 11, count
def test_extracted_traceback_contains_correct_backtrace(): api.register_plugin_path(os.path.dirname(__file__)) context = api.Context() context.create_instance('test instance') plugins = api.discover() plugins = [p for p in plugins if p.__name__ in ('FailingExplicitPlugin', 'FailingImplicitPlugin')] util.publish(context, plugins) for result in context.data['results']: assert result["error"].traceback[0] == plugins[0].__module__ formatted_tb = result['error'].formatted_traceback assert formatted_tb.startswith('Traceback (most recent call last):\n') assert formatted_tb.endswith('\nException: A test exception\n') assert 'File "{0}",'.format(plugins[0].__module__) in formatted_tb
def test_iterator(): """Iterator skips inactive plug-ins and instances""" count = {"#": 0} class MyCollector(api.ContextPlugin): order = api.CollectorOrder def process(self, context): inactive = context.create_instance("Inactive") active = context.create_instance("Active") inactive.data["publish"] = False active.data["publish"] = True count["#"] += 1 class MyValidatorA(api.InstancePlugin): order = api.ValidatorOrder active = False def process(self, instance): count["#"] += 10 class MyValidatorB(api.InstancePlugin): order = api.ValidatorOrder def process(self, instance): count["#"] += 100 context = api.Context() plugins = [MyCollector, MyValidatorA, MyValidatorB] assert count["#"] == 0, count for Plugin, instance in logic.Iterator(plugins, context): assert instance.name != "Inactive" if instance else True assert Plugin.__name__ != "MyValidatorA" plugin.process(Plugin, context, instance) # Collector runs once, one Validator runs once assert count["#"] == 101, count
def test_iterator_with_explicit_targets(): """Iterator skips non-targeted plug-ins""" count = {"#": 0} class MyCollectorA(api.ContextPlugin): order = api.CollectorOrder targets = ["studio"] def process(self, context): count["#"] += 1 class MyCollectorB(api.ContextPlugin): order = api.CollectorOrder def process(self, context): count["#"] += 10 class MyCollectorC(api.ContextPlugin): order = api.CollectorOrder targets = ["studio"] def process(self, context): count["#"] += 100 context = api.Context() plugins = [MyCollectorA, MyCollectorB, MyCollectorC] assert count["#"] == 0, count for Plugin, instance in logic.Iterator( plugins, context, targets=["studio"] ): assert Plugin.__name__ != "MyCollectorB" plugin.process(Plugin, context, instance) # Collector runs once, one Validator runs once assert count["#"] == 101, count