[core/output] Rewrite to hide sys.stout

Add generic "draw()" method that redirects internally to the actual
calls. These can now produce JSON, which is nicer because:

1. Easier to use during testing
2. More flexible
3. Centralizes printing (somewhat)

Still, the "suffix" concept isn't really nice, but so far, I have no
better approach.
This commit is contained in:
Tobias Witek 2020-02-01 21:37:38 +01:00
parent b7ca5eb3a5
commit 66537fbe05
4 changed files with 44 additions and 20 deletions

View file

@ -3,6 +3,7 @@
import sys
import core.config
import core.output
import core.module
def main():
config = core.config.Config(sys.argv[1:])
@ -10,15 +11,15 @@ def main():
modules = []
for module in config.modules():
modules.append(core.module.load(module))
sys.stdout.write(output.start())
output.draw('start')
while True:
sys.stdout.write(output.begin_status_line())
output.clear()
for module in modules:
module.update()
sys.stdout.write(output.draw(module))
sys.stdout.write(output.end_status_line())
output.append(module)
output.draw('statusline')
output.wait(config.interval())
sys.stdout.write(output.stop())
output.draw('stop')
if __name__ == "__main__":

View file

@ -1,18 +1,42 @@
import sys
import json
import time
class i3(object):
def __init__(self):
self.clear()
def draw(self, what):
data = getattr(self, what)()
if 'data' in data:
sys.stdout.write(json.dumps(data['data']))
if 'suffix' in data:
sys.stdout.write(data['suffix'])
sys.stdout.write('\n')
def start(self):
return '{}\n'.format(json.dumps({ 'version': 1, 'click_events': True }))
return {
'data': { 'version': 1, 'click_events': True },
'suffix': '\n[',
}
def stop(self):
return ']\n'
return { 'suffix': '\n]' }
def begin_status_line(self):
return '['
def clear(self):
self._statusline = []
def end_status_line(self):
return '],\n'
def append(self, module):
for widget in module.widgets():
self._statusline.append({
'full_text': widget.full_text()
})
def statusline(self):
return {
'data': self._statusline,
'suffix': ','
}
def wait(self, interval):
time.sleep(interval)

View file

@ -3,10 +3,11 @@
"""Test module
"""
import core.widget
import core.module
class Module(core.module.Module):
def __init__(self):
pass
super().__init__(core.widget.Widget('test'))
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4

View file

@ -11,17 +11,15 @@ class i3(unittest.TestCase):
pass
def test_start(self):
data = json.loads(self.i3.start())
all_data = self.i3.start()
data = all_data['data']
self.assertEqual(1, data['version'], 'i3bar protocol version 1 expected')
self.assertTrue(data['click_events'], 'click events should be enabled')
def test_begin_status_line(self):
self.assertEqual('[', self.i3.begin_status_line(), 'each line must be a JSON array')
def test_end_status_line(self):
self.assertEqual('],\n', self.i3.end_status_line(), 'each line must terminate properly')
self.assertEqual('\n[', all_data['suffix'])
def test_stop(self):
self.assertEqual(']\n', self.i3.stop())
self.assertEqual('\n]', self.i3.stop()['suffix'])
# TODO: mock a "draw" call
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4