mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-11-29 08:25:10 +00:00
module_loader, app: Move app init to app module
- Don't try to get the depends from module level and sort modules based on that. - Instead after all App instances are created, sort the apps based on app.info.depends and app.info.is_essential. - Print message that apps have been initialized instead of printing before they are initialized. The correct order of apps is only known after they have been initialized and sorted. - Avoid circular import on module_loader and setup. Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org> Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
1 parent
3421469825
commit
603b63bbac
7 files changed
+198
-109
No files matched your search
+4
-98
@@ -5,15 +5,14 @@ Discover, load and manage FreedomBox applications.
|
||||
|
||||
import collections
|
||||
import importlib
|
||||
import inspect
|
||||
import logging
|
||||
import pathlib
|
||||
import re
|
||||
|
||||
import django
|
||||
|
||||
from plinth import app, cfg, setup
|
||||
from plinth.signals import post_module_loading, pre_module_loading
|
||||
from plinth import cfg
|
||||
from plinth.signals import pre_module_loading
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -34,63 +33,17 @@ def load_modules():
|
||||
import them from modules directory.
|
||||
"""
|
||||
pre_module_loading.send_robust(sender="module_loader")
|
||||
modules = {}
|
||||
for module_import_path in get_modules_to_load():
|
||||
module_name = module_import_path.split('.')[-1]
|
||||
try:
|
||||
modules[module_name] = importlib.import_module(module_import_path)
|
||||
loaded_modules[module_name] = importlib.import_module(
|
||||
module_import_path)
|
||||
except Exception as exception:
|
||||
logger.exception('Could not import %s: %s', module_import_path,
|
||||
exception)
|
||||
if cfg.develop:
|
||||
raise
|
||||
|
||||
ordered_modules = []
|
||||
remaining_modules = dict(modules) # Make a copy
|
||||
# Place all essential modules ahead of others in module load order
|
||||
sorted_modules = sorted(
|
||||
modules, key=lambda module: not modules[module].app.info.is_essential)
|
||||
for module_name in sorted_modules:
|
||||
if module_name not in remaining_modules:
|
||||
continue
|
||||
|
||||
module = remaining_modules.pop(module_name)
|
||||
try:
|
||||
_insert_modules(module_name, module, remaining_modules,
|
||||
ordered_modules)
|
||||
except KeyError:
|
||||
logger.error('Unsatified dependency for module - %s', module_name)
|
||||
|
||||
for module_name in ordered_modules:
|
||||
loaded_modules[module_name] = modules[module_name]
|
||||
|
||||
|
||||
def _insert_modules(module_name, module, remaining_modules, ordered_modules):
|
||||
"""Insert modules into a list based on dependency order"""
|
||||
if module_name in ordered_modules:
|
||||
return
|
||||
|
||||
dependencies = []
|
||||
try:
|
||||
dependencies = module.depends
|
||||
except AttributeError:
|
||||
pass
|
||||
|
||||
for dependency in dependencies:
|
||||
if dependency in ordered_modules:
|
||||
continue
|
||||
|
||||
try:
|
||||
module = remaining_modules.pop(dependency)
|
||||
except KeyError:
|
||||
logger.error('Not found or circular dependency - %s, %s',
|
||||
module_name, dependency)
|
||||
raise
|
||||
|
||||
_insert_modules(dependency, module, remaining_modules, ordered_modules)
|
||||
|
||||
ordered_modules.append(module_name)
|
||||
|
||||
|
||||
def _include_module_urls(module_import_path, module_name):
|
||||
"""Include the module's URLs in global project URLs list"""
|
||||
@@ -107,53 +60,6 @@ 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"""
|
||||
|
||||
# Perform setup related initialization on the module
|
||||
setup.init(module_name, module)
|
||||
|
||||
try:
|
||||
module_classes = inspect.getmembers(module, inspect.isclass)
|
||||
app_class = [
|
||||
cls for cls in module_classes if issubclass(cls[1], app.App)
|
||||
]
|
||||
if module_classes and app_class:
|
||||
module.app = app_class[0][1]()
|
||||
except Exception as exception:
|
||||
logger.exception('Exception while running init for %s: %s', module,
|
||||
exception)
|
||||
if cfg.develop:
|
||||
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 not module.app.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
|
||||
|
||||
Reference in new issue
Block a user