Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature - Added callback pluginProcessed #292

Merged
merged 6 commits into from
Aug 18, 2016
Merged
3 changes: 2 additions & 1 deletion CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ danielottosson2@gmail.com
https://github.com/davidmartinezanim
https://github.com/tokejepsen
https://github.com/bigroy
https://github.com/mkolar
https://github.com/mkolar
https://github.com/pscadding
7 changes: 5 additions & 2 deletions pyblish/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,9 +440,12 @@ def process(plugin, context, instance=None, action=None):
"""

if issubclass(plugin, (ContextPlugin, InstancePlugin)):
return __explicit_process(plugin, context, instance, action)
result = __explicit_process(plugin, context, instance, action)
else:
return __implicit_process(plugin, context, instance, action)
result = __implicit_process(plugin, context, instance, action)

lib.emit("pluginProcessed", result=result)
return result


def __explicit_process(plugin, context, instance=None, action=None):
Expand Down
77 changes: 77 additions & 0 deletions tests/test_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,80 @@ def on_validated(context):
pyblish.util.validate()

assert count["#"] == 1, count

@with_setup(lib.setup_empty)
def test_plugin_processed_event():
"""pluginProcessed is emitted upon a plugin being processed, regardless of its success"""

class MyContextCollector(pyblish.api.ContextPlugin):
order = pyblish.api.CollectorOrder

def process(self, context):
context.create_instance("A")

class CheckInstancePass(pyblish.api.InstancePlugin):
order = pyblish.api.ValidatorOrder

def process(self, instance):
pass

class CheckInstanceFail(pyblish.api.InstancePlugin):
order = pyblish.api.ValidatorOrder

def process(self, instance):
raise Exception("Test Fail")

pyblish.api.register_plugin(MyContextCollector)
pyblish.api.register_plugin(CheckInstancePass)
pyblish.api.register_plugin(CheckInstanceFail)


count = {"#": 0}

def on_processed(result):
assert isinstance(result, dict)
count["#"] += 1

pyblish.api.register_callback("pluginProcessed", on_processed)
pyblish.util.publish()

assert count["#"] == 3, count

@with_setup(lib.setup_empty)
def test_plugin_failed_event():
"""pluginFailed is emitted upon a plugin failing for any reason"""

class MyContextCollector(pyblish.api.ContextPlugin):
order = pyblish.api.CollectorOrder
def process(self, context):
context.create_instance("A")

class CheckInstancePass(pyblish.api.InstancePlugin):
order = pyblish.api.ValidatorOrder
def process(self, instance):
pass

class CheckInstanceFail(pyblish.api.InstancePlugin):
order = pyblish.api.ValidatorOrder
def process(self, instance):
raise Exception("Test Fail")

pyblish.api.register_plugin(MyContextCollector)
pyblish.api.register_plugin(CheckInstancePass)
pyblish.api.register_plugin(CheckInstanceFail)

count = {"#": 0}

def on_failed(plugin, context, instance, error):
#todo: add further checks for the other incoming args
#assert isinstance(instance, CheckInstanceFailRaise)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, yes I see you mean these comment-out ones. How come they didn't get included?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't get the isinstance to work with the plugin and instance.
I tried isinstance(instance, pyblish.api.InstancePlugin) but that didn't work and I also tried comparing to directly to the class defined in the test, but that didn't seem to work.
It needs a bit more looking into.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, yes the plug-in you're getting is probably not an instance, but the class itself. Try something like issubclass(plugin, pyblish.api.InstancePlugin) or simply plugin == CheckInstanceFail.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, and with isinstance(instance, pyblish.api.InstancePlugin), the instance isn't the plug-in, but the instance you create via context.create_instance. You can compare it with pyblish.api.Instance.

#assert isinstance(plugin, pyblish.api.InstancePlugin)
assert isinstance(context, pyblish.api.Context)
assert isinstance(error, Exception)

count["#"] += 1

pyblish.api.register_callback("pluginFailed", on_failed)
pyblish.util.publish()

assert count["#"] == 1, count