mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-02-18 08:33:41 +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:
parent
3e2c84dba2
commit
7f4c5f7410
@ -132,9 +132,7 @@ def setup_server():
|
||||
cherrypy.tree.mount(None, manual_url, config)
|
||||
logger.debug('Serving manual images %s on %s', manual_dir, manual_url)
|
||||
|
||||
for module_import_path in module_loader.loaded_modules:
|
||||
module = importlib.import_module(module_import_path)
|
||||
module_name = module_import_path.split('.')[-1]
|
||||
for module_name, module in module_loader.loaded_modules.items():
|
||||
module_path = os.path.dirname(module.__file__)
|
||||
static_dir = os.path.join(module_path, 'static')
|
||||
if not os.path.isdir(static_dir):
|
||||
|
||||
@ -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
|
||||
|
||||
@ -20,16 +20,14 @@ Plinth module for service discovery.
|
||||
"""
|
||||
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
import subprocess
|
||||
|
||||
from plinth import actions
|
||||
from plinth import action_utils
|
||||
from plinth import cfg
|
||||
from plinth import service as service_module
|
||||
|
||||
# pylint: disable=C0103
|
||||
|
||||
depends = ['plinth.modules.system']
|
||||
depends = ['system']
|
||||
|
||||
service = None
|
||||
|
||||
|
||||
@ -24,6 +24,4 @@ from .config import init
|
||||
|
||||
__all__ = ['config', 'init']
|
||||
|
||||
depends = ['plinth.modules.system',
|
||||
'plinth.modules.firewall',
|
||||
'plinth.modules.names']
|
||||
depends = ['system', 'firewall', 'names']
|
||||
|
||||
@ -19,7 +19,7 @@
|
||||
URLs for the Configuration module
|
||||
"""
|
||||
|
||||
from django.conf.urls import patterns, url
|
||||
from django.conf.urls import url
|
||||
|
||||
from . import config as views
|
||||
|
||||
|
||||
@ -22,13 +22,12 @@ Plinth module to configure system date and time
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
import subprocess
|
||||
|
||||
from plinth import actions
|
||||
from plinth import action_utils
|
||||
from plinth import cfg
|
||||
from plinth import service as service_module
|
||||
|
||||
|
||||
depends = ['plinth.modules.system']
|
||||
depends = ['system']
|
||||
|
||||
service = None
|
||||
|
||||
|
||||
@ -21,13 +21,12 @@ Plinth module to configure a Deluge web client.
|
||||
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from plinth import actions
|
||||
from plinth import action_utils
|
||||
from plinth import cfg
|
||||
from plinth import service as service_module
|
||||
|
||||
|
||||
depends = ['plinth.modules.apps']
|
||||
depends = ['apps']
|
||||
|
||||
service = None
|
||||
|
||||
|
||||
@ -25,7 +25,7 @@ from plinth import action_utils
|
||||
|
||||
__all__ = ['diagnostics', 'init']
|
||||
|
||||
depends = ['plinth.modules.system']
|
||||
depends = ['system']
|
||||
|
||||
|
||||
def diagnose():
|
||||
|
||||
@ -60,19 +60,14 @@ def index(request):
|
||||
@require_POST
|
||||
def module(request, module_name):
|
||||
"""Return diagnostics for a particular module."""
|
||||
found = False
|
||||
for module_import_path in module_loader.loaded_modules:
|
||||
if module_name == module_import_path.split('.')[-1]:
|
||||
found = True
|
||||
break
|
||||
|
||||
if not found:
|
||||
try:
|
||||
module = module_loader.loaded_modules[module_name]
|
||||
except KeyError:
|
||||
raise Http404('Module does not exist or not loaded')
|
||||
|
||||
loaded_module = importlib.import_module(module_import_path)
|
||||
results = []
|
||||
if hasattr(loaded_module, 'diagnose'):
|
||||
results = loaded_module.diagnose()
|
||||
if hasattr(module, 'diagnose'):
|
||||
results = module.diagnose()
|
||||
|
||||
return TemplateResponse(request, 'diagnostics_module.html',
|
||||
{'title': _('Diagnostic Test'),
|
||||
@ -110,17 +105,15 @@ def run_on_all_modules():
|
||||
'progress_percentage': 0}
|
||||
|
||||
modules = []
|
||||
for module_import_path in module_loader.loaded_modules:
|
||||
loaded_module = importlib.import_module(module_import_path)
|
||||
if not hasattr(loaded_module, 'diagnose'):
|
||||
for module_name, module in module_loader.loaded_modules.items():
|
||||
if not hasattr(module, 'diagnose'):
|
||||
continue
|
||||
|
||||
module_name = module_import_path.split('.')[-1]
|
||||
modules.append((module_name, loaded_module))
|
||||
modules.append((module_name, module))
|
||||
current_results['results'][module_name] = None
|
||||
|
||||
current_results['modules'] = modules
|
||||
for current_index, (module_name, loaded_module) in enumerate(modules):
|
||||
current_results['results'][module_name] = loaded_module.diagnose()
|
||||
for current_index, (module_name, module) in enumerate(modules):
|
||||
current_results['results'][module_name] = module.diagnose()
|
||||
current_results['progress_percentage'] = \
|
||||
int((current_index + 1) * 100 / len(modules))
|
||||
|
||||
@ -24,4 +24,4 @@ from .dynamicdns import init
|
||||
|
||||
__all__ = ['dynamicdns', 'init']
|
||||
|
||||
depends = ['plinth.modules.apps']
|
||||
depends = ['apps']
|
||||
|
||||
@ -24,4 +24,4 @@ from .firewall import init
|
||||
|
||||
__all__ = ['firewall', 'init']
|
||||
|
||||
depends = ['plinth.modules.system']
|
||||
depends = ['system']
|
||||
|
||||
@ -21,13 +21,12 @@ Plinth module to configure ikiwiki
|
||||
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from plinth import actions
|
||||
from plinth import action_utils
|
||||
from plinth import cfg
|
||||
from plinth import service as service_module
|
||||
|
||||
|
||||
depends = ['plinth.modules.apps']
|
||||
depends = ['apps']
|
||||
|
||||
service = None
|
||||
|
||||
|
||||
@ -20,19 +20,13 @@ Plinth module for using Let's Encrypt.
|
||||
"""
|
||||
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
import json
|
||||
|
||||
from plinth import actions
|
||||
from plinth import action_utils
|
||||
from plinth import cfg
|
||||
from plinth import service as service_module
|
||||
from plinth.modules import names
|
||||
|
||||
|
||||
depends = [
|
||||
'plinth.modules.apps',
|
||||
'plinth.modules.names'
|
||||
]
|
||||
depends = ['apps', 'names']
|
||||
|
||||
service = None
|
||||
|
||||
|
||||
@ -23,7 +23,7 @@ from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from plinth import cfg
|
||||
|
||||
depends = ['plinth.modules.system']
|
||||
depends = ['system']
|
||||
|
||||
|
||||
def init():
|
||||
|
||||
@ -21,13 +21,12 @@ Plinth module to configure Mumble server
|
||||
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from plinth import actions
|
||||
from plinth import action_utils
|
||||
from plinth import cfg
|
||||
from plinth import service as service_module
|
||||
|
||||
|
||||
depends = ['plinth.modules.apps']
|
||||
depends = ['apps']
|
||||
|
||||
service = None
|
||||
|
||||
|
||||
@ -31,7 +31,7 @@ SERVICES = (
|
||||
('ssh', _('SSH'), 22),
|
||||
)
|
||||
|
||||
depends = ['plinth.modules.system']
|
||||
depends = ['system']
|
||||
|
||||
domain_types = {}
|
||||
domains = {}
|
||||
|
||||
@ -31,7 +31,7 @@ from plinth import network
|
||||
|
||||
__all__ = ['networks', 'init']
|
||||
|
||||
depends = ['plinth.modules.system']
|
||||
depends = ['system']
|
||||
|
||||
logger = Logger(__name__)
|
||||
|
||||
|
||||
@ -27,7 +27,7 @@ from plinth import cfg
|
||||
from plinth import service as service_module
|
||||
|
||||
|
||||
depends = ['plinth.modules.apps']
|
||||
depends = ['apps']
|
||||
|
||||
service = None
|
||||
|
||||
|
||||
@ -26,7 +26,7 @@ from plinth import action_utils
|
||||
|
||||
__all__ = ['owncloud', 'init']
|
||||
|
||||
depends = ['plinth.modules.apps']
|
||||
depends = ['apps']
|
||||
|
||||
|
||||
def diagnose():
|
||||
|
||||
@ -26,7 +26,7 @@ from . import utils
|
||||
|
||||
__all__ = ['init']
|
||||
|
||||
depends = ['plinth.modules.apps', 'plinth.modules.names']
|
||||
depends = ['apps', 'names']
|
||||
|
||||
|
||||
def init():
|
||||
|
||||
@ -23,7 +23,7 @@ from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from plinth import cfg
|
||||
|
||||
depends = ['plinth.modules.system']
|
||||
depends = ['system']
|
||||
|
||||
|
||||
def init():
|
||||
|
||||
@ -20,7 +20,6 @@ Plinth module to configure Privoxy.
|
||||
"""
|
||||
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
import json
|
||||
|
||||
from plinth import actions
|
||||
from plinth import action_utils
|
||||
@ -28,7 +27,7 @@ from plinth import cfg
|
||||
from plinth import service as service_module
|
||||
|
||||
|
||||
depends = ['plinth.modules.apps']
|
||||
depends = ['apps']
|
||||
|
||||
service = None
|
||||
|
||||
@ -84,9 +83,7 @@ def diagnose_url_with_proxy():
|
||||
|
||||
result = action_utils.diagnose_url(url, kind=address['kind'], env=env)
|
||||
result[0] = _('Access {url} with proxy {proxy} on tcp{kind}') \
|
||||
.format(url=url, proxy=proxy, kind=address['kind'])
|
||||
.format(url=url, proxy=proxy, kind=address['kind'])
|
||||
results.append(result)
|
||||
|
||||
return results
|
||||
|
||||
|
||||
|
||||
@ -25,7 +25,7 @@ from plinth import action_utils
|
||||
from plinth import cfg
|
||||
from plinth import service as service_module
|
||||
|
||||
depends = ['plinth.modules.apps']
|
||||
depends = ['apps']
|
||||
|
||||
service = None
|
||||
|
||||
|
||||
@ -25,7 +25,7 @@ from plinth import action_utils
|
||||
from plinth import cfg
|
||||
from plinth import service as service_module
|
||||
|
||||
depends = ['plinth.modules.apps']
|
||||
depends = ['apps']
|
||||
|
||||
service = None
|
||||
|
||||
|
||||
@ -27,7 +27,7 @@ service = None
|
||||
|
||||
__all__ = ['init']
|
||||
|
||||
depends = ['plinth.modules.apps']
|
||||
depends = ['apps']
|
||||
|
||||
|
||||
def init():
|
||||
|
||||
@ -21,13 +21,11 @@ Plinth module to configure Roundcube.
|
||||
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from plinth import actions
|
||||
from plinth import action_utils
|
||||
from plinth import cfg
|
||||
from plinth import service as service_module
|
||||
|
||||
|
||||
depends = ['plinth.modules.apps']
|
||||
depends = ['apps']
|
||||
|
||||
|
||||
def init():
|
||||
|
||||
@ -26,7 +26,7 @@ from plinth import cfg
|
||||
from plinth import service as service_module
|
||||
|
||||
|
||||
depends = ['plinth.modules.apps']
|
||||
depends = ['apps']
|
||||
|
||||
service = None
|
||||
|
||||
|
||||
@ -32,7 +32,7 @@ from plinth.modules.names import SERVICES
|
||||
from plinth.signals import domain_added
|
||||
|
||||
|
||||
depends = ['plinth.modules.apps', 'plinth.modules.names']
|
||||
depends = ['apps', 'names']
|
||||
|
||||
socks_service = None
|
||||
bridge_service = None
|
||||
|
||||
@ -21,13 +21,12 @@ Plinth module to configure Transmission server
|
||||
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from plinth import actions
|
||||
from plinth import action_utils
|
||||
from plinth import cfg
|
||||
from plinth import service as service_module
|
||||
|
||||
|
||||
depends = ['plinth.modules.apps']
|
||||
depends = ['apps']
|
||||
|
||||
service = None
|
||||
|
||||
|
||||
@ -24,7 +24,7 @@ from django.utils.translation import ugettext_lazy as _
|
||||
from plinth import cfg
|
||||
|
||||
|
||||
depends = ['plinth.modules.system']
|
||||
depends = ['system']
|
||||
|
||||
|
||||
def init():
|
||||
|
||||
@ -20,14 +20,12 @@ Plinth module to manage users
|
||||
"""
|
||||
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
import json
|
||||
import subprocess
|
||||
|
||||
from plinth import cfg
|
||||
from plinth import actions
|
||||
from plinth import action_utils
|
||||
|
||||
depends = ['plinth.modules.system']
|
||||
depends = ['system']
|
||||
|
||||
|
||||
def init():
|
||||
|
||||
@ -20,7 +20,6 @@ Plinth module to configure XMPP server
|
||||
"""
|
||||
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
import json
|
||||
|
||||
from plinth import actions
|
||||
from plinth import action_utils
|
||||
@ -30,7 +29,7 @@ from plinth.signals import pre_hostname_change, post_hostname_change
|
||||
from plinth.signals import domainname_change
|
||||
|
||||
|
||||
depends = ['plinth.modules.apps']
|
||||
depends = ['apps']
|
||||
|
||||
service = None
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user