2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-08-01 17:41:33 +00:00

Improved exception handling for plugin loading

This commit is contained in:
Oliver Walters
2024-11-17 11:03:09 +00:00
parent 12538c9e83
commit ade4a46c86

View File

@@ -177,41 +177,47 @@ def get_modules(pkg, path=None):
elif type(path) is not list:
path = [path]
try:
for finder, name, _ in pkgutil.walk_packages(path):
try:
if sys.version_info < (3, 12):
module = finder.find_module(name).load_module(name)
else:
spec = finder.find_spec(name)
module = module_from_spec(spec)
sys.modules[name] = module
spec.loader.exec_module(module)
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: # pragma: no cover
pass
except Exception as error:
# this 'protects' against malformed plugin modules by more or less silently failing
packages = pkgutil.walk_packages(path)
# log to stack
log_error({name: str(error)}, 'discovery')
except Exception as error:
# log to stack
log_error({pkg.__name__: str(error)}, 'discovery')
while True:
try:
finder, name, _ = next(packages)
except StopIteration:
break
except Exception as error:
log_error({pkg.__name__: str(error)}, 'discovery')
continue
try:
if sys.version_info < (3, 12):
module = finder.find_module(name).load_module(name)
else:
spec = finder.find_spec(name)
module = module_from_spec(spec)
sys.modules[name] = module
spec.loader.exec_module(module)
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: # pragma: no cover
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) -> list:
"""Get all classes in a given module."""
try:
return inspect.getmembers(module, inspect.isclass)
except Exception:
return []
# try:
return inspect.getmembers(module, inspect.isclass)
# except Exception:
# return []
def get_plugins(pkg, baseclass, path=None):