[core] Refactor -> replace some getter/setter pairs with attributes

Remove some set_* methods and replace them with a simple attribute.

see #23
This commit is contained in:
Tobi-wan Kenobi 2016-12-09 11:49:59 +01:00
parent 068968bbf5
commit e59e969bdc
7 changed files with 16 additions and 21 deletions

View file

@ -63,10 +63,10 @@ class Engine(object):
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"], module["name"]))
self._modules.append(self._load_module(module["module"], module["name"]))
return self._modules
def load_module(self, module_name, config_name=None):
def _load_module(self, module_name, config_name=None):
"""Load specified module and return it as object"""
if config_name is None:
config_name = module_name
@ -95,7 +95,7 @@ class Engine(object):
for module in self._modules:
module.update(module.widgets())
for widget in module.widgets():
widget.set_module(module)
widget.link_module(module)
self._output.draw(widget=widget, engine=self)
self._output.flush()
self._output.end()

View file

@ -9,18 +9,14 @@ class Widget(object):
"""Represents a single visible block in the status bar"""
def __init__(self, full_text):
self._full_text = full_text
self._module = None
self.module = None
def set_module(self, module):
def link_module(self, module):
"""Set the module that spawned this widget
This is done outside the constructor to avoid having to
pass in the module name in every concrete module implementation"""
self._module = module.name
def module(self):
"""Return the name of the module that spawned this widget"""
return self._module
self.module = module.name
def full_text(self):
"""Retrieve the full text to display in the widget"""

View file

@ -63,7 +63,7 @@ class Theme(object):
def _get(self, widget, name, default=None):
"""Return the config value 'name' for 'widget'"""
module_theme = self._theme.get(widget.module(), {})
module_theme = self._theme.get(widget.module, {})
padding = None
if name != "padding":

View file

@ -20,6 +20,8 @@ class TestGenericModules(unittest.TestCase):
for mod in self.objects:
widgets = self.objects[mod].widgets()
for widget in widgets:
widget.link_module(self.objects[mod])
self.assertEquals(widget.module, mod)
assertWidgetAttributes(self, widget)
def test_update(self):

View file

@ -26,16 +26,16 @@ class TestEngine(unittest.TestCase):
self.assertFalse(self.engine.running())
def test_load_module(self):
module = self.engine.load_module(self.testModule)
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)
self.engine._load_module(self.invalidModule)
def test_load_none(self):
with self.assertRaises(ModuleLoadError):
self.engine.load_module(None)
self.engine._load_module(None)
def test_load_modules(self):
modules = self.engine.load_modules(self.testModules)

View file

@ -44,17 +44,17 @@ class TestTheme(unittest.TestCase):
self.assertEquals(self.theme.suffix(self.someWidget), self.defaultSuffix)
def test_widget_prefix(self):
self.someWidget.attr_module = self.widgetTheme
self.someWidget.module = self.widgetTheme
self.assertEquals(self.theme.prefix(self.someWidget), self.widgetPrefix)
def test_widget_fg(self):
self.assertEquals(self.theme.fg(self.someWidget), self.defaultColor)
self.someWidget.attr_module = self.widgetTheme
self.someWidget.module = self.widgetTheme
self.assertEquals(self.theme.fg(self.someWidget), self.widgetColor)
def test_widget_bg(self):
self.assertEquals(self.theme.bg(self.someWidget), self.defaultBgColor)
self.someWidget.attr_module = self.widgetTheme
self.someWidget.module = self.widgetTheme
self.assertEquals(self.theme.bg(self.someWidget), self.widgetBgColor)
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4

View file

@ -31,14 +31,11 @@ class MockOutput(object):
class MockWidget(object):
def __init__(self, text):
self._text = text
self.attr_module = None
self.module = None
def update(self, widgets):
pass
def module(self):
return self.attr_module
def full_text(self):
return self._text