From 66537fbe0503b043c4cc11738e52be10658c988f Mon Sep 17 00:00:00 2001 From: Tobias Witek Date: Sat, 1 Feb 2020 21:37:38 +0100 Subject: [PATCH] [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. --- bumblebee-status | 11 ++++++----- core/output.py | 36 ++++++++++++++++++++++++++++++------ modules/test.py | 3 ++- tests/core/test_output.py | 14 ++++++-------- 4 files changed, 44 insertions(+), 20 deletions(-) diff --git a/bumblebee-status b/bumblebee-status index 75ad1b8..e461758 100755 --- a/bumblebee-status +++ b/bumblebee-status @@ -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__": diff --git a/core/output.py b/core/output.py index ddbb1ca..9a6b898 100644 --- a/core/output.py +++ b/core/output.py @@ -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) diff --git a/modules/test.py b/modules/test.py index 57af6db..3d22ddb 100644 --- a/modules/test.py +++ b/modules/test.py @@ -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 diff --git a/tests/core/test_output.py b/tests/core/test_output.py index 17526f3..e32839a 100644 --- a/tests/core/test_output.py +++ b/tests/core/test_output.py @@ -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