diff --git a/pype/__init__.py b/pype/__init__.py index 7f189bb814..43ca61e29a 100644 --- a/pype/__init__.py +++ b/pype/__init__.py @@ -2,7 +2,7 @@ import os from pyblish import api as pyblish from avalon import api as avalon -from Qt import QtWidgets +# from Qt import QtWidgets from .lib import filter_pyblish_plugins import logging @@ -28,17 +28,17 @@ def install(): avalon.register_plugin_path(avalon.Loader, LOAD_PATH) # pyblish-qml settings. - try: - __import__("pyblish_qml") - except ImportError as e: - log.error("Could not load pyblish-qml: %s " % e) - else: - from pyblish_qml import settings - app = QtWidgets.QApplication.instance() - screen_resolution = app.desktop().screenGeometry() - width, height = screen_resolution.width(), screen_resolution.height() - settings.WindowSize = (width / 3, height * 0.75) - settings.WindowPosition = (0, 0) + # try: + # __import__("pyblish_qml") + # except ImportError as e: + # log.error("Could not load pyblish-qml: %s " % e) + # else: + # from pyblish_qml import settings + # app = QtWidgets.QApplication.instance() + # screen_resolution = app.desktop().screenGeometry() + # width, height = screen_resolution.width(), screen_resolution.height() + # settings.WindowSize = (width / 3, height * 0.75) + # settings.WindowPosition = (0, 0) def uninstall(): diff --git a/pype/lib.py b/pype/lib.py index 8120a1456a..78658b498b 100644 --- a/pype/lib.py +++ b/pype/lib.py @@ -490,18 +490,26 @@ def filter_pyblish_plugins(plugins): :type plugins: Dict """ from pypeapp import config + from pyblish import api + + host = api.current_host() # load plugins config_data = config.get_presets()['plugins']['config'] # iterate over plugins for plugin in plugins[:]: - if config_data.get(plugin.__name__): - for option, value in config_data[plugin.__name__].items(): - if hasattr(plugin, option): - log.info('setting {}:{} on plugin {}'.format( - option, value, plugin.__name__)) - setattr(plugin, option, value) - if option == "enabled": - log.info('removing plugin {}'.format(plugin.__name__)) - plugins.remove(plugin) + try: + config_data = config.get_presets()['plugins'][host][plugin.__name__] # noqa: E501 + except KeyError: + continue + + for option, value in config_data.items(): + if option == "enabled" and value is False: + log.info('removing plugin {}'.format(plugin.__name__)) + plugins.remove(plugin) + else: + log.info('setting {}:{} on plugin {}'.format( + option, value, plugin.__name__)) + + setattr(plugin, option, value) diff --git a/pype/tests/__init__.py b/pype/tests/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/pype/tests/lib.py b/pype/tests/lib.py new file mode 100644 index 0000000000..85b9032836 --- /dev/null +++ b/pype/tests/lib.py @@ -0,0 +1,80 @@ +import os +import sys +import shutil +import tempfile +import contextlib + +import pyblish +import pyblish.cli +import pyblish.plugin +from pyblish.vendor import six + + +# Setup +HOST = 'python' +FAMILY = 'test.family' + +REGISTERED = pyblish.plugin.registered_paths() +PACKAGEPATH = pyblish.lib.main_package_path() +ENVIRONMENT = os.environ.get("PYBLISHPLUGINPATH", "") +PLUGINPATH = os.path.join(PACKAGEPATH, '..', 'tests', 'plugins') + + +def setup(): + pyblish.plugin.deregister_all_paths() + + +def setup_empty(): + """Disable all plug-ins""" + setup() + pyblish.plugin.deregister_all_plugins() + pyblish.plugin.deregister_all_paths() + pyblish.plugin.deregister_all_hosts() + pyblish.plugin.deregister_all_callbacks() + pyblish.plugin.deregister_all_targets() + pyblish.api.deregister_all_discovery_filters() + + +def teardown(): + """Restore previously REGISTERED paths""" + + pyblish.plugin.deregister_all_paths() + for path in REGISTERED: + pyblish.plugin.register_plugin_path(path) + + os.environ["PYBLISHPLUGINPATH"] = ENVIRONMENT + pyblish.api.deregister_all_plugins() + pyblish.api.deregister_all_hosts() + pyblish.api.deregister_all_discovery_filters() + pyblish.api.deregister_test() + pyblish.api.__init__() + + +@contextlib.contextmanager +def captured_stdout(): + """Temporarily reassign stdout to a local variable""" + try: + sys.stdout = six.StringIO() + yield sys.stdout + finally: + sys.stdout = sys.__stdout__ + + +@contextlib.contextmanager +def captured_stderr(): + """Temporarily reassign stderr to a local variable""" + try: + sys.stderr = six.StringIO() + yield sys.stderr + finally: + sys.stderr = sys.__stderr__ + + +@contextlib.contextmanager +def tempdir(): + """Provide path to temporary directory""" + try: + tempdir = tempfile.mkdtemp() + yield tempdir + finally: + shutil.rmtree(tempdir) diff --git a/pype/tests/test_pyblish_filter.py b/pype/tests/test_pyblish_filter.py new file mode 100644 index 0000000000..27fed50c40 --- /dev/null +++ b/pype/tests/test_pyblish_filter.py @@ -0,0 +1,41 @@ +from . import lib +import pyblish.api +import pyblish.util +import pyblish.plugin +from pype.lib import filter_pyblish_plugins +import os + + +def test_pyblish_plugin_filter(printer, monkeypatch): + """ + Test if pyblish filter can filter and modify plugins on-the-fly. + """ + + lib.setup_empty() + monkeypatch.setitem(os.environ, 'PYBLISHPLUGINPATH', '') + plugins = pyblish.api.registered_plugins() + printer("Test if we have no registered plugins") + assert len(plugins) == 0 + paths = pyblish.api.registered_paths() + printer("Test if we have no registered plugin paths") + print(paths) + + class MyTestPlugin(pyblish.api.InstancePlugin): + my_test_property = 1 + label = "Collect Renderable Camera(s)" + hosts = ["test"] + families = ["default"] + + pyblish.api.register_host("test") + pyblish.api.register_plugin(MyTestPlugin) + pyblish.api.register_discovery_filter(filter_pyblish_plugins) + plugins = pyblish.api.discover() + + printer("Test if only one plugin was discovered") + assert len(plugins) == 1 + printer("Test if properties are modified correctly") + assert plugins[0].label == "loaded from preset" + assert plugins[0].families == ["changed", "by", "preset"] + assert plugins[0].optional is True + + lib.teardown()