2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-06-16 12:05:53 +00:00

Allow plugins to be loaded from an external directory

This commit is contained in:
Oliver Walters
2022-07-20 18:13:57 +10:00
parent 899c604a98
commit 94cc74ddf0
2 changed files with 27 additions and 5 deletions

View File

@ -172,10 +172,16 @@ class GitStatus:
# region plugin finders
def get_modules(pkg):
def get_modules(pkg, path=None):
"""Get all modules in a package."""
context = {}
for loader, name, _ in pkgutil.walk_packages(pkg.__path__):
if path is None:
path = pkg.__path__
elif type(path) is not list:
path = [path]
for loader, name, _ in pkgutil.walk_packages(path):
try:
module = loader.find_module(name).load_module(name)
pkg_names = getattr(module, '__all__', None)
@ -199,7 +205,7 @@ def get_classes(module):
return inspect.getmembers(module, inspect.isclass)
def get_plugins(pkg, baseclass):
def get_plugins(pkg, baseclass, path=None):
"""Return a list of all modules under a given package.
- Modules must be a subclass of the provided 'baseclass'
@ -207,7 +213,7 @@ def get_plugins(pkg, baseclass):
"""
plugins = []
modules = get_modules(pkg)
modules = get_modules(pkg, path=path)
# Iterate through each module in the package
for mod in modules:

View File

@ -197,7 +197,23 @@ class PluginsRegistry:
# Collect plugins from paths
for plugin in settings.PLUGIN_DIRS:
modules = get_plugins(importlib.import_module(plugin), InvenTreePlugin)
parent_path = None
# If a "path" is provided, some special handling is required
if os.path.sep in plugin:
path_split = [el for el in plugin.split(os.path.sep)]
if len(path_split) > 0:
parent_path = os.path.sep.join(path_split[:-1])
if not parent_path.endswith(os.path.sep):
parent_path += os.path.sep
plugin = path_split[-1]
modules = get_plugins(importlib.import_module(plugin), InvenTreePlugin, path=parent_path)
if modules:
[self.plugin_modules.append(item) for item in modules]