[core/engine] Add aliasing mechanism to modules

Allow modules to define aliases. This replaces the symlink mechanism
that was in place previously, because it was a bit ugly (and confused
code climate).

see #23
This commit is contained in:
Tobi-wan Kenobi 2016-12-11 07:18:06 +01:00
parent 8f759e6134
commit 2cc2cf8282
5 changed files with 18 additions and 13 deletions

View file

@ -1,10 +1,6 @@
engines:
duplication:
enabled: true
exclude_fingerprints:
- 729e672cc5eafa97b4ff7b4487b43e73
- b6bcd0bad16e901a7e93b31776477fde
- 39aa354fb2009c102f8b297a4eade693
config:
languages:
- python

View file

@ -26,11 +26,9 @@ class Module(object):
this base class.
"""
def __init__(self, engine, config={}, widgets=[]):
self.name = self.__module__.split(".")[-1]
self.name = config.get("name", self.__module__.split(".")[-1])
self._config = config
if "name" not in self._config:
self._config["name"] = self.name
self.id = self._config["name"]
self.id = self.name
self._widgets = []
if widgets:
self._widgets = widgets if isinstance(widgets, list) else [widgets]
@ -50,7 +48,7 @@ class Module(object):
def parameter(self, name, default=None):
"""Return the config parameter 'name' for this module"""
name = "{}.{}".format(self._config["name"], name)
name = "{}.{}".format(self.name, name)
return self._config["config"].get(name, default)
class Engine(object):
@ -65,6 +63,7 @@ class Engine(object):
self._running = True
self._modules = []
self.input = inp
self._aliases = self._read_aliases()
self.load_modules(config.modules())
self.input.register_callback(None, bumblebee.input.WHEEL_UP,
@ -80,8 +79,19 @@ class Engine(object):
self._modules.append(self._load_module(module["module"], module["name"]))
return self._modules
def _read_aliases(self):
result = {}
for module in all_modules():
mod = importlib.import_module("bumblebee.modules.{}".format(module["name"]))
for alias in getattr(mod, "ALIASES", []):
result[alias] = module["name"]
return result
def _load_module(self, module_name, config_name=None):
"""Load specified module and return it as object"""
if module_name in self._aliases:
config_name is config_name if config_name else module_name
module_name = self._aliases[module_name]
if config_name is None:
config_name = module_name
try:

View file

@ -1 +0,0 @@
datetime.py

View file

@ -12,6 +12,8 @@ from __future__ import absolute_import
import datetime
import bumblebee.engine
ALIASES = [ "date", "time" ]
def default_format(module):
default = "%x %X"
if module == "date":
@ -25,8 +27,7 @@ class Module(bumblebee.engine.Module):
super(Module, self).__init__(engine, config,
bumblebee.output.Widget(full_text=self.get_time)
)
module = self.__module__.split(".")[-1]
self._fmt = self.parameter("format", default_format(module))
self._fmt = self.parameter("format", default_format(self.name))
def get_time(self):
return datetime.datetime.now().strftime(self._fmt)

View file

@ -1 +0,0 @@
datetime.py