[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:
parent
3a531c359f
commit
6adc497f68
4 changed files with 42 additions and 14 deletions
|
@ -15,11 +15,23 @@ class Module(object):
|
|||
def __init__(self, config=None, widgets=[]):
|
||||
self._config = config
|
||||
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):
|
||||
pass
|
||||
|
||||
def name(self):
|
||||
return self._name if self._name else self.module_name()
|
||||
|
||||
def module_name(self):
|
||||
return self.__module__.split('.')[-1]
|
||||
|
||||
def widgets(self):
|
||||
|
|
|
@ -9,7 +9,7 @@ class Widget(util.store.Store):
|
|||
self._full_text = value
|
||||
else:
|
||||
if callable(self._full_text):
|
||||
return self._full_text(self)
|
||||
return self._full_text()
|
||||
return self._full_text
|
||||
|
||||
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
||||
|
|
|
@ -1,29 +1,29 @@
|
|||
import unittest
|
||||
|
||||
import shlex
|
||||
|
||||
import core.module
|
||||
import core.widget
|
||||
import core.config
|
||||
|
||||
class TestModule(core.module.Module):
|
||||
pass
|
||||
|
||||
class module(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self._invalidModuleName = 'invalid-module-name'
|
||||
self._validModuleName = 'test'
|
||||
self.invalidModuleName = 'invalid-module-name'
|
||||
self.validModuleName = 'test'
|
||||
self.someWidget = core.widget.Widget('randomeWidgetContent')
|
||||
self.anotherWidget = core.widget.Widget('more Widget content')
|
||||
|
||||
def tearDown(self):
|
||||
pass
|
||||
|
||||
def test_load_invalid_module(self):
|
||||
module = core.module.load(module_name=self._invalidModuleName)
|
||||
def test_loadinvalid_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('Error', module.__class__.__name__, 'an invalid module must be a core.module.Error')
|
||||
|
||||
def test_load_valid_module(self):
|
||||
module = core.module.load(module_name=self._validModuleName)
|
||||
self.assertEqual('modules.{}'.format(self._validModuleName), module.__class__.__module__, 'module must be a modules.<name> object')
|
||||
def test_loadvalid_module(self):
|
||||
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('Module', module.__class__.__name__, 'a valid module must have a Module class')
|
||||
|
||||
def test_empty_widgets(self):
|
||||
|
@ -39,7 +39,23 @@ class module(unittest.TestCase):
|
|||
self.assertEqual([ self.someWidget, self.anotherWidget ], module.widgets())
|
||||
|
||||
def test_module_Name(self):
|
||||
module = TestModule([])
|
||||
module = TestModule()
|
||||
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
|
||||
|
|
|
@ -28,11 +28,11 @@ class widget(unittest.TestCase):
|
|||
def test_callable_fulltext(self):
|
||||
newWidget = core.widget.Widget(full_text=self.someCallback)
|
||||
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):
|
||||
self.someWidget.full_text(self.someCallback)
|
||||
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
|
||||
|
|
Loading…
Reference in a new issue