From 678b89e09398720ce09f51ff9374ea9cab80cf79 Mon Sep 17 00:00:00 2001 From: Matthias Date: Wed, 12 Jan 2022 00:59:09 +0100 Subject: [PATCH] consolidate helpers --- InvenTree/plugin/helpers.py | 56 ++++++++++++++++++++++++++++++++++ InvenTree/plugin/plugins.py | 59 ------------------------------------ InvenTree/plugin/registry.py | 5 ++- 3 files changed, 58 insertions(+), 62 deletions(-) delete mode 100644 InvenTree/plugin/plugins.py diff --git a/InvenTree/plugin/helpers.py b/InvenTree/plugin/helpers.py index 3b9164fc8f..31eac195c3 100644 --- a/InvenTree/plugin/helpers.py +++ b/InvenTree/plugin/helpers.py @@ -6,8 +6,11 @@ import subprocess import pathlib import sysconfig import traceback +import inspect +import pkgutil from django.conf import settings +from django.core.exceptions import AppRegistryNotReady # region logging / errors @@ -135,3 +138,56 @@ class GitStatus: R = Definition(key='R', status=2, msg='good signature, revoked key',) E = Definition(key='E', status=1, msg='cannot be checked',) # endregion + + +# region plugin finders +def get_modules(pkg): + """get all modules in a package""" + + context = {} + for loader, name, ispkg in pkgutil.walk_packages(pkg.__path__): + try: + module = loader.find_module(name).load_module(name) + pkg_names = getattr(module, '__all__', None) + for k, v in vars(module).items(): + if not k.startswith('_') and (pkg_names is None or k in pkg_names): + context[k] = v + context[name] = module + except AppRegistryNotReady: + pass + except Exception as error: + # this 'protects' against malformed plugin modules by more or less silently failing + + # log to stack + log_error({name: str(error)}, 'discovery') + + return [v for k, v in context.items()] + + +def get_classes(module): + """get all classes in a given module""" + return inspect.getmembers(module, inspect.isclass) + + +def get_plugins(pkg, baseclass): + """ + Return a list of all modules under a given package. + + - Modules must be a subclass of the provided 'baseclass' + - Modules must have a non-empty PLUGIN_NAME parameter + """ + + plugins = [] + + modules = get_modules(pkg) + + # Iterate through each module in the package + for mod in modules: + # Iterate through each class in the module + for item in get_classes(mod): + plugin = item[1] + if issubclass(plugin, baseclass) and plugin.PLUGIN_NAME: + plugins.append(plugin) + + return plugins +# endregion \ No newline at end of file diff --git a/InvenTree/plugin/plugins.py b/InvenTree/plugin/plugins.py deleted file mode 100644 index 0d51442d45..0000000000 --- a/InvenTree/plugin/plugins.py +++ /dev/null @@ -1,59 +0,0 @@ -# -*- coding: utf-8 -*- -"""general functions for plugin handeling""" - -import inspect -import pkgutil - -from django.core.exceptions import AppRegistryNotReady - - -def get_modules(pkg): - """get all modules in a package""" - from plugin.helpers import log_error - - context = {} - for loader, name, ispkg in pkgutil.walk_packages(pkg.__path__): - try: - module = loader.find_module(name).load_module(name) - pkg_names = getattr(module, '__all__', None) - for k, v in vars(module).items(): - if not k.startswith('_') and (pkg_names is None or k in pkg_names): - context[k] = v - context[name] = module - except AppRegistryNotReady: - pass - except Exception as error: - # this 'protects' against malformed plugin modules by more or less silently failing - - # log to stack - log_error({name: str(error)}, 'discovery') - - return [v for k, v in context.items()] - - -def get_classes(module): - """get all classes in a given module""" - return inspect.getmembers(module, inspect.isclass) - - -def get_plugins(pkg, baseclass): - """ - Return a list of all modules under a given package. - - - Modules must be a subclass of the provided 'baseclass' - - Modules must have a non-empty PLUGIN_NAME parameter - """ - - plugins = [] - - modules = get_modules(pkg) - - # Iterate through each module in the package - for mod in modules: - # Iterate through each class in the module - for item in get_classes(mod): - plugin = item[1] - if issubclass(plugin, baseclass) and plugin.PLUGIN_NAME: - plugins.append(plugin) - - return plugins diff --git a/InvenTree/plugin/registry.py b/InvenTree/plugin/registry.py index 6c43296c76..e31f3c6529 100644 --- a/InvenTree/plugin/registry.py +++ b/InvenTree/plugin/registry.py @@ -28,9 +28,8 @@ except: from maintenance_mode.core import maintenance_mode_on from maintenance_mode.core import get_maintenance_mode, set_maintenance_mode -from plugin import plugins as inventree_plugins from .integration import IntegrationPluginBase -from .helpers import handle_error, log_error, IntegrationPluginError +from .helpers import handle_error, log_error, get_plugins, IntegrationPluginError logger = logging.getLogger('inventree') @@ -177,7 +176,7 @@ class PluginsRegistry: # Collect plugins from paths for plugin in settings.PLUGIN_DIRS: - modules = inventree_plugins.get_plugins(importlib.import_module(plugin), IntegrationPluginBase) + modules = get_plugins(importlib.import_module(plugin), IntegrationPluginBase) if modules: [self.plugin_modules.append(item) for item in modules]