[core/module] Add error widgets if a module throws
Module should have to care less about thrown exceptions.
This commit is contained in:
parent
cb3482ae27
commit
a1aec8fff6
7 changed files with 36 additions and 4 deletions
|
@ -46,7 +46,7 @@ def main():
|
||||||
|
|
||||||
config = core.config.Config(sys.argv[1:])
|
config = core.config.Config(sys.argv[1:])
|
||||||
theme = core.theme.Theme(config.theme(), config.iconset())
|
theme = core.theme.Theme(config.theme(), config.iconset())
|
||||||
output = core.output.i3(theme)
|
output = core.output.i3(theme, config)
|
||||||
modules = []
|
modules = []
|
||||||
|
|
||||||
input_thread = threading.Thread(target=handle_input, args=(output,))
|
input_thread = threading.Thread(target=handle_input, args=(output,))
|
||||||
|
|
|
@ -38,6 +38,14 @@ class Module(core.input.Object):
|
||||||
def update(self):
|
def update(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
def update_wrapper(self):
|
||||||
|
try:
|
||||||
|
self.update()
|
||||||
|
except Exception as e:
|
||||||
|
module = Error(self._config, 'error', str(e))
|
||||||
|
self._widgets = [module.widgets()[0]]
|
||||||
|
self.update = module.update
|
||||||
|
|
||||||
def name(self):
|
def name(self):
|
||||||
return self._name if self._name else self.module_name()
|
return self._name if self._name else self.module_name()
|
||||||
|
|
||||||
|
|
|
@ -6,14 +6,20 @@ import core.theme
|
||||||
import core.event
|
import core.event
|
||||||
|
|
||||||
class i3(object):
|
class i3(object):
|
||||||
def __init__(self, theme=core.theme.Theme()):
|
def __init__(self, theme=core.theme.Theme(), config=None):
|
||||||
self._modules = []
|
self._modules = []
|
||||||
self._status = {}
|
self._status = {}
|
||||||
self._theme = theme
|
self._theme = theme
|
||||||
|
self._config = config
|
||||||
core.event.register('start', self.draw, 'start')
|
core.event.register('start', self.draw, 'start')
|
||||||
core.event.register('update', self.draw, 'statusline')
|
core.event.register('update', self.draw, 'statusline')
|
||||||
core.event.register('stop', self.draw, 'stop')
|
core.event.register('stop', self.draw, 'stop')
|
||||||
|
|
||||||
|
def theme(self, new_theme=None):
|
||||||
|
if new_theme:
|
||||||
|
self._theme = new_theme
|
||||||
|
return self._theme
|
||||||
|
|
||||||
def modules(self, modules=None):
|
def modules(self, modules=None):
|
||||||
if not modules:
|
if not modules:
|
||||||
return self._modules
|
return self._modules
|
||||||
|
@ -96,7 +102,7 @@ class i3(object):
|
||||||
for module in self._modules:
|
for module in self._modules:
|
||||||
if affected_modules and not module.id() in affected_modules:
|
if affected_modules and not module.id() in affected_modules:
|
||||||
continue
|
continue
|
||||||
module.update()
|
module.update_wrapper()
|
||||||
for widget in module.widgets():
|
for widget in module.widgets():
|
||||||
self._status[widget] = widget.full_text()
|
self._status[widget] = widget.full_text()
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
import core.input
|
import core.input
|
||||||
|
import core.decorators
|
||||||
import util.store
|
import util.store
|
||||||
|
|
||||||
class Widget(util.store.Store, core.input.Object):
|
class Widget(util.store.Store, core.input.Object):
|
||||||
|
|
|
@ -23,5 +23,5 @@
|
||||||
|
|
||||||
## Improvements
|
## Improvements
|
||||||
- pango output (improve - maybe autodetect? see #531)
|
- pango output (improve - maybe autodetect? see #531)
|
||||||
- only update specific, affected modules when clicking
|
|
||||||
- allow handlers to specify whether to update or not (e.g. scroll)
|
- allow handlers to specify whether to update or not (e.g. scroll)
|
||||||
|
- error handling: don't catch too many exceptions in a module (pulseaudio!), instead, propagate them as error widgets
|
||||||
|
|
|
@ -26,11 +26,20 @@ class module(unittest.TestCase):
|
||||||
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.{}'.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')
|
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')
|
||||||
|
|
||||||
def test_empty_widgets(self):
|
def test_empty_widgets(self):
|
||||||
module = core.module.Module(widgets=[])
|
module = core.module.Module(widgets=[])
|
||||||
self.assertEqual([], module.widgets())
|
self.assertEqual([], module.widgets())
|
||||||
|
|
||||||
|
def test_error_widget(self):
|
||||||
|
cfg = core.config.Config(shlex.split('-p test_module.foo=5'))
|
||||||
|
module = core.module.Error(cfg, 'test-mod', 'xyz')
|
||||||
|
self.assertEqual(['critical'], module.state(None), 'error module must have critical state')
|
||||||
|
full_text = module.full_text(module.widgets()[0])
|
||||||
|
self.assertTrue('test-mod' in full_text)
|
||||||
|
self.assertTrue('xyz' in full_text)
|
||||||
|
|
||||||
def test_single_widget(self):
|
def test_single_widget(self):
|
||||||
module = core.module.Module(widgets=self.someWidget)
|
module = core.module.Module(widgets=self.someWidget)
|
||||||
self.assertEqual([self.someWidget], module.widgets())
|
self.assertEqual([self.someWidget], module.widgets())
|
||||||
|
|
|
@ -13,6 +13,9 @@ class i3(unittest.TestCase):
|
||||||
widget = unittest.mock.MagicMock()
|
widget = unittest.mock.MagicMock()
|
||||||
widget.full_text.return_value = "test"
|
widget.full_text.return_value = "test"
|
||||||
self.someModule = TestModule(widgets=[widget, widget, widget])
|
self.someModule = TestModule(widgets=[widget, widget, widget])
|
||||||
|
self.paddedTheme = core.theme.Theme(raw_data = {
|
||||||
|
'defaults': { 'padding': ' ' }
|
||||||
|
});
|
||||||
|
|
||||||
def test_start(self):
|
def test_start(self):
|
||||||
core.event.clear()
|
core.event.clear()
|
||||||
|
@ -55,4 +58,9 @@ class i3(unittest.TestCase):
|
||||||
data = self.i3.statusline()
|
data = self.i3.statusline()
|
||||||
self.assertEqual(len(self.someModule.widgets())*3, len(data['data']), 'wrong number of widgets')
|
self.assertEqual(len(self.someModule.widgets())*3, len(data['data']), 'wrong number of widgets')
|
||||||
|
|
||||||
|
def test_padding(self):
|
||||||
|
self.i3.theme(self.paddedTheme)
|
||||||
|
result = self.i3.__pad(self.someModule, self.someModule.widgets()[0], 'abc')
|
||||||
|
self.assertEqual(' abc ', result)
|
||||||
|
|
||||||
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
||||||
|
|
Loading…
Reference in a new issue