[core/module] Add config to module

Add access for all modules to the commandline parameters (called
"config" in bumblebee-status)
This commit is contained in:
Tobias Witek 2020-02-03 21:30:06 +01:00
parent 74e74bb78b
commit 3a531c359f
6 changed files with 22 additions and 11 deletions

View file

@ -10,7 +10,7 @@ def main():
output = core.output.i3() output = core.output.i3()
modules = [] modules = []
for module in config.modules(): for module in config.modules():
modules.append(core.module.load(module)) modules.append(core.module.load(module, config))
output.modules(modules) output.modules(modules)
output.draw('start') output.draw('start')
while True: while True:

View file

@ -3,21 +3,25 @@ import logging
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
def load(module_name): def load(module_name, config=None):
try: try:
mod = importlib.import_module('modules.{}'.format(module_name)) mod = importlib.import_module('modules.{}'.format(module_name))
except ImportError as error: except ImportError as error:
log.fatal('failed to import {}: {}'.format(module_name, str(error))) log.fatal('failed to import {}: {}'.format(module_name, str(error)))
return Error(module_name) return Error(module_name)
return getattr(mod, 'Module')() return getattr(mod, 'Module')(config)
class Module(object): class Module(object):
def __init__(self, widgets): def __init__(self, config=None, widgets=[]):
self._config = config
self._widgets = widgets if isinstance(widgets, list) else [ widgets ] self._widgets = widgets if isinstance(widgets, list) else [ widgets ]
def update(self): def update(self):
pass pass
def name(self):
return self.__module__.split('.')[-1]
def widgets(self): def widgets(self):
return self._widgets return self._widgets

View file

@ -8,7 +8,7 @@ import core.module
import core.widget import core.widget
class Module(core.module.Module): class Module(core.module.Module):
def __init__(self): def __init__(self, config=None):
super().__init__(core.widget.Widget(platform.release())) super().__init__(config, core.widget.Widget(platform.release()))
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4

View file

@ -7,7 +7,7 @@ import core.widget
import core.module import core.module
class Module(core.module.Module): class Module(core.module.Module):
def __init__(self): def __init__(self, config=None):
super().__init__(core.widget.Widget('test')) super().__init__(config, core.widget.Widget('test'))
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4

View file

@ -3,6 +3,9 @@ import unittest
import core.module import core.module
import core.widget import core.widget
class TestModule(core.module.Module):
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'
@ -14,12 +17,12 @@ class module(unittest.TestCase):
pass pass
def test_load_invalid_module(self): def test_load_invalid_module(self):
module = core.module.load(self._invalidModuleName) 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_load_valid_module(self):
module = core.module.load(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')
@ -35,4 +38,8 @@ class module(unittest.TestCase):
module = core.module.Module(widgets=[ self.someWidget, self.anotherWidget ]) module = core.module.Module(widgets=[ self.someWidget, self.anotherWidget ])
self.assertEqual([ self.someWidget, self.anotherWidget ], module.widgets()) self.assertEqual([ self.someWidget, self.anotherWidget ], module.widgets())
def test_module_Name(self):
module = TestModule([])
self.assertEqual('test_module', module.name(), 'module has wrong name')
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4

View file

@ -12,7 +12,7 @@ class i3(unittest.TestCase):
self.i3 = core.output.i3() self.i3 = core.output.i3()
widget = unittest.mock.MagicMock() widget = unittest.mock.MagicMock()
widget.full_text.return_value = "test" widget.full_text.return_value = "test"
self.someModule = TestModule([widget, widget, widget]) self.someModule = TestModule(widgets=[widget, widget, widget])
def tearDown(self): def tearDown(self):
pass pass