From 252260c249410af2f8b0c32feed9f926b46a9dae Mon Sep 17 00:00:00 2001 From: Tobi-wan Kenobi Date: Fri, 9 Dec 2016 08:23:53 +0100 Subject: [PATCH] [modules/datetime] Use parameter functionality to get format Make the format string of the datetime module configurable using the new parameter() method in the module. Also, restructured the setting of the config information a bit so that the parameter() method can be used in the constructor of a module. see #23 --- bumblebee/engine.py | 21 ++++++++++----------- bumblebee/modules/cpu.py | 4 ++-- bumblebee/modules/datetime.py | 9 +++------ bumblebee/modules/test.py | 4 ++-- tests/modules/test_modules.py | 4 +++- tests/test_module.py | 10 ++++++---- 6 files changed, 26 insertions(+), 26 deletions(-) diff --git a/bumblebee/engine.py b/bumblebee/engine.py index d219b2e..99cea34 100644 --- a/bumblebee/engine.py +++ b/bumblebee/engine.py @@ -25,8 +25,11 @@ class Module(object): (e.g. CPU utilization, disk usage, etc.) derive from this base class. """ - def __init__(self, engine, widgets): + def __init__(self, engine, config={}, widgets=[]): self.name = self.__module__.split(".")[-1] + self._config = config + if not "name" in self._config: + self._config["name"] = self.name self._widgets = [] if widgets: self._widgets = widgets if isinstance(widgets, list) else [widgets] @@ -41,13 +44,8 @@ class Module(object): def parameter(self, name, default=None): """Return the config parameter 'name' for this module""" - name = "{}.{}".format(self._config_name, name) - return self._config.get(name, default) - - def set_config(self, config, name): - """Set the config for this module""" - self._config = config - self._config_name = name + name = "{}.{}".format(self._config["name"], name) + return self._config["config"].get(name, default) class Engine(object): """Engine for driving the application @@ -76,9 +74,10 @@ class Engine(object): module = importlib.import_module("bumblebee.modules.{}".format(module_name)) except ImportError as error: raise bumblebee.error.ModuleLoadError(error) - res = getattr(module, "Module")(self) - res.set_config(self._config, config_name) - return res + return getattr(module, "Module")(self, { + "name": config_name, + "config": self._config + }) def running(self): """Check whether the event loop is running""" diff --git a/bumblebee/modules/cpu.py b/bumblebee/modules/cpu.py index dbee32e..2951ad8 100644 --- a/bumblebee/modules/cpu.py +++ b/bumblebee/modules/cpu.py @@ -6,8 +6,8 @@ import psutil import bumblebee.engine class Module(bumblebee.engine.Module): - def __init__(self, engine): - super(Module, self).__init__(engine, + def __init__(self, engine, config): + super(Module, self).__init__(engine, config, bumblebee.output.Widget(full_text=self.utilization) ) self._utilization = psutil.cpu_percent(percpu=False) diff --git a/bumblebee/modules/datetime.py b/bumblebee/modules/datetime.py index fc11a89..7d776ad 100644 --- a/bumblebee/modules/datetime.py +++ b/bumblebee/modules/datetime.py @@ -15,15 +15,12 @@ def default_format(module): return default class Module(bumblebee.engine.Module): - def __init__(self, engine): - super(Module, self).__init__(engine, + def __init__(self, engine, config): + super(Module, self).__init__(engine, config, bumblebee.output.Widget(full_text=self.get_time) ) module = self.__module__.split(".")[-1] - - self._fmt = default_format(module) - -# self._fmt = self._config.parameter("format", default_format(module)) + self._fmt = self.parameter("format", default_format(module)) def get_time(self): return datetime.datetime.now().strftime(self._fmt) diff --git a/bumblebee/modules/test.py b/bumblebee/modules/test.py index 61af7e9..ad07faa 100644 --- a/bumblebee/modules/test.py +++ b/bumblebee/modules/test.py @@ -5,8 +5,8 @@ import bumblebee.engine class Module(bumblebee.engine.Module): - def __init__(self, engine): - super(Module, self).__init__(engine, + def __init__(self, engine, config): + super(Module, self).__init__(engine, config, bumblebee.output.Widget(full_text="test") ) diff --git a/tests/modules/test_modules.py b/tests/modules/test_modules.py index 7cdb48b..d08e783 100644 --- a/tests/modules/test_modules.py +++ b/tests/modules/test_modules.py @@ -5,15 +5,17 @@ import importlib from bumblebee.modules.cpu import Module from bumblebee.engine import modules +from bumblebee.config import Config from tests.util import assertWidgetAttributes, MockEngine class TestGenericModules(unittest.TestCase): def setUp(self): engine = MockEngine() + config = Config() self.objects = {} for mod in modules(): cls = importlib.import_module("bumblebee.modules.{}".format(mod["name"])) - self.objects[mod["name"]] = getattr(cls, "Module")(engine) + self.objects[mod["name"]] = getattr(cls, "Module")(engine, { "config": config }) def test_widgets(self): for mod in self.objects: diff --git a/tests/test_module.py b/tests/test_module.py index a2e62a7..44a39af 100644 --- a/tests/test_module.py +++ b/tests/test_module.py @@ -16,18 +16,20 @@ class TestModule(unittest.TestCase): widgets=[self.widget, self.widget, self.widget] ) - self.anyModule = Module(engine=None, widgets = self.widget) - self.anotherModule = Module(engine=None, widgets = self.widget) self.anyConfigName = "cfg" self.anotherConfigName = "cfg2" + self.anyModule = Module(engine=None, widgets=self.widget, config={ + "name": self.anyConfigName, "config": self.config + }) + self.anotherModule = Module(engine=None, widgets=self.widget, config={ + "name": self.anotherConfigName, "config": self.config + }) self.anyKey = "some-parameter" self.anyValue = "value" self.anotherValue = "another-value" self.emptyKey = "i-do-not-exist" self.config.set("{}.{}".format(self.anyConfigName, self.anyKey), self.anyValue) self.config.set("{}.{}".format(self.anotherConfigName, self.anyKey), self.anotherValue) - self.anyModule.set_config(self.config, self.anyConfigName) - self.anotherModule.set_config(self.config, self.anotherConfigName) def test_empty_widgets(self): self.assertEquals(self.moduleWithoutWidgets.widgets(), [])