2020-01-19 16:06:21 +01:00
|
|
|
import importlib
|
|
|
|
import logging
|
|
|
|
|
2020-02-07 21:28:29 +01:00
|
|
|
import core.input
|
2020-02-15 14:04:53 +01:00
|
|
|
import core.widget
|
2020-02-07 21:28:29 +01:00
|
|
|
|
2020-01-19 16:06:21 +01:00
|
|
|
log = logging.getLogger(__name__)
|
2020-01-19 15:36:52 +01:00
|
|
|
|
2020-02-03 21:30:06 +01:00
|
|
|
def load(module_name, config=None):
|
2020-01-19 16:06:21 +01:00
|
|
|
try:
|
|
|
|
mod = importlib.import_module('modules.{}'.format(module_name))
|
|
|
|
except ImportError as error:
|
2020-02-15 14:04:53 +01:00
|
|
|
log.fatal('failed to import {}: {}'.format(module_name, error))
|
|
|
|
return Error(config, module_name, error)
|
2020-02-03 21:30:06 +01:00
|
|
|
return getattr(mod, 'Module')(config)
|
2020-01-19 15:36:52 +01:00
|
|
|
|
2020-02-07 21:28:29 +01:00
|
|
|
class Module(core.input.Object):
|
2020-02-03 21:30:06 +01:00
|
|
|
def __init__(self, config=None, widgets=[]):
|
2020-02-07 21:28:29 +01:00
|
|
|
super().__init__()
|
2020-02-03 21:30:06 +01:00
|
|
|
self._config = config
|
2020-01-26 14:06:09 +01:00
|
|
|
self._widgets = widgets if isinstance(widgets, list) else [ widgets ]
|
2020-02-04 21:09:11 +01:00
|
|
|
self._name = None
|
|
|
|
|
|
|
|
def parameter(self, key, default=None):
|
|
|
|
value = default
|
|
|
|
|
|
|
|
for prefix in [ self.name(), self.module_name() ]:
|
|
|
|
value = self._config.get('{}.{}'.format(prefix, key), value)
|
|
|
|
# TODO retrieve from config file
|
|
|
|
return value
|
2020-01-26 14:06:09 +01:00
|
|
|
|
2020-01-19 15:36:52 +01:00
|
|
|
def update(self):
|
|
|
|
pass
|
|
|
|
|
2020-02-03 21:30:06 +01:00
|
|
|
def name(self):
|
2020-02-04 21:09:11 +01:00
|
|
|
return self._name if self._name else self.module_name()
|
|
|
|
|
|
|
|
def module_name(self):
|
2020-02-03 21:30:06 +01:00
|
|
|
return self.__module__.split('.')[-1]
|
|
|
|
|
2020-01-26 14:06:09 +01:00
|
|
|
def widgets(self):
|
|
|
|
return self._widgets
|
|
|
|
|
2020-01-19 16:06:21 +01:00
|
|
|
class Error(Module):
|
2020-02-15 14:04:53 +01:00
|
|
|
def __init__(self, config, module, error):
|
|
|
|
super().__init__(config, core.widget.Widget(self.full_text))
|
|
|
|
self._module = module
|
|
|
|
self._error = error
|
|
|
|
|
|
|
|
def full_text(self):
|
|
|
|
return '{}: {}'.format(self._module, self._error)
|
2020-01-19 16:06:21 +01:00
|
|
|
|
2020-01-19 15:36:52 +01:00
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|