From a2c6214baa9d65dc5432f335541e66e0f2b84508 Mon Sep 17 00:00:00 2001 From: Tobi-wan Kenobi Date: Sun, 4 Dec 2016 11:09:10 +0100 Subject: [PATCH] [core/engine] Add module loading logic Allow the engine to load modules from the bumblebee/modules/ directory. see #23 --- bumblebee/engine.py | 26 ++++++++++++++++++++++++++ bumblebee/modules/__init__.py | 0 bumblebee/modules/test.py | 11 +++++++++++ runtests.sh | 10 ++++++---- tests/test_engine.py | 23 ++++++++++++++++++++++- 5 files changed, 65 insertions(+), 5 deletions(-) create mode 100644 bumblebee/modules/__init__.py create mode 100644 bumblebee/modules/test.py diff --git a/bumblebee/engine.py b/bumblebee/engine.py index f9996b6..0264083 100644 --- a/bumblebee/engine.py +++ b/bumblebee/engine.py @@ -1,5 +1,18 @@ """Core application engine""" +import importlib + +class Module(object): + """Module instance base class + + Objects of this type represent the modules that + the user configures. Concrete module implementations + (e.g. CPU utilization, disk usage, etc.) derive from + this base class. + """ + def __init__(self, engine): + pass + class Engine(object): """Engine for driving the application @@ -8,6 +21,19 @@ class Engine(object): """ def __init__(self, config): self._running = True + self._modules = [] + self.load_modules(config.modules()) + + def load_modules(self, modules): + """Load specified modules and return them as list""" + for module in modules: + self._modules.append(self.load_module(module["module"])) + return self._modules + + def load_module(self, module_name): + """Load specified module and return it as object""" + module = importlib.import_module("bumblebee.modules.{}".format(module_name)) + return getattr(module, "Module")(self) def running(self): """Check whether the event loop is running""" diff --git a/bumblebee/modules/__init__.py b/bumblebee/modules/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/bumblebee/modules/test.py b/bumblebee/modules/test.py new file mode 100644 index 0000000..028f742 --- /dev/null +++ b/bumblebee/modules/test.py @@ -0,0 +1,11 @@ +# pylint: disable=C0111,R0903 + +"""Test module""" + +import bumblebee.engine + +class Module(bumblebee.engine.Module): + def __init__(self, engine): + super(Module, self).__init__(engine) + +# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/runtests.sh b/runtests.sh index 8786182..cbc5a2a 100755 --- a/runtests.sh +++ b/runtests.sh @@ -2,10 +2,12 @@ test=$(which nosetests) -echo "testing $(python2 -V 2>&1)" +echo "testing with $(python2 -V 2>&1)" python2 $test --rednose -v tests/ -echo +if [ $? == 0 ]; then + echo -echo "testing $(python3 -V 2>&1)" -python3 $test --rednose -v tests/ + echo "testing with $(python3 -V 2>&1)" + python3 $test --rednose -v tests/ +fi diff --git a/tests/test_engine.py b/tests/test_engine.py index 0d81578..7a6152f 100644 --- a/tests/test_engine.py +++ b/tests/test_engine.py @@ -2,12 +2,33 @@ import unittest from bumblebee.engine import Engine +from bumblebee.config import Config class TestEngine(unittest.TestCase): def setUp(self): - self.engine = Engine(None) + self.engine = Engine(Config()) + self.testModule = "test" + self.testModuleSpec = "bumblebee.modules.{}".format(self.testModule) + self.testModules = [ + { "module": "test", "name": "a" }, + { "module": "test", "name": "b" }, + ] def test_stop(self): self.assertTrue(self.engine.running()) self.engine.stop() self.assertFalse(self.engine.running()) + + def test_load_module(self): + module = self.engine.load_module(self.testModule) + self.assertEquals(module.__module__, self.testModuleSpec) + + def test_load_modules(self): + modules = self.engine.load_modules(self.testModules) + self.assertEquals(len(modules), len(self.testModules)) + self.assertEquals( + [ module.__module__ for module in modules ], + [ self.testModuleSpec for module in modules ] + ) + +# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4