[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
This commit is contained in:
parent
f33711f49f
commit
252260c249
6 changed files with 26 additions and 26 deletions
|
@ -25,8 +25,11 @@ class Module(object):
|
||||||
(e.g. CPU utilization, disk usage, etc.) derive from
|
(e.g. CPU utilization, disk usage, etc.) derive from
|
||||||
this base class.
|
this base class.
|
||||||
"""
|
"""
|
||||||
def __init__(self, engine, widgets):
|
def __init__(self, engine, config={}, widgets=[]):
|
||||||
self.name = self.__module__.split(".")[-1]
|
self.name = self.__module__.split(".")[-1]
|
||||||
|
self._config = config
|
||||||
|
if not "name" in self._config:
|
||||||
|
self._config["name"] = self.name
|
||||||
self._widgets = []
|
self._widgets = []
|
||||||
if widgets:
|
if widgets:
|
||||||
self._widgets = widgets if isinstance(widgets, list) else [widgets]
|
self._widgets = widgets if isinstance(widgets, list) else [widgets]
|
||||||
|
@ -41,13 +44,8 @@ class Module(object):
|
||||||
|
|
||||||
def parameter(self, name, default=None):
|
def parameter(self, name, default=None):
|
||||||
"""Return the config parameter 'name' for this module"""
|
"""Return the config parameter 'name' for this module"""
|
||||||
name = "{}.{}".format(self._config_name, name)
|
name = "{}.{}".format(self._config["name"], name)
|
||||||
return self._config.get(name, default)
|
return self._config["config"].get(name, default)
|
||||||
|
|
||||||
def set_config(self, config, name):
|
|
||||||
"""Set the config for this module"""
|
|
||||||
self._config = config
|
|
||||||
self._config_name = name
|
|
||||||
|
|
||||||
class Engine(object):
|
class Engine(object):
|
||||||
"""Engine for driving the application
|
"""Engine for driving the application
|
||||||
|
@ -76,9 +74,10 @@ class Engine(object):
|
||||||
module = importlib.import_module("bumblebee.modules.{}".format(module_name))
|
module = importlib.import_module("bumblebee.modules.{}".format(module_name))
|
||||||
except ImportError as error:
|
except ImportError as error:
|
||||||
raise bumblebee.error.ModuleLoadError(error)
|
raise bumblebee.error.ModuleLoadError(error)
|
||||||
res = getattr(module, "Module")(self)
|
return getattr(module, "Module")(self, {
|
||||||
res.set_config(self._config, config_name)
|
"name": config_name,
|
||||||
return res
|
"config": self._config
|
||||||
|
})
|
||||||
|
|
||||||
def running(self):
|
def running(self):
|
||||||
"""Check whether the event loop is running"""
|
"""Check whether the event loop is running"""
|
||||||
|
|
|
@ -6,8 +6,8 @@ import psutil
|
||||||
import bumblebee.engine
|
import bumblebee.engine
|
||||||
|
|
||||||
class Module(bumblebee.engine.Module):
|
class Module(bumblebee.engine.Module):
|
||||||
def __init__(self, engine):
|
def __init__(self, engine, config):
|
||||||
super(Module, self).__init__(engine,
|
super(Module, self).__init__(engine, config,
|
||||||
bumblebee.output.Widget(full_text=self.utilization)
|
bumblebee.output.Widget(full_text=self.utilization)
|
||||||
)
|
)
|
||||||
self._utilization = psutil.cpu_percent(percpu=False)
|
self._utilization = psutil.cpu_percent(percpu=False)
|
||||||
|
|
|
@ -15,15 +15,12 @@ def default_format(module):
|
||||||
return default
|
return default
|
||||||
|
|
||||||
class Module(bumblebee.engine.Module):
|
class Module(bumblebee.engine.Module):
|
||||||
def __init__(self, engine):
|
def __init__(self, engine, config):
|
||||||
super(Module, self).__init__(engine,
|
super(Module, self).__init__(engine, config,
|
||||||
bumblebee.output.Widget(full_text=self.get_time)
|
bumblebee.output.Widget(full_text=self.get_time)
|
||||||
)
|
)
|
||||||
module = self.__module__.split(".")[-1]
|
module = self.__module__.split(".")[-1]
|
||||||
|
self._fmt = self.parameter("format", default_format(module))
|
||||||
self._fmt = default_format(module)
|
|
||||||
|
|
||||||
# self._fmt = self._config.parameter("format", default_format(module))
|
|
||||||
|
|
||||||
def get_time(self):
|
def get_time(self):
|
||||||
return datetime.datetime.now().strftime(self._fmt)
|
return datetime.datetime.now().strftime(self._fmt)
|
||||||
|
|
|
@ -5,8 +5,8 @@
|
||||||
import bumblebee.engine
|
import bumblebee.engine
|
||||||
|
|
||||||
class Module(bumblebee.engine.Module):
|
class Module(bumblebee.engine.Module):
|
||||||
def __init__(self, engine):
|
def __init__(self, engine, config):
|
||||||
super(Module, self).__init__(engine,
|
super(Module, self).__init__(engine, config,
|
||||||
bumblebee.output.Widget(full_text="test")
|
bumblebee.output.Widget(full_text="test")
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -5,15 +5,17 @@ import importlib
|
||||||
|
|
||||||
from bumblebee.modules.cpu import Module
|
from bumblebee.modules.cpu import Module
|
||||||
from bumblebee.engine import modules
|
from bumblebee.engine import modules
|
||||||
|
from bumblebee.config import Config
|
||||||
from tests.util import assertWidgetAttributes, MockEngine
|
from tests.util import assertWidgetAttributes, MockEngine
|
||||||
|
|
||||||
class TestGenericModules(unittest.TestCase):
|
class TestGenericModules(unittest.TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
engine = MockEngine()
|
engine = MockEngine()
|
||||||
|
config = Config()
|
||||||
self.objects = {}
|
self.objects = {}
|
||||||
for mod in modules():
|
for mod in modules():
|
||||||
cls = importlib.import_module("bumblebee.modules.{}".format(mod["name"]))
|
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):
|
def test_widgets(self):
|
||||||
for mod in self.objects:
|
for mod in self.objects:
|
||||||
|
|
|
@ -16,18 +16,20 @@ class TestModule(unittest.TestCase):
|
||||||
widgets=[self.widget, self.widget, self.widget]
|
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.anyConfigName = "cfg"
|
||||||
self.anotherConfigName = "cfg2"
|
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.anyKey = "some-parameter"
|
||||||
self.anyValue = "value"
|
self.anyValue = "value"
|
||||||
self.anotherValue = "another-value"
|
self.anotherValue = "another-value"
|
||||||
self.emptyKey = "i-do-not-exist"
|
self.emptyKey = "i-do-not-exist"
|
||||||
self.config.set("{}.{}".format(self.anyConfigName, self.anyKey), self.anyValue)
|
self.config.set("{}.{}".format(self.anyConfigName, self.anyKey), self.anyValue)
|
||||||
self.config.set("{}.{}".format(self.anotherConfigName, self.anyKey), self.anotherValue)
|
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):
|
def test_empty_widgets(self):
|
||||||
self.assertEquals(self.moduleWithoutWidgets.widgets(), [])
|
self.assertEquals(self.moduleWithoutWidgets.widgets(), [])
|
||||||
|
|
Loading…
Reference in a new issue