mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-11-29 08:25:10 +00:00
Make app names as module identifiers
- The last part of the module import path is the module name. This also becomes the Django app name. Apps names have to be unique. Hence, there is no scope for two different modules with same name but different load path to exist in the project. - Most uses of list of loaded modules are dealing with app names instead of full module load path. This is due to the fact that Django deals with app names and not module paths. - It is also somewhat clumsy to access a loaded module as we are re-importing every time to get access module. - Simplify all of the above by using app names are module identifiers and maintaing an ordered dictionary of app names to loadded modules. - Remove unused imports. - Minor styling fixes.
This commit is contained in:
32 files changed
+61
-91
No files matched your search
+20
-18
@@ -19,6 +19,7 @@
|
||||
Discover, load and manage Plinth modules
|
||||
"""
|
||||
|
||||
import collections
|
||||
import django
|
||||
import importlib
|
||||
import logging
|
||||
@@ -29,9 +30,9 @@ from plinth import cfg
|
||||
from plinth import urls
|
||||
from plinth.signals import pre_module_loading, post_module_loading
|
||||
|
||||
LOGGER = logging.getLogger(__name__)
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
loaded_modules = []
|
||||
loaded_modules = collections.OrderedDict()
|
||||
_modules_to_load = None
|
||||
|
||||
|
||||
@@ -42,16 +43,18 @@ def load_modules():
|
||||
"""
|
||||
pre_module_loading.send_robust(sender="module_loader")
|
||||
modules = {}
|
||||
for module_name in get_modules_to_load():
|
||||
LOGGER.info('Importing %s', module_name)
|
||||
for module_import_path in get_modules_to_load():
|
||||
logger.info('Importing %s', module_import_path)
|
||||
module_name = module_import_path.split('.')[-1]
|
||||
try:
|
||||
modules[module_name] = importlib.import_module(module_name)
|
||||
modules[module_name] = importlib.import_module(module_import_path)
|
||||
except Exception as exception:
|
||||
LOGGER.exception('Could not import %s: %s', module_name, exception)
|
||||
logger.exception('Could not import %s: %s', module_import_path,
|
||||
exception)
|
||||
if cfg.debug:
|
||||
raise
|
||||
|
||||
_include_module_urls(module_name)
|
||||
_include_module_urls(module_import_path, module_name)
|
||||
|
||||
ordered_modules = []
|
||||
remaining_modules = dict(modules) # Make a copy
|
||||
@@ -64,14 +67,14 @@ def load_modules():
|
||||
_insert_modules(module_name, module, remaining_modules,
|
||||
ordered_modules)
|
||||
except KeyError:
|
||||
LOGGER.error('Unsatified dependency for module - %s',
|
||||
logger.error('Unsatified dependency for module - %s',
|
||||
module_name)
|
||||
|
||||
LOGGER.debug('Module load order - %s', ordered_modules)
|
||||
logger.debug('Module load order - %s', ordered_modules)
|
||||
|
||||
for module_name in ordered_modules:
|
||||
_initialize_module(modules[module_name])
|
||||
loaded_modules.append(module_name)
|
||||
loaded_modules[module_name] = modules[module_name]
|
||||
|
||||
post_module_loading.send_robust(sender="module_loader")
|
||||
|
||||
@@ -94,7 +97,7 @@ def _insert_modules(module_name, module, remaining_modules, ordered_modules):
|
||||
try:
|
||||
module = remaining_modules.pop(dependency)
|
||||
except KeyError:
|
||||
LOGGER.error('Not found or circular dependency - %s, %s',
|
||||
logger.error('Not found or circular dependency - %s, %s',
|
||||
module_name, dependency)
|
||||
raise
|
||||
|
||||
@@ -103,16 +106,15 @@ def _insert_modules(module_name, module, remaining_modules, ordered_modules):
|
||||
ordered_modules.append(module_name)
|
||||
|
||||
|
||||
def _include_module_urls(module_name):
|
||||
def _include_module_urls(module_import_path, module_name):
|
||||
"""Include the module's URLs in global project URLs list"""
|
||||
namespace = module_name.split('.')[-1]
|
||||
url_module = module_name + '.urls'
|
||||
url_module = module_import_path + '.urls'
|
||||
try:
|
||||
urls.urlpatterns += [
|
||||
django.conf.urls.url(
|
||||
r'', django.conf.urls.include(url_module, namespace))]
|
||||
r'', django.conf.urls.include(url_module, module_name))]
|
||||
except ImportError:
|
||||
LOGGER.debug('No URLs for %s', module_name)
|
||||
logger.debug('No URLs for %s', module_name)
|
||||
if cfg.debug:
|
||||
raise
|
||||
|
||||
@@ -122,13 +124,13 @@ def _initialize_module(module):
|
||||
try:
|
||||
init = module.init
|
||||
except AttributeError:
|
||||
LOGGER.debug('No init() for module - %s', module.__name__)
|
||||
logger.debug('No init() for module - %s', module.__name__)
|
||||
return
|
||||
|
||||
try:
|
||||
init()
|
||||
except Exception as exception:
|
||||
LOGGER.exception('Exception while running init for %s: %s',
|
||||
logger.exception('Exception while running init for %s: %s',
|
||||
module, exception)
|
||||
if cfg.debug:
|
||||
raise
|
||||
|
||||
Reference in new issue
Block a user