Permalink
Browse files

Refactoring legacy functions into its own module

  • Loading branch information...
mottosso committed May 28, 2015
1 parent b931e1e commit 68ded825ea07b6de3bd5a791628815a9394d6156
Showing with 306 additions and 317 deletions.
  1. +123 −0 pyblish/legacy.py
  2. +23 −0 pyblish/lib.py
  3. +128 −0 pyblish/plugin.py
  4. +13 −299 pyblish/util.py
  5. +7 −7 tests/pre11/test_logic.py
  6. +6 −6 tests/test_di.py
  7. +6 −5 tests/test_logic.py
@@ -0,0 +1,123 @@
"""Backwards compatibility module"""

import logging

import pyblish.lib


def _process_legacy(plugin, context, instance=None):
import time

if "results" not in context.data():
context.set_data("results", list())

if not hasattr(plugin, "process_context"):
plugin.process_context = lambda self, context: None

if not hasattr(plugin, "process_instance"):
plugin.process_instance = lambda self, instance: None

result = {
"success": False,
"plugin": plugin,
"instance": instance,
"error": None,
"records": list(),
"duration": None
}

records = list()
handler = pyblish.lib.MessageHandler(records)

root_logger = logging.getLogger()
root_logger.addHandler(handler)

__start = time.time()

try:
plugin().process_context(context)
result["success"] = True
except Exception as exc:
pyblish.lib.extract_traceback(exc)
result["error"] = exc

if instance is not None:
try:
plugin().process_instance(instance)
result["success"] = True
except Exception as exc:
pyblish.lib.extract_traceback(exc)
result["error"] = exc

__end = time.time()

for record in records:
result["records"].append(record)

# Restore balance to the world
root_logger.removeHandler(handler)

result["duration"] = (__end - __start) * 1000 # ms

context.data("results").append(result)

return result


def _repair_legacy(plugin, context, instance=None):
import time

if "results" not in context.data():
context.set_data("results", list())

if not hasattr(plugin, "repair_context"):
plugin.repair_context = lambda self, context: None

if not hasattr(plugin, "repair_instance"):
plugin.repair_instance = lambda self, instance: None

result = {
"success": False,
"plugin": plugin,
"instance": instance,
"error": None,
"records": list(),
"duration": None
}

records = list()
handler = pyblish.lib.MessageHandler(records)

root_logger = logging.getLogger()
root_logger.addHandler(handler)

__start = time.time()

try:
plugin().repair_context(context)
result["success"] = True
except Exception as exc:
pyblish.lib.extract_traceback(exc)
result["error"] = exc

if instance is not None:
try:
plugin().repair_instance(instance)
result["success"] = True
except Exception as exc:
pyblish.lib.extract_traceback(exc)
result["error"] = exc

__end = time.time()

for record in records:
result["records"].append(record)

# Restore balance to the world
root_logger.removeHandler(handler)

result["duration"] = (__end - __start) * 1000 # ms

context.data("results").append(result)

return result
@@ -2,12 +2,35 @@
import re
import sys
import logging
import traceback

_filename_ascii_strip_re = re.compile(r'[^-\w.]')
_windows_device_files = ('CON', 'AUX', 'COM1', 'COM2', 'COM3', 'COM4',
'LPT1', 'LPT2', 'LPT3', 'PRN', 'NUL')


class MessageHandler(logging.Handler):
def __init__(self, records, *args, **kwargs):
# Not using super(), for compatibility with Python 2.6
logging.Handler.__init__(self, *args, **kwargs)
self.records = records

def emit(self, record):
self.records.append(record)


def extract_traceback(exception):
try:
exc_type, exc_value, exc_traceback = sys.exc_info()
exception.traceback = traceback.extract_tb(exc_traceback)[-1]

except:
pass

finally:
del(exc_type, exc_value, exc_traceback)


class ItemList(list):
"""List with keys
@@ -24,6 +24,7 @@
import pyblish
import pyblish.lib
import pyblish.error
import pyblish.legacy

from .vendor import yaml
from .vendor import iscompatible
@@ -231,6 +232,133 @@ class Conformer(Plugin):
Integrator = Conformer


def process(plugin, context, instance=None):
"""Determine whether the given plug-in to be dependency injected"""
if (hasattr(plugin, "process_instance")
or hasattr(plugin, "process_context")):
return pyblish.legacy._process_legacy(plugin, context, instance)
else:
return _process(plugin, context, instance)


def repair(plugin, context, instance=None):
"""Determine whether the given plug-in to be dependency injected"""
if (hasattr(plugin, "repair_instance")
or hasattr(plugin, "repair_context")):
return pyblish.legacy._repair_legacy(plugin, context, instance)
else:
return _repair(plugin, context, instance)


def _process(plugin, context, instance=None):
"""Produce a single result from a Plug-in
Returns:
Result dictionary
"""

import time

if "results" not in context.data():
context.set_data("results", list())

result = {
"success": False,
"plugin": plugin,
"instance": instance,
"error": None,
"records": list(),
"duration": None
}

plugin = plugin()

provider = pyblish.plugin.Provider()
provider.inject("context", context)
provider.inject("instance", instance)

records = list()
handler = pyblish.lib.MessageHandler(records)

root_logger = logging.getLogger()
root_logger.addHandler(handler)

__start = time.time()

try:
provider.invoke(plugin.process)
result["success"] = True
except Exception as error:
pyblish.lib.extract_traceback(error)
result["error"] = error

__end = time.time()

for record in records:
result["records"].append(record)

# Restore balance to the world
root_logger.removeHandler(handler)

result["duration"] = (__end - __start) * 1000 # ms

context.data("results").append(result)

return result


def _repair(plugin, context, instance=None):
import time

if "results" not in context.data():
context.set_data("results", list())

result = {
"success": False,
"plugin": plugin,
"instance": instance,
"error": None,
"records": list(),
"duration": None
}

plugin = plugin()

provider = pyblish.plugin.Provider()
provider.inject("context", context)
provider.inject("instance", instance)

records = list()
handler = pyblish.lib.MessageHandler(records)

root_logger = logging.getLogger()
root_logger.addHandler(handler)

__start = time.time()

try:
provider.invoke(plugin.repair)
result["success"] = True
except Exception as error:
pyblish.lib.extract_traceback(error)
result["error"] = error

__end = time.time()

for record in records:
result["records"].append(record)

# Restore balance to the world
root_logger.removeHandler(handler)

result["duration"] = (__end - __start) * 1000 # ms

context.data("results").append(result)

return result


class AbstractEntity(list):
"""Superclass for Context and Instance"""

Oops, something went wrong.

0 comments on commit 68ded82

Please sign in to comment.