mirror of
https://github.com/inventree/InvenTree.git
synced 2025-08-02 01:51:33 +00:00
Improved exception handling for plugin loading
This commit is contained in:
@@ -177,41 +177,47 @@ def get_modules(pkg, path=None):
|
|||||||
elif type(path) is not list:
|
elif type(path) is not list:
|
||||||
path = [path]
|
path = [path]
|
||||||
|
|
||||||
try:
|
packages = pkgutil.walk_packages(path)
|
||||||
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
|
|
||||||
|
|
||||||
# log to stack
|
while True:
|
||||||
log_error({name: str(error)}, 'discovery')
|
try:
|
||||||
except Exception as error:
|
finder, name, _ = next(packages)
|
||||||
# log to stack
|
except StopIteration:
|
||||||
log_error({pkg.__name__: str(error)}, 'discovery')
|
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()]
|
return [v for k, v in context.items()]
|
||||||
|
|
||||||
|
|
||||||
def get_classes(module) -> list:
|
def get_classes(module) -> list:
|
||||||
"""Get all classes in a given module."""
|
"""Get all classes in a given module."""
|
||||||
try:
|
# try:
|
||||||
return inspect.getmembers(module, inspect.isclass)
|
return inspect.getmembers(module, inspect.isclass)
|
||||||
except Exception:
|
# except Exception:
|
||||||
return []
|
# return []
|
||||||
|
|
||||||
|
|
||||||
def get_plugins(pkg, baseclass, path=None):
|
def get_plugins(pkg, baseclass, path=None):
|
||||||
|
Reference in New Issue
Block a user