[core] Add simple module loading
Add a way to load modules located in modules/*
This commit is contained in:
parent
8622673114
commit
bd12a51bfb
3 changed files with 41 additions and 7 deletions
|
@ -1,9 +1,22 @@
|
|||
import importlib
|
||||
import logging
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
def load(module_name):
|
||||
pass
|
||||
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')()
|
||||
|
||||
class Module(object):
|
||||
def update(self):
|
||||
pass
|
||||
|
||||
class Error(Module):
|
||||
def __init__(self, loaded_module_name):
|
||||
self._loaded_module_name = loaded_module_name
|
||||
|
||||
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
||||
|
|
|
@ -3,12 +3,10 @@
|
|||
"""Test module
|
||||
"""
|
||||
|
||||
import bumblebee.engine
|
||||
import core.module
|
||||
|
||||
class Module(bumblebee.engine.Module):
|
||||
def __init__(self, engine, config):
|
||||
super(Module, self).__init__(engine, config,
|
||||
bumblebee.output.Widget(full_text="test")
|
||||
)
|
||||
class Module(core.module.Module):
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
||||
|
|
23
tests/test_module.py
Normal file
23
tests/test_module.py
Normal file
|
@ -0,0 +1,23 @@
|
|||
import unittest
|
||||
|
||||
import core.module
|
||||
|
||||
class module(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self._invalidModuleName = 'invalid-module-name'
|
||||
self._validModuleName = 'test'
|
||||
|
||||
def tearDown(self):
|
||||
pass
|
||||
|
||||
def test_load_invalid_module(self):
|
||||
module = core.module.load(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)
|
||||
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')
|
||||
|
||||
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
Loading…
Reference in a new issue