Skip to content

Commit

Permalink
Merge pull request #364 from buddly27/explicit_targets_not_registered…
Browse files Browse the repository at this point in the history
…_globally

Explicit targets not registered globally
  • Loading branch information
mottosso authored Mar 21, 2020
2 parents acf97ef + 08e6373 commit 010c630
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 21 deletions.
10 changes: 6 additions & 4 deletions pyblish/logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ def _extract_traceback(exception):
del(exc_type, exc_value, exc_traceback)


def Iterator(plugins, context, state=None):
def Iterator(plugins, context, state=None, targets=None):
"""Primary iterator
This is the brains of publishing. It handles logic related
Expand All @@ -345,6 +345,7 @@ def Iterator(plugins, context, state=None):
plugins (list): Plug-ins to consider
context (list): Instances to consider
state (dict): Mutable state
targets (list, optional): Targets to include for publish session.
"""

Expand All @@ -354,9 +355,10 @@ def Iterator(plugins, context, state=None):
"ordersWithError": set()
}

# We'll add "default" target if no targets are registered. This happens
# when running the Iterator directly without registering any targets.
targets = registered_targets() or ["default"]
# Include "default" target and registered targets when no targets are
# explicitly requested.
if not targets:
targets = ["default"] + registered_targets()

plugins = plugins_by_targets(plugins, targets)

Expand Down
22 changes: 8 additions & 14 deletions pyblish/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,6 @@ def publish_iter(context=None, plugins=None, targets=None):


def _convenience_iter(context=None, plugins=None, targets=None, order=None):
# Include "default" target when no targets are requested.
targets = targets or ["default"]

# Must check against None, as objects be emptys
context = api.Context() if context is None else context
plugins = api.discover() if plugins is None else plugins
Expand All @@ -107,10 +104,6 @@ def _convenience_iter(context=None, plugins=None, targets=None, order=None):
if lib.inrange(Plugin.order, order)
)

# Register targets
for target in targets:
api.register_target(target)

# Do not consider inactive plug-ins
plugins = list(p for p in plugins if p.active)
collectors = list(p for p in plugins if lib.inrange(
Expand All @@ -123,11 +116,13 @@ def _convenience_iter(context=None, plugins=None, targets=None, order=None):
# dynamically determined at run-time by contents of
# the context and families of contained instances;
# each of which may differ between task.
task_count = len(list(logic.Iterator(plugins, context)))
task_count = len(list(logic.Iterator(plugins, context, targets=targets)))

# First pass, collection
tasks_processed_count = 1
for Plugin, instance in logic.Iterator(collectors, context):
for Plugin, instance in logic.Iterator(collectors,
context,
targets=targets):
result = plugin.process(Plugin, context, instance)

# Inject additional member for results here.
Expand All @@ -153,7 +148,10 @@ def _convenience_iter(context=None, plugins=None, targets=None, order=None):
}

# Second pass, the remainder
for Plugin, instance in logic.Iterator(plugins, context, state):
for Plugin, instance in logic.Iterator(plugins,
context,
state,
targets=targets):
try:
result = plugin.process(Plugin, context, instance)
result["progress"] = (
Expand Down Expand Up @@ -184,10 +182,6 @@ def _convenience_iter(context=None, plugins=None, targets=None, order=None):

yield result

# Deregister targets
for target in targets:
api.deregister_target(target)


def collect(context=None, plugins=None, targets=None):
"""Convenience function for collection-only
Expand Down
2 changes: 1 addition & 1 deletion pyblish/version.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

VERSION_MAJOR = 1
VERSION_MINOR = 8
VERSION_PATCH = 4
VERSION_PATCH = 5

version_info = (VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH)
version = '%i.%i.%i' % version_info
Expand Down
41 changes: 41 additions & 0 deletions tests/test_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,47 @@ def process(self, instance):
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


def test_register_gui():
"""Registering at run-time takes precedence over those from environment"""

Expand Down
34 changes: 32 additions & 2 deletions tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,8 @@ def process(self, context):


@with_setup(lib.setup, lib.teardown)
def test_publishing_targets():
"""Publishing with targets works"""
def test_publishing_explicit_targets():
"""Publishing with explicit targets works"""

count = {"#": 0}

Expand All @@ -254,6 +254,36 @@ def process(self, context):
assert count["#"] == 1, count


def test_publishing_explicit_targets_with_global():
"""Publishing with explicit and globally registered targets works"""

count = {"#": 0}

class Plugin1(api.ContextPlugin):
targets = ["custom"]

def process(self, context):
count["#"] += 1

class Plugin2(api.ContextPlugin):
targets = ["foo"]

def process(self, context):
count["#"] += 10

api.register_target("foo")
api.register_target("custom")
api.register_plugin(Plugin1)
api.register_plugin(Plugin2)

util.publish(targets=["custom"])

assert count["#"] == 1, count
assert api.registered_targets() == ["foo", "custom"]

api.deregister_all_targets()


@with_setup(lib.setup, lib.teardown)
def test_per_session_targets():
"""Register targets per session works"""
Expand Down

0 comments on commit 010c630

Please sign in to comment.