[core] Add support for module parameters

Parse '-p' parameters and make them accessible via 'parameter()' from
within a module (basically, restore the previous configuration
facility).

Still TODO: Add support for configuration files.
This commit is contained in:
Tobias Witek 2020-02-04 21:09:11 +01:00
parent 3a531c359f
commit 6adc497f68
4 changed files with 42 additions and 14 deletions

View file

@ -15,11 +15,23 @@ class Module(object):
def __init__(self, config=None, widgets=[]): def __init__(self, config=None, widgets=[]):
self._config = config self._config = config
self._widgets = widgets if isinstance(widgets, list) else [ widgets ] self._widgets = widgets if isinstance(widgets, list) else [ widgets ]
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
def update(self): def update(self):
pass pass
def name(self): def name(self):
return self._name if self._name else self.module_name()
def module_name(self):
return self.__module__.split('.')[-1] return self.__module__.split('.')[-1]
def widgets(self): def widgets(self):

View file

@ -9,7 +9,7 @@ class Widget(util.store.Store):
self._full_text = value self._full_text = value
else: else:
if callable(self._full_text): if callable(self._full_text):
return self._full_text(self) return self._full_text()
return self._full_text return self._full_text
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4

View file

@ -1,29 +1,29 @@
import unittest import unittest
import shlex
import core.module import core.module
import core.widget import core.widget
import core.config
class TestModule(core.module.Module): class TestModule(core.module.Module):
pass pass
class module(unittest.TestCase): class module(unittest.TestCase):
def setUp(self): def setUp(self):
self._invalidModuleName = 'invalid-module-name' self.invalidModuleName = 'invalid-module-name'
self._validModuleName = 'test' self.validModuleName = 'test'
self.someWidget = core.widget.Widget('randomeWidgetContent') self.someWidget = core.widget.Widget('randomeWidgetContent')
self.anotherWidget = core.widget.Widget('more Widget content') self.anotherWidget = core.widget.Widget('more Widget content')
def tearDown(self): def test_loadinvalid_module(self):
pass module = core.module.load(module_name=self.invalidModuleName)
def test_load_invalid_module(self):
module = core.module.load(module_name=self._invalidModuleName)
self.assertEqual('core.module', module.__class__.__module__, 'module must be a module object') self.assertEqual('core.module', module.__class__.__module__, 'module must be a module object')
self.assertEqual('Error', module.__class__.__name__, 'an invalid module must be a core.module.Error') self.assertEqual('Error', module.__class__.__name__, 'an invalid module must be a core.module.Error')
def test_load_valid_module(self): def test_loadvalid_module(self):
module = core.module.load(module_name=self._validModuleName) module = core.module.load(module_name=self.validModuleName)
self.assertEqual('modules.{}'.format(self._validModuleName), module.__class__.__module__, 'module must be a modules.<name> object') self.assertEqual('modules.{}'.format(self.validModuleName), module.__class__.__module__, 'module must be a modules.<name> object')
self.assertEqual('Module', module.__class__.__name__, 'a valid module must have a Module class') self.assertEqual('Module', module.__class__.__name__, 'a valid module must have a Module class')
def test_empty_widgets(self): def test_empty_widgets(self):
@ -39,7 +39,23 @@ class module(unittest.TestCase):
self.assertEqual([ self.someWidget, self.anotherWidget ], module.widgets()) self.assertEqual([ self.someWidget, self.anotherWidget ], module.widgets())
def test_module_Name(self): def test_module_Name(self):
module = TestModule([]) module = TestModule()
self.assertEqual('test_module', module.name(), 'module has wrong name') self.assertEqual('test_module', module.name(), 'module has wrong name')
self.assertEqual('test_module', module.module_name(), 'module has wrong name')
def testvalid_parameter(self):
cfg = core.config.Config(shlex.split('-p test_module.foo=5'))
module = TestModule(config=cfg)
self.assertEqual(5, int(module.parameter('foo')))
def test_default_parameter(self):
cfg = core.config.Config([])
module = TestModule(config=cfg)
self.assertEqual('default', module.parameter('foo', 'default'))
def test_default_is_none(self):
cfg = core.config.Config([])
module = TestModule(config=cfg)
self.assertEqual(None, module.parameter('foo'))
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4

View file

@ -28,11 +28,11 @@ class widget(unittest.TestCase):
def test_callable_fulltext(self): def test_callable_fulltext(self):
newWidget = core.widget.Widget(full_text=self.someCallback) newWidget = core.widget.Widget(full_text=self.someCallback)
self.assertEqual(newWidget.full_text(), self.callbackReturnValue) self.assertEqual(newWidget.full_text(), self.callbackReturnValue)
self.someCallback.assert_called_once_with(unittest.mock.ANY) self.someCallback.assert_called_once_with()
def test_set_callable_fulltext(self): def test_set_callable_fulltext(self):
self.someWidget.full_text(self.someCallback) self.someWidget.full_text(self.someCallback)
self.assertEqual(self.someWidget.full_text(), self.callbackReturnValue) self.assertEqual(self.someWidget.full_text(), self.callbackReturnValue)
self.someCallback.assert_called_once_with(unittest.mock.ANY) self.someCallback.assert_called_once_with()
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4