[modules] Separate modules into core & contrib
Also, improve errors when importing a module fails. Also, add more tests.
This commit is contained in:
parent
47950240d0
commit
efc2e4f94e
15 changed files with 38 additions and 10 deletions
|
@ -3,6 +3,7 @@ import util.format
|
||||||
def scrollable(func):
|
def scrollable(func):
|
||||||
def wrapper(module, widget):
|
def wrapper(module, widget):
|
||||||
text = func(module, widget)
|
text = func(module, widget)
|
||||||
|
widget.set('_raw', text)
|
||||||
if not text:
|
if not text:
|
||||||
return text
|
return text
|
||||||
width = widget.get('theme.width', util.format.asint(module.parameter('width', 30)))
|
width = widget.get('theme.width', util.format.asint(module.parameter('width', 30)))
|
||||||
|
|
|
@ -8,12 +8,19 @@ import core.decorators
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
def load(module_name, config=None):
|
def load(module_name, config=None):
|
||||||
try:
|
error = None
|
||||||
mod = importlib.import_module('modules.{}'.format(module_name))
|
for namespace in [ 'core', 'contrib' ]:
|
||||||
except ImportError as error:
|
try:
|
||||||
log.fatal('failed to import {}: {}'.format(module_name, error))
|
mod = importlib.import_module('modules.{}.{}'.format(namespace, module_name))
|
||||||
return Error(config, module_name, error)
|
return getattr(mod, 'Module')(config)
|
||||||
return getattr(mod, 'Module')(config)
|
except ModuleNotFoundError as e:
|
||||||
|
pass
|
||||||
|
except ImportError as e:
|
||||||
|
error = str(e)
|
||||||
|
if not error:
|
||||||
|
error = 'No such module'
|
||||||
|
log.fatal('failed to import {}: {}'.format(module_name, error))
|
||||||
|
return Error(config, module_name, error)
|
||||||
|
|
||||||
class Module(core.input.Object):
|
class Module(core.input.Object):
|
||||||
def __init__(self, config=None, widgets=[]):
|
def __init__(self, config=None, widgets=[]):
|
||||||
|
|
|
@ -26,9 +26,19 @@ class module(unittest.TestCase):
|
||||||
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_importerror(self):
|
||||||
|
with unittest.mock.patch('core.module.importlib') as importlib:
|
||||||
|
importlib.import_module.side_effect = ImportError('some-error')
|
||||||
|
|
||||||
|
config = unittest.mock.MagicMock()
|
||||||
|
module = core.module.load(module_name=self.validModuleName, config=config)
|
||||||
|
module.widget().full_text()
|
||||||
|
self.assertEqual('Error', module.__class__.__name__, 'an invalid module must be a core.module.Error')
|
||||||
|
self.assertEqual(module.widget().get('_raw'), 'test: some-error')
|
||||||
|
|
||||||
def test_loadvalid_module(self):
|
def test_loadvalid_module(self):
|
||||||
module = core.module.load(module_name=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.core.{}'.format(self.validModuleName), module.__class__.__module__, 'module must be a modules.core.<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')
|
||||||
self.assertEqual([], module.state(None), 'default state of module is empty')
|
self.assertEqual([], module.state(None), 'default state of module is empty')
|
||||||
|
|
||||||
|
@ -90,4 +100,14 @@ class module(unittest.TestCase):
|
||||||
self.assertEqual(None, module.widget(self.unusedWidgetName))
|
self.assertEqual(None, module.widget(self.unusedWidgetName))
|
||||||
self.assertEqual(self.someWidget, module.widget())
|
self.assertEqual(self.someWidget, module.widget())
|
||||||
|
|
||||||
|
def test_default_thresholds(self):
|
||||||
|
cfg = core.config.Config([])
|
||||||
|
module = TestModule(config=cfg, widgets=[self.someWidget, self.anotherWidget])
|
||||||
|
|
||||||
|
self.assertEqual('critical', module.threshold_state(100, 80, 99))
|
||||||
|
self.assertEqual('warning', module.threshold_state(100, 80, 100))
|
||||||
|
self.assertEqual('warning', module.threshold_state(81, 80, 100))
|
||||||
|
self.assertEqual(None, module.threshold_state(80, 80, 100))
|
||||||
|
self.assertEqual(None, module.threshold_state(10, 80, 100))
|
||||||
|
|
||||||
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
||||||
|
|
|
@ -1,13 +1,13 @@
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
import modules.kernel
|
import modules.core.kernel
|
||||||
|
|
||||||
class kernel(unittest.TestCase):
|
class kernel(unittest.TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.someKernel = 'this-is-my-kernel'
|
self.someKernel = 'this-is-my-kernel'
|
||||||
with unittest.mock.patch('modules.kernel.platform') as platform:
|
with unittest.mock.patch('modules.core.kernel.platform') as platform:
|
||||||
platform.release.return_value = self.someKernel
|
platform.release.return_value = self.someKernel
|
||||||
self.module = modules.kernel.Module()
|
self.module = modules.core.kernel.Module()
|
||||||
|
|
||||||
def test_full_text(self):
|
def test_full_text(self):
|
||||||
self.assertEqual(1, len(self.module.widgets()))
|
self.assertEqual(1, len(self.module.widgets()))
|
||||||
|
|
Loading…
Reference in a new issue