From 3a531c359ffeb416264dce96313120009577c2a7 Mon Sep 17 00:00:00 2001 From: Tobias Witek Date: Mon, 3 Feb 2020 21:30:06 +0100 Subject: [PATCH] [core/module] Add config to module Add access for all modules to the commandline parameters (called "config" in bumblebee-status) --- bumblebee-status | 2 +- core/module.py | 10 +++++++--- modules/kernel.py | 4 ++-- modules/test.py | 4 ++-- tests/core/test_module.py | 11 +++++++++-- tests/core/test_output.py | 2 +- 6 files changed, 22 insertions(+), 11 deletions(-) diff --git a/bumblebee-status b/bumblebee-status index 29e7876..bca1a01 100755 --- a/bumblebee-status +++ b/bumblebee-status @@ -10,7 +10,7 @@ def main(): output = core.output.i3() modules = [] for module in config.modules(): - modules.append(core.module.load(module)) + modules.append(core.module.load(module, config)) output.modules(modules) output.draw('start') while True: diff --git a/core/module.py b/core/module.py index 31c2c2a..60b2aba 100644 --- a/core/module.py +++ b/core/module.py @@ -3,21 +3,25 @@ import logging log = logging.getLogger(__name__) -def load(module_name): +def load(module_name, config=None): try: mod = importlib.import_module('modules.{}'.format(module_name)) except ImportError as error: log.fatal('failed to import {}: {}'.format(module_name, str(error))) return Error(module_name) - return getattr(mod, 'Module')() + return getattr(mod, 'Module')(config) 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 ] def update(self): pass + def name(self): + return self.__module__.split('.')[-1] + def widgets(self): return self._widgets diff --git a/modules/kernel.py b/modules/kernel.py index 43f435f..99fc436 100644 --- a/modules/kernel.py +++ b/modules/kernel.py @@ -8,7 +8,7 @@ import core.module import core.widget class Module(core.module.Module): - def __init__(self): - super().__init__(core.widget.Widget(platform.release())) + def __init__(self, config=None): + super().__init__(config, core.widget.Widget(platform.release())) # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/modules/test.py b/modules/test.py index 3d22ddb..ef0dde6 100644 --- a/modules/test.py +++ b/modules/test.py @@ -7,7 +7,7 @@ import core.widget import core.module class Module(core.module.Module): - def __init__(self): - super().__init__(core.widget.Widget('test')) + def __init__(self, config=None): + super().__init__(config, core.widget.Widget('test')) # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/tests/core/test_module.py b/tests/core/test_module.py index b28eca8..ae33b74 100644 --- a/tests/core/test_module.py +++ b/tests/core/test_module.py @@ -3,6 +3,9 @@ import unittest import core.module import core.widget +class TestModule(core.module.Module): + pass + class module(unittest.TestCase): def setUp(self): self._invalidModuleName = 'invalid-module-name' @@ -14,12 +17,12 @@ class module(unittest.TestCase): pass 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('Error', module.__class__.__name__, 'an invalid module must be a core.module.Error') 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. object') 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 ]) 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 diff --git a/tests/core/test_output.py b/tests/core/test_output.py index c751ed9..88be280 100644 --- a/tests/core/test_output.py +++ b/tests/core/test_output.py @@ -12,7 +12,7 @@ class i3(unittest.TestCase): self.i3 = core.output.i3() widget = unittest.mock.MagicMock() widget.full_text.return_value = "test" - self.someModule = TestModule([widget, widget, widget]) + self.someModule = TestModule(widgets=[widget, widget, widget]) def tearDown(self): pass