refactor a bit more

This commit is contained in:
Matthias 2021-11-12 02:32:17 +01:00
parent 0e6f203660
commit aa0237723a
No known key found for this signature in database
GPG Key ID: F50EF5741D33E076

View File

@ -29,56 +29,28 @@ class PluginAppConfig(AppConfig):
try: try:
# we are using the db from here - so for migrations etc we need to try this block # we are using the db from here - so for migrations etc we need to try this block
self.init_plugins() self.init_plugins()
self.activate_plugins() self.activate_integration()
except (OperationalError, ProgrammingError): except (OperationalError, ProgrammingError):
# Exception if the database has not been migrated yet # Exception if the database has not been migrated yet
pass pass
def activate_plugins(self): def collect_plugins(self):
"""fullfill integrations for all activated plugins""" """collect integration plugins from all possible ways of loading"""
from common.models import InvenTreeSetting # Collect plugins from paths
for plugin in settings.PLUGIN_DIRS:
modules = inventree_plugins.get_plugins(importlib.import_module(plugin), IntegrationPluginBase, True)
if modules:
[settings.PLUGINS.append(item) for item in modules]
# activate integrations # Collect plugins from setup entry points
plugins = settings.INTEGRATION_PLUGINS.items() for entry in metadata.entry_points().get('inventree_plugins', []):
logger.info(f'Found {len(plugins)} active plugins') plugin = entry.load()
plugin.is_package = True
settings.PLUGINS.append(plugin)
# if plugin settings are enabled enhance the settings # Log found plugins
if settings.TESTING or InvenTreeSetting.get_setting('ENABLE_PLUGINS_SETTING'): logger.info(f'Found {len(settings.PLUGINS)} plugins!')
logger.info('Registering IntegrationPlugin settings') logger.info(", ".join([a.__module__ for a in settings.PLUGINS]))
for slug, plugin in plugins:
if plugin.mixin_enabled('settings'):
plugin_setting = plugin.settingspatterns
settings.INTEGRATION_PLUGIN_SETTING[slug] = plugin_setting
# Add to settings dir
InvenTreeSetting.GLOBAL_SETTINGS.update(plugin_setting)
# if plugin apps are enabled
if settings.TESTING or ((not settings.INTEGRATION_APPS_LOADED) and InvenTreeSetting.get_setting('ENABLE_PLUGINS_APP')):
logger.info('Registering IntegrationPlugin apps')
settings.INTEGRATION_APPS_LOADED = True # ensure this section will not run again
apps_changed = False
# add them to the INSTALLED_APPS
for slug, plugin in plugins:
if plugin.mixin_enabled('app'):
try:
# for local path plugins
plugin_path = '.'.join(pathlib.Path(plugin.path).relative_to(settings.BASE_DIR).parts)
except ValueError:
# plugin is shipped as package
plugin_path = plugin.PLUGIN_NAME
if plugin_path not in settings.INSTALLED_APPS:
settings.INSTALLED_APPS += [plugin_path]
apps_changed = True
# if apps were changed reload
# TODO this is a bit jankey to be honest
if apps_changed:
apps.app_configs = OrderedDict()
apps.apps_ready = apps.models_ready = apps.loading = apps.ready = False
apps.clear_cache()
apps.populate(settings.INSTALLED_APPS)
def init_plugins(self): def init_plugins(self):
"""initialise all found plugins""" """initialise all found plugins"""
@ -110,20 +82,56 @@ class PluginAppConfig(AppConfig):
# save for later reference # save for later reference
settings.INTEGRATION_PLUGINS_INACTIVE[plug_key] = plugin_db_setting settings.INTEGRATION_PLUGINS_INACTIVE[plug_key] = plugin_db_setting
def collect_plugins(self): def activate_integration(self):
"""collect integration plugins from all possible ways of loading""" """fullfill integrations for all activated plugins"""
# Collect plugins from paths # activate integrations
for plugin in settings.PLUGIN_DIRS: plugins = settings.INTEGRATION_PLUGINS.items()
modules = inventree_plugins.get_plugins(importlib.import_module(plugin), IntegrationPluginBase, True) logger.info(f'Found {len(plugins)} active plugins')
if modules:
[settings.PLUGINS.append(item) for item in modules]
# Collect plugins from setup entry points # if plugin settings are enabled enhance the settings
for entry in metadata.entry_points().get('inventree_plugins', []): self.activate_integration_settings(plugins)
plugin = entry.load()
plugin.is_package = True
settings.PLUGINS.append(plugin)
# Log found plugins # if plugin apps are enabled
logger.info(f'Found {len(settings.PLUGINS)} plugins!') self.activate_integration_app(plugins)
logger.info(", ".join([a.__module__ for a in settings.PLUGINS]))
def activate_integration_settings(self, plugins):
from common.models import InvenTreeSetting
if settings.TESTING or InvenTreeSetting.get_setting('ENABLE_PLUGINS_SETTING'):
logger.info('Registering IntegrationPlugin settings')
for slug, plugin in plugins:
if plugin.mixin_enabled('settings'):
plugin_setting = plugin.settingspatterns
settings.INTEGRATION_PLUGIN_SETTING[slug] = plugin_setting
# Add to settings dir
InvenTreeSetting.GLOBAL_SETTINGS.update(plugin_setting)
def activate_integration_app(self, plugins):
from common.models import InvenTreeSetting
if settings.TESTING or ((not settings.INTEGRATION_APPS_LOADED) and InvenTreeSetting.get_setting('ENABLE_PLUGINS_APP')):
logger.info('Registering IntegrationPlugin apps')
settings.INTEGRATION_APPS_LOADED = True # ensure this section will not run again
apps_changed = False
# add them to the INSTALLED_APPS
for slug, plugin in plugins:
if plugin.mixin_enabled('app'):
try:
# for local path plugins
plugin_path = '.'.join(pathlib.Path(plugin.path).relative_to(settings.BASE_DIR).parts)
except ValueError:
# plugin is shipped as package
plugin_path = plugin.PLUGIN_NAME
if plugin_path not in settings.INSTALLED_APPS:
settings.INSTALLED_APPS += [plugin_path]
apps_changed = True
# if apps were changed reload
# TODO this is a bit jankey to be honest
if apps_changed:
apps.app_configs = OrderedDict()
apps.apps_ready = apps.models_ready = apps.loading = apps.ready = False
apps.clear_cache()
apps.populate(settings.INSTALLED_APPS)