From 8855f1155be9bfbc52f35a11520a9f8164ede29b Mon Sep 17 00:00:00 2001 From: Tobi-wan Kenobi Date: Sun, 4 Dec 2016 16:23:44 +0100 Subject: [PATCH] [core/errors] Add custom exceptions Add custom exceptions and add error handling to the engine's module loading logic. I.e. when a non-existent module is loaded, an exception is thrown now. see #23 --- bumblebee-status | 6 +++++- bumblebee/engine.py | 8 +++++++- bumblebee/error.py | 11 +++++++++++ tests/test_engine.py | 10 ++++++++++ 4 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 bumblebee/error.py diff --git a/bumblebee-status b/bumblebee-status index 4d1cfe6..fcb5679 100755 --- a/bumblebee-status +++ b/bumblebee-status @@ -16,6 +16,10 @@ def main(): engine.run() if __name__ == "__main__": - main() + try: + main() + except bumblebee.error.BaseError as error: + sys.stderr.write("fatal: {}\n".format(error)) + sys.exit(1) # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/bumblebee/engine.py b/bumblebee/engine.py index ddf6396..3c07d98 100644 --- a/bumblebee/engine.py +++ b/bumblebee/engine.py @@ -1,6 +1,8 @@ """Core application engine""" +import time import importlib +import bumblebee.error class Module(object): """Module instance base class @@ -33,7 +35,10 @@ class Engine(object): def load_module(self, module_name): """Load specified module and return it as object""" - module = importlib.import_module("bumblebee.modules.{}".format(module_name)) + try: + module = importlib.import_module("bumblebee.modules.{}".format(module_name)) + except ImportError as error: + raise bumblebee.error.ModuleLoadError(error) return getattr(module, "Module")(self) def running(self): @@ -53,6 +58,7 @@ class Engine(object): widgets += module.widgets() self._output.draw(widgets) self._output.flush() + time.sleep(1) self._output.stop() diff --git a/bumblebee/error.py b/bumblebee/error.py new file mode 100644 index 0000000..b07cc24 --- /dev/null +++ b/bumblebee/error.py @@ -0,0 +1,11 @@ +"""Collection of all exceptions raised by this tool""" + +class BaseError(Exception): + """Base class for all exceptions generated by this tool""" + pass + +class ModuleLoadError(BaseError): + """Raised whenever loading a module fails""" + pass + +# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/tests/test_engine.py b/tests/test_engine.py index cc5c69f..2cb5ea3 100644 --- a/tests/test_engine.py +++ b/tests/test_engine.py @@ -1,6 +1,7 @@ # pylint: disable=C0103,C0111 import unittest +from bumblebee.error import ModuleLoadError from bumblebee.engine import Engine from bumblebee.config import Config @@ -8,6 +9,7 @@ class TestEngine(unittest.TestCase): def setUp(self): self.engine = Engine(Config()) self.testModule = "test" + self.invalidModule = "no-such-module" self.testModuleSpec = "bumblebee.modules.{}".format(self.testModule) self.testModules = [ {"module": "test", "name": "a"}, @@ -23,6 +25,14 @@ class TestEngine(unittest.TestCase): module = self.engine.load_module(self.testModule) self.assertEquals(module.__module__, self.testModuleSpec) + def test_load_invalid_module(self): + with self.assertRaises(ModuleLoadError): + self.engine.load_module(self.invalidModule) + + def test_load_none(self): + with self.assertRaises(ModuleLoadError): + self.engine.load_module(None) + def test_load_modules(self): modules = self.engine.load_modules(self.testModules) self.assertEquals(len(modules), len(self.testModules))