bumblebee-status/bumblebee-status
Tobias Witek a4904d998f [core] Use core.event to decouple main and output
Make output act on events, not on concrete calls.
2020-02-16 14:30:45 +01:00

76 lines
2 KiB
Python
Executable file

#!/usr/bin/env python
import sys
import json
import select
import logging
import threading
import core.config
import core.output
import core.module
import core.input
import core.event
def handle_input(output):
poll = select.poll()
poll.register(sys.stdin.fileno(), select.POLLIN)
while True:
events = poll.poll()
modules = {}
for fileno, event in events:
line = '['
while line.startswith('['):
line = sys.stdin.readline().strip(',').strip()
logging.info('input event: {}'.format(line))
try:
event = json.loads(line)
core.input.trigger(event)
if 'name' in event:
modules[event['name']] = True
except ValueError:
pass
output.update(modules.keys())
core.event.trigger('update')
poll.unregister(sys.stdin.fileno())
def main():
logging.basicConfig(
level=logging.DEBUG,
format="[%(asctime)s] %(module)-16s %(levelname)-8s %(message)s",
stream=sys.stderr
)
config = core.config.Config(sys.argv[1:])
theme = core.theme.Theme(config.theme(), config.iconset())
output = core.output.i3(theme)
modules = []
input_thread = threading.Thread(target=handle_input, args=(output,))
input_thread.daemon = True
input_thread.start()
for module in config.modules():
modules.append(core.module.load(module, config))
output.modules(modules)
core.event.trigger('start')
while True:
output.update()
core.event.trigger('update')
output.wait(config.interval())
core.event.trigger('stop')
if __name__ == "__main__":
try:
main()
except Exception as e:
output = core.output.i3()
output.modules(core.module.Error(None, 'main', e))
output.draw('start')
output.update()
output.draw('statusline')
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4