Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Pyblish in 100 lines
import types
# Mocked Pyblish module
pyblish = types.ModuleType('pyblish')
pyblish.api = types.ModuleType('api')
pyblish.api.CollectorOrder = 0
pyblish.api.ValidatorOrder = 1
pyblish.plugin = types.ModuleType('plugin')
pyblish.logic = types.ModuleType('logic')
# Ignore this class
class _Plugin(object):
def __init__(self):
self._records = list()
self.log = type('Logger', (object,), {})()
self.log.info = lambda text: self._records.append(text + '\n')
# In place of Plugin and CVEI superclasses, two distinct types are defined.
# Each has a unique, explicit behaviour, as opposed to the implicit behavior
# present in the current CVEI types.
class ContextPlugin(_Plugin):
pass
class InstancePlugin(_Plugin):
pass
# Example plugins
class CollectInstances(ContextPlugin):
# The order is equally explicit and assigned
# either via static, named numbers, or as usual
# via arbitrary numbers. Sorting remains unchanged
order = pyblish.api.CollectorOrder
def process(self, context):
context.append('instance1')
context.append('instance2')
self.log.info('Processing context..')
class ValidateInstances(InstancePlugin):
order = pyblish.api.ValidatorOrder
def process(self, instance):
self.log.info('Processing %s' % instance)
plugins = [ValidateInstances, CollectInstances]
plugins = sorted(plugins, key=lambda item: item.order)
# plugin.process is greatly simplified.
# Note the disappearance of the provider.
def process(plugin, **kwargs):
print('individually processing %s' % kwargs.values()[0])
result = {
'plugin': plugin,
'item': None,
'error': None,
'records': list(),
'success': False
}
try:
plugin = plugin()
plugin.process(**kwargs)
except Exception as e:
result['success'] = False
result['error'] = e
result['records'] = plugin._records
return result
pyblish.plugin.process = process
# logic.process is greatly simplified
def process(plugins, context):
print('logic.process running..')
for plugin in plugins:
print('Processing %s' % plugin)
# Run once
if issubclass(plugin, ContextPlugin):
yield pyblish.plugin.process(plugin, context=context)
# Run once per instance
if issubclass(plugin, InstancePlugin):
for instance in context:
yield pyblish.plugin.process(plugin, instance=instance)
pyblish.logic.process = process
# Example usage
context = list()
processor = pyblish.logic.process(plugins, context)
results = list(processor)
print(results)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.