[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:
parent
b7ca5eb3a5
commit
66537fbe05
4 changed files with 44 additions and 20 deletions
|
@ -3,6 +3,7 @@
|
||||||
import sys
|
import sys
|
||||||
import core.config
|
import core.config
|
||||||
import core.output
|
import core.output
|
||||||
|
import core.module
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
config = core.config.Config(sys.argv[1:])
|
config = core.config.Config(sys.argv[1:])
|
||||||
|
@ -10,15 +11,15 @@ def main():
|
||||||
modules = []
|
modules = []
|
||||||
for module in config.modules():
|
for module in config.modules():
|
||||||
modules.append(core.module.load(module))
|
modules.append(core.module.load(module))
|
||||||
sys.stdout.write(output.start())
|
output.draw('start')
|
||||||
while True:
|
while True:
|
||||||
sys.stdout.write(output.begin_status_line())
|
output.clear()
|
||||||
for module in modules:
|
for module in modules:
|
||||||
module.update()
|
module.update()
|
||||||
sys.stdout.write(output.draw(module))
|
output.append(module)
|
||||||
sys.stdout.write(output.end_status_line())
|
output.draw('statusline')
|
||||||
output.wait(config.interval())
|
output.wait(config.interval())
|
||||||
sys.stdout.write(output.stop())
|
output.draw('stop')
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
|
@ -1,18 +1,42 @@
|
||||||
|
import sys
|
||||||
import json
|
import json
|
||||||
import time
|
import time
|
||||||
|
|
||||||
class i3(object):
|
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):
|
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):
|
def stop(self):
|
||||||
return ']\n'
|
return { 'suffix': '\n]' }
|
||||||
|
|
||||||
def begin_status_line(self):
|
def clear(self):
|
||||||
return '['
|
self._statusline = []
|
||||||
|
|
||||||
def end_status_line(self):
|
def append(self, module):
|
||||||
return '],\n'
|
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):
|
def wait(self, interval):
|
||||||
time.sleep(interval)
|
time.sleep(interval)
|
||||||
|
|
|
@ -3,10 +3,11 @@
|
||||||
"""Test module
|
"""Test module
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
import core.widget
|
||||||
import core.module
|
import core.module
|
||||||
|
|
||||||
class Module(core.module.Module):
|
class Module(core.module.Module):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
pass
|
super().__init__(core.widget.Widget('test'))
|
||||||
|
|
||||||
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
||||||
|
|
|
@ -11,17 +11,15 @@ class i3(unittest.TestCase):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def test_start(self):
|
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.assertEqual(1, data['version'], 'i3bar protocol version 1 expected')
|
||||||
self.assertTrue(data['click_events'], 'click events should be enabled')
|
self.assertTrue(data['click_events'], 'click events should be enabled')
|
||||||
|
self.assertEqual('\n[', all_data['suffix'])
|
||||||
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')
|
|
||||||
|
|
||||||
def test_stop(self):
|
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
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
||||||
|
|
Loading…
Reference in a new issue