From fa9168d660065665ba6ad753ca256963d813eb4a Mon Sep 17 00:00:00 2001 From: Matthias Date: Sat, 9 Oct 2021 20:43:46 +0200 Subject: [PATCH] preoad modules from external --- InvenTree/InvenTree/settings.py | 19 +++++++++++++++++++ InvenTree/plugin/plugins.py | 30 +++++++++++++++++++++++------- 2 files changed, 42 insertions(+), 7 deletions(-) diff --git a/InvenTree/InvenTree/settings.py b/InvenTree/InvenTree/settings.py index baed15afa2..216908798f 100644 --- a/InvenTree/InvenTree/settings.py +++ b/InvenTree/InvenTree/settings.py @@ -19,6 +19,7 @@ import string import shutil import sys import pathlib +import importlib from datetime import datetime import moneyed @@ -28,6 +29,7 @@ from django.utils.translation import gettext_lazy as _ from django.contrib.messages import constants as messages from plugin import plugins as inventree_plugins +from plugin.integration import IntegrationPluginBase def _is_true(x): @@ -659,6 +661,23 @@ MESSAGE_TAGS = { # Plugins PLUGIN_URL = 'plugin' +PLUGIN_DIRS = [ + 'plugin.builtin', + 'plugins', +] + +# load samples if in debug mode +if DEBUG: + PLUGIN_DIRS.append('plugin.samples') + +# collect all plugins from paths +PLUGINS = [] +for plugin in PLUGIN_DIRS: + modules = inventree_plugins.get_plugins(importlib.import_module(plugin), IntegrationPluginBase, True) + if modules: + [PLUGINS.append(item) for item in modules] + +# collect integration plugins INTEGRATION_PLUGINS = [] INTEGRATION_PLUGIN_SETTINGS = {} diff --git a/InvenTree/plugin/plugins.py b/InvenTree/plugin/plugins.py index e31c905550..e4ec361a38 100644 --- a/InvenTree/plugin/plugins.py +++ b/InvenTree/plugin/plugins.py @@ -6,12 +6,14 @@ import importlib import pkgutil import logging +from django.conf import settings +from django.core.exceptions import AppRegistryNotReady + # Action plugins import plugin.builtin.action as action from plugin.action import ActionPlugin # Integration -import plugin.builtin.integration as integration from plugin.integration import IntegrationPluginBase @@ -23,9 +25,23 @@ def iter_namespace(pkg): return pkgutil.iter_modules(pkg.__path__, pkg.__name__ + ".") -def get_modules(pkg): +def get_modules(pkg, recursive): """get all modules in a package""" - return [importlib.import_module(name) for finder, name, ispkg in iter_namespace(pkg)] + if not recursive: + return [importlib.import_module(name) for finder, name, ispkg in iter_namespace(pkg)] + + 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 + return [v for k, v in context.items()] def get_classes(module): @@ -33,7 +49,7 @@ def get_classes(module): return inspect.getmembers(module, inspect.isclass) -def get_plugins(pkg, baseclass): +def get_plugins(pkg, baseclass, recursive): """ Return a list of all modules under a given package. @@ -43,7 +59,7 @@ def get_plugins(pkg, baseclass): plugins = [] - modules = get_modules(pkg) + modules = get_modules(pkg, recursive) # Iterate through each module in the package for mod in modules: @@ -67,7 +83,7 @@ def load_plugins(name: str, cls, module=None): logger.debug("Loading %s plugins", name) - plugins = get_plugins(module, cls) + plugins = get_plugins(module, cls) if module else settings.PLUGINS if len(plugins) > 0: logger.info("Discovered %i %s plugins:", len(plugins), name) @@ -89,7 +105,7 @@ def load_integration_plugins(): """ Return a list of all registered integration plugins """ - return load_plugins('integration', IntegrationPluginBase, module=integration) + return load_plugins('integration', IntegrationPluginBase) def load_barcode_plugins():