mirror of
				https://github.com/inventree/InvenTree.git
				synced 2025-10-30 20:55:42 +00:00 
			
		
		
		
	consolidate helpers
This commit is contained in:
		| @@ -6,8 +6,11 @@ import subprocess | |||||||
| import pathlib | import pathlib | ||||||
| import sysconfig | import sysconfig | ||||||
| import traceback | import traceback | ||||||
|  | import inspect | ||||||
|  | import pkgutil | ||||||
|  |  | ||||||
| from django.conf import settings | from django.conf import settings | ||||||
|  | from django.core.exceptions import AppRegistryNotReady | ||||||
|  |  | ||||||
|  |  | ||||||
| # region logging / errors | # region logging / errors | ||||||
| @@ -135,3 +138,56 @@ class GitStatus: | |||||||
|     R = Definition(key='R', status=2, msg='good signature, revoked key',) |     R = Definition(key='R', status=2, msg='good signature, revoked key',) | ||||||
|     E = Definition(key='E', status=1, msg='cannot be checked',) |     E = Definition(key='E', status=1, msg='cannot be checked',) | ||||||
| # endregion | # endregion | ||||||
|  |  | ||||||
|  |  | ||||||
|  | # region plugin finders | ||||||
|  | def get_modules(pkg): | ||||||
|  |     """get all modules in a package""" | ||||||
|  |  | ||||||
|  |     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 | ||||||
|  |         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): | ||||||
|  |     """get all classes in a given module""" | ||||||
|  |     return inspect.getmembers(module, inspect.isclass) | ||||||
|  |  | ||||||
|  |  | ||||||
|  | def get_plugins(pkg, baseclass): | ||||||
|  |     """ | ||||||
|  |     Return a list of all modules under a given package. | ||||||
|  |  | ||||||
|  |     - Modules must be a subclass of the provided 'baseclass' | ||||||
|  |     - Modules must have a non-empty PLUGIN_NAME parameter | ||||||
|  |     """ | ||||||
|  |  | ||||||
|  |     plugins = [] | ||||||
|  |  | ||||||
|  |     modules = get_modules(pkg) | ||||||
|  |  | ||||||
|  |     # Iterate through each module in the package | ||||||
|  |     for mod in modules: | ||||||
|  |         # Iterate through each class in the module | ||||||
|  |         for item in get_classes(mod): | ||||||
|  |             plugin = item[1] | ||||||
|  |             if issubclass(plugin, baseclass) and plugin.PLUGIN_NAME: | ||||||
|  |                 plugins.append(plugin) | ||||||
|  |  | ||||||
|  |     return plugins | ||||||
|  | # endregion | ||||||
| @@ -1,59 +0,0 @@ | |||||||
| # -*- coding: utf-8 -*- |  | ||||||
| """general functions for plugin handeling""" |  | ||||||
|  |  | ||||||
| import inspect |  | ||||||
| import pkgutil |  | ||||||
|  |  | ||||||
| from django.core.exceptions import AppRegistryNotReady |  | ||||||
|  |  | ||||||
|  |  | ||||||
| def get_modules(pkg): |  | ||||||
|     """get all modules in a package""" |  | ||||||
|     from plugin.helpers import log_error |  | ||||||
|  |  | ||||||
|     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 |  | ||||||
|         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): |  | ||||||
|     """get all classes in a given module""" |  | ||||||
|     return inspect.getmembers(module, inspect.isclass) |  | ||||||
|  |  | ||||||
|  |  | ||||||
| def get_plugins(pkg, baseclass): |  | ||||||
|     """ |  | ||||||
|     Return a list of all modules under a given package. |  | ||||||
|  |  | ||||||
|     - Modules must be a subclass of the provided 'baseclass' |  | ||||||
|     - Modules must have a non-empty PLUGIN_NAME parameter |  | ||||||
|     """ |  | ||||||
|  |  | ||||||
|     plugins = [] |  | ||||||
|  |  | ||||||
|     modules = get_modules(pkg) |  | ||||||
|  |  | ||||||
|     # Iterate through each module in the package |  | ||||||
|     for mod in modules: |  | ||||||
|         # Iterate through each class in the module |  | ||||||
|         for item in get_classes(mod): |  | ||||||
|             plugin = item[1] |  | ||||||
|             if issubclass(plugin, baseclass) and plugin.PLUGIN_NAME: |  | ||||||
|                 plugins.append(plugin) |  | ||||||
|  |  | ||||||
|     return plugins |  | ||||||
| @@ -28,9 +28,8 @@ except: | |||||||
| from maintenance_mode.core import maintenance_mode_on | from maintenance_mode.core import maintenance_mode_on | ||||||
| from maintenance_mode.core import get_maintenance_mode, set_maintenance_mode | from maintenance_mode.core import get_maintenance_mode, set_maintenance_mode | ||||||
|  |  | ||||||
| from plugin import plugins as inventree_plugins |  | ||||||
| from .integration import IntegrationPluginBase | from .integration import IntegrationPluginBase | ||||||
| from .helpers import handle_error, log_error, IntegrationPluginError | from .helpers import handle_error, log_error, get_plugins, IntegrationPluginError | ||||||
|  |  | ||||||
|  |  | ||||||
| logger = logging.getLogger('inventree') | logger = logging.getLogger('inventree') | ||||||
| @@ -177,7 +176,7 @@ class PluginsRegistry: | |||||||
|  |  | ||||||
|         # Collect plugins from paths |         # Collect plugins from paths | ||||||
|         for plugin in settings.PLUGIN_DIRS: |         for plugin in settings.PLUGIN_DIRS: | ||||||
|             modules = inventree_plugins.get_plugins(importlib.import_module(plugin), IntegrationPluginBase) |             modules = get_plugins(importlib.import_module(plugin), IntegrationPluginBase) | ||||||
|             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