From 6e1a1bfbe7c46d66ff5785b316f527b7625979ef Mon Sep 17 00:00:00 2001 From: Hannes Delbeke Date: Fri, 8 Oct 2021 12:02:07 +0100 Subject: [PATCH 1/4] move shared code to helper function --- pyblish/plugin.py | 48 +++++++++++++++++++++++++++++------------------ 1 file changed, 30 insertions(+), 18 deletions(-) diff --git a/pyblish/plugin.py b/pyblish/plugin.py index e6636a9..f8db63f 100644 --- a/pyblish/plugin.py +++ b/pyblish/plugin.py @@ -1354,27 +1354,11 @@ def discover(type=None, regex=None, paths=None): log.debug("Skipped: \"%s\" (%s)", mod_name, err) continue - for plugin in plugins_from_module(module): - if not ALLOW_DUPLICATES and plugin.__name__ in plugin_names: - log.debug("Duplicate plug-in found: %s", plugin) - continue - - plugin_names.append(plugin.__name__) - - plugin.__module__ = module.__file__ - key = "{0}.{1}".format(plugin.__module__, plugin.__name__) - plugins[key] = plugin + _register_plugins_helper(plugins_from_module(module), plugin_names, plugins, module=module) # Include plug-ins from registration. # Directly registered plug-ins take precedence. - for plugin in registered_plugins(): - if not ALLOW_DUPLICATES and plugin.__name__ in plugin_names: - log.debug("Duplicate plug-in found: %s", plugin) - continue - - plugin_names.append(plugin.__name__) - - plugins[plugin.__name__] = plugin + _register_plugins_helper(registered_plugins(), plugin_names, plugins, module=None) plugins = list(plugins.values()) sort(plugins) # In-place @@ -1386,6 +1370,34 @@ def discover(type=None, regex=None, paths=None): return plugins +def _register_plugins_helper(plugins_to_register, plugin_names, plugins, module=None): + """ + add the plugin to the dict with the correct key + append the plugin name to plugin_names + + arguments: + plugins_to_register: new plugins to register + plugin_names: list of plugin names to append to + plugins: dict of registered plugins + + module=None: optional module when importing a plugin from a module instead of a file + """ + for plugin in plugins_to_register: + if not ALLOW_DUPLICATES and plugin.__name__ in plugin_names: + log.debug("Duplicate plug-in found: %s", plugin) + continue + + plugin_names.append(plugin.__name__) + + if module: + plugin.__module__ = module.__file__ + key = "{0}.{1}".format(plugin.__module__, plugin.__name__) + else: + key = plugin.__name__ + + plugins[key] = plugin + + def plugins_from_module(module): """Return plug-ins from module From 9ad7b9f2b6cee907c915bc56083a1d65ca3fd5ce Mon Sep 17 00:00:00 2001 From: Hannes Delbeke Date: Fri, 8 Oct 2021 12:21:07 +0100 Subject: [PATCH 2/4] move code to helper func to reuse later (+1 squashed commits) Squashed commits: [094c298] fix previous commit [f015344] move code to helper func to reuse later --- pyblish/plugin.py | 74 ++++++++++++++++++++++++++++------------------- 1 file changed, 44 insertions(+), 30 deletions(-) diff --git a/pyblish/plugin.py b/pyblish/plugin.py index f8db63f..33ab6f6 100644 --- a/pyblish/plugin.py +++ b/pyblish/plugin.py @@ -1324,37 +1324,12 @@ def discover(type=None, regex=None, paths=None): if not os.path.isdir(path): continue + # register plugins from modules in folder for fname in os.listdir(path): - if fname.startswith("_"): - continue - - abspath = os.path.join(path, fname) - - if not os.path.isfile(abspath): - continue - - mod_name, mod_ext = os.path.splitext(fname) - - if not mod_ext == ".py": - continue - - module = types.ModuleType(mod_name) - module.__file__ = abspath - - try: - with open(abspath, "rb") as f: - six.exec_(f.read(), module.__dict__) - - # Store reference to original module, to avoid - # garbage collection from collecting it's global - # imports, such as `import os`. - sys.modules[abspath] = module - - except Exception as err: - log.debug("Skipped: \"%s\" (%s)", mod_name, err) - continue - - _register_plugins_helper(plugins_from_module(module), plugin_names, plugins, module=module) + module = _valid_plugin_module(abspath) + if module: + plugins_in_module = plugins_from_module(module) + _register_plugins_helper(plugins_in_module, plugin_names, plugins, module=module) # Include plug-ins from registration. # Directly registered plug-ins take precedence. @@ -1370,6 +1345,45 @@ def discover(type=None, regex=None, paths=None): return plugins +def _valid_plugin_module(abspath): + """load any""" + + if '/' in abspath: + split_char = '/' + elif '\\' in abspath: + split_char = '\\' + path, fname = abspath.rsplit(split_char, 1) + + if fname.startswith("_"): + return + + if not os.path.isfile(abspath): + return + + mod_name, mod_ext = os.path.splitext(fname) + + if not mod_ext == ".py": + return + + module = types.ModuleType(mod_name) + module.__file__ = abspath + + try: + with open(abspath, "rb") as f: + six.exec_(f.read(), module.__dict__) + + # Store reference to original module, to avoid + # garbage collection from collecting it's global + # imports, such as `import os`. + sys.modules[abspath] = module + + except Exception as err: + log.debug("Skipped: \"%s\" (%s)", mod_name, err) + return + + return module + + def _register_plugins_helper(plugins_to_register, plugin_names, plugins, module=None): """ add the plugin to the dict with the correct key From 36d9902732491e0bd240a9bdc8ee17e298098869 Mon Sep 17 00:00:00 2001 From: "hannes.delbeke" Date: Fri, 8 Oct 2021 14:28:42 +0100 Subject: [PATCH 3/4] add support for direct registered modules, instead of only parent folder --- pyblish/plugin.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/pyblish/plugin.py b/pyblish/plugin.py index 33ab6f6..d63daac 100644 --- a/pyblish/plugin.py +++ b/pyblish/plugin.py @@ -1321,12 +1321,22 @@ def discover(type=None, regex=None, paths=None): # Include plug-ins from registered paths for path in paths or plugin_paths(): path = os.path.normpath(path) - if not os.path.isdir(path): + + if not os.path.exists(path): continue + # check if folder or file + if os.path.isdir(path): + file_paths = [os.path.join(path, fname) for fname in os.listdir(path)] + elif os.path.isfile(path): + # todo check if valid path? + file_paths = [path] + # register plugins from modules in folder - for fname in os.listdir(path): + for abspath in file_paths: + module = _valid_plugin_module(abspath) + if module: plugins_in_module = plugins_from_module(module) _register_plugins_helper(plugins_in_module, plugin_names, plugins, module=module) From 4dc0e2d633ba82f270b41701c4cf48b15ac36406 Mon Sep 17 00:00:00 2001 From: "hannes.delbeke" Date: Mon, 11 Oct 2021 18:55:12 +0100 Subject: [PATCH 4/4] add log debug info --- pyblish/plugin.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pyblish/plugin.py b/pyblish/plugin.py index d63daac..a3b0e92 100644 --- a/pyblish/plugin.py +++ b/pyblish/plugin.py @@ -1365,14 +1365,17 @@ def _valid_plugin_module(abspath): path, fname = abspath.rsplit(split_char, 1) if fname.startswith("_"): + log.debug('Skipped: private module: "%s"', fname) return if not os.path.isfile(abspath): + log.debug('Skipped: "%s" is not a file', abspath) return mod_name, mod_ext = os.path.splitext(fname) if not mod_ext == ".py": + log.debug('Skipped: "%s" is not a python file', fname) return module = types.ModuleType(mod_name)