mirror of
https://github.com/inventree/InvenTree.git
synced 2025-06-16 20:15:44 +00:00
Allow plugins to be loaded from an external directory
This commit is contained in:
@ -172,10 +172,16 @@ class GitStatus:
|
|||||||
|
|
||||||
|
|
||||||
# region plugin finders
|
# region plugin finders
|
||||||
def get_modules(pkg):
|
def get_modules(pkg, path=None):
|
||||||
"""Get all modules in a package."""
|
"""Get all modules in a package."""
|
||||||
context = {}
|
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:
|
try:
|
||||||
module = loader.find_module(name).load_module(name)
|
module = loader.find_module(name).load_module(name)
|
||||||
pkg_names = getattr(module, '__all__', None)
|
pkg_names = getattr(module, '__all__', None)
|
||||||
@ -199,7 +205,7 @@ def get_classes(module):
|
|||||||
return inspect.getmembers(module, inspect.isclass)
|
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.
|
"""Return a list of all modules under a given package.
|
||||||
|
|
||||||
- Modules must be a subclass of the provided 'baseclass'
|
- Modules must be a subclass of the provided 'baseclass'
|
||||||
@ -207,7 +213,7 @@ def get_plugins(pkg, baseclass):
|
|||||||
"""
|
"""
|
||||||
plugins = []
|
plugins = []
|
||||||
|
|
||||||
modules = get_modules(pkg)
|
modules = get_modules(pkg, path=path)
|
||||||
|
|
||||||
# Iterate through each module in the package
|
# Iterate through each module in the package
|
||||||
for mod in modules:
|
for mod in modules:
|
||||||
|
@ -197,7 +197,23 @@ class PluginsRegistry:
|
|||||||
|
|
||||||
# Collect plugins from paths
|
# Collect plugins from paths
|
||||||
for plugin in settings.PLUGIN_DIRS:
|
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:
|
if modules:
|
||||||
[self.plugin_modules.append(item) for item in modules]
|
[self.plugin_modules.append(item) for item in modules]
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user