module_loader: Split app initialization into separate steps

- Loading module is only for importing python modules and determining the order
in which they should be loaded.

- Initializing apps will create the instances which involves just creating the
components of the apps.

- Post initialization involves connecting to signals, running configuration
fixes, etc.

Tests:

- All apps that have post initialization step have been tested.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
Sunil Mohan Adapa 2021-11-14 12:17:47 -08:00 committed by James Valleroy
parent 1147249bd8
commit d2aee03c89
No known key found for this signature in database
GPG Key ID: 77C0C75E7B650808
2 changed files with 31 additions and 10 deletions

View File

@ -112,6 +112,7 @@ def main():
module_loader.include_urls()
menu.init()
module_loader.load_modules()
module_loader.apps_init()
list_dependencies(arguments.list_dependencies)
log.init()
@ -129,6 +130,8 @@ def main():
menu.init()
module_loader.load_modules()
module_loader.apps_init()
module_loader.apps_post_init()
frontpage.add_custom_shortcuts()
if arguments.setup is not False:

View File

@ -66,15 +66,9 @@ def load_modules():
except KeyError:
logger.error('Unsatified dependency for module - %s', module_name)
logger.info('Initializing apps - %s', ', '.join(ordered_modules))
for module_name in ordered_modules:
_initialize_module(module_name, modules[module_name])
loaded_modules[module_name] = modules[module_name]
logger.debug('App initialization completed.')
post_module_loading.send_robust(sender="module_loader")
def _insert_modules(module_name, module, remaining_modules, ordered_modules):
"""Insert modules into a list based on dependency order"""
@ -118,6 +112,13 @@ def _include_module_urls(module_import_path, module_name):
raise
def apps_init():
"""Create apps by constructing them with components."""
logger.info('Initializing apps - %s', ', '.join(loaded_modules))
for module_name, module in loaded_modules.items():
_initialize_module(module_name, module)
def _initialize_module(module_name, module):
"""Perform module initialization"""
@ -131,10 +132,6 @@ def _initialize_module(module_name, module):
]
if module_classes and app_class:
module.app = app_class[0][1]()
if module.setup_helper.get_state(
) != 'needs-setup' and module.app.is_enabled():
module.app.set_enabled(True)
except Exception as exception:
logger.exception('Exception while running init for %s: %s', module,
exception)
@ -142,6 +139,27 @@ def _initialize_module(module_name, module):
raise
def apps_post_init():
"""Run post initialization on each app."""
for module in loaded_modules.values():
if not hasattr(module, 'app') or not module.app:
continue
try:
module.app.post_init()
if module.setup_helper.get_state(
) != 'needs-setup' and module.app.is_enabled():
module.app.set_enabled(True)
except Exception as exception:
logger.exception('Exception while running post init for %s: %s',
module, exception)
if cfg.develop:
raise
logger.debug('App initialization completed.')
post_module_loading.send_robust(sender="module_loader")
def get_modules_to_load():
"""Get the list of modules to be loaded"""
global _modules_to_load