[core/module] Re-enable aliases

This commit is contained in:
tobi-wan-kenobi 2020-04-07 21:23:42 +02:00
parent be4e901e42
commit cde06bd33b
2 changed files with 14 additions and 11 deletions

View file

@ -12,16 +12,18 @@ except Exception as e:
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
def load(module_name, config=None): def load(module_name, config=core.config.Config([])):
error = None error = None
module_short, alias = (module_name.split(':') + [module_name])[0:2]
config.set('__alias__', alias)
for namespace in [ 'core', 'contrib' ]: for namespace in [ 'core', 'contrib' ]:
try: try:
mod = importlib.import_module('modules.{}.{}'.format(namespace, module_name)) mod = importlib.import_module('modules.{}.{}'.format(namespace, module_short))
return getattr(mod, 'Module')(config) return getattr(mod, 'Module')(config)
except ModuleNotFoundError as e: except ModuleNotFoundError as e:
log.fatal('failed to import {}: {}'.format(module_name, e)) log.fatal('failed to import {}: {}'.format(module_short, e))
except ImportError as e: except ImportError as e:
log.fatal('failed to import {}: {}'.format(module_name, e)) log.fatal('failed to import {}: {}'.format(module_short, e))
error = str(e) error = str(e)
if not error: if not error:
error = 'No such module' error = 'No such module'
@ -29,25 +31,27 @@ def load(module_name, config=None):
return Error(config=config, module=module_name, error=error) return Error(config=config, module=module_name, error=error)
class Module(core.input.Object): class Module(core.input.Object):
def __init__(self, config=None, widgets=[]): def __init__(self, config=core.config.Config([]), widgets=[]):
super().__init__() super().__init__()
self._config = config self.__config = config
self.__widgets = widgets if isinstance(widgets, list) else [ widgets ] self.__widgets = widgets if isinstance(widgets, list) else [ widgets ]
for widget in self.__widgets: for widget in self.__widgets:
widget.module(self) widget.module(self)
self.__name = None self.__name = None
self.alias = self.__config.get('__alias__', None)
print('ALIAS {}'.format(self.alias))
self.next_update = None self.next_update = None
def parameter(self, key, default=None): def parameter(self, key, default=None):
value = default value = default
for prefix in [ self.name(), self.module_name() ]: for prefix in [ self.name(), self.module_name(), self.alias ]:
value = self._config.get('{}.{}'.format(prefix, key), value) value = self.__config.get('{}.{}'.format(prefix, key), value)
# TODO retrieve from config file # TODO retrieve from config file
return value return value
def set(self, key, value): def set(self, key, value):
self._config.set('{}.{}'.format(self.name(), key), value) self.__config.set('{}.{}'.format(self.name(), key), value)
def update(self): def update(self):
pass pass
@ -56,7 +60,7 @@ class Module(core.input.Object):
try: try:
self.update() self.update()
except Exception as e: except Exception as e:
module = Error(config=self._config, module='error', error=str(e)) module = Error(config=self.__config, module='error', error=str(e))
self.__widgets = [module.widget()] self.__widgets = [module.widget()]
self.update = module.update self.update = module.update

View file

@ -14,7 +14,6 @@
- new themeing? (and add a "version" for backwards compat?) - new themeing? (and add a "version" for backwards compat?)
## Backwards-compatibility ## Backwards-compatibility
- aliases
- charts (braille) - charts (braille)
- minimize modules - minimize modules
- tkinter / popups - tkinter / popups