595778f7c3
Re-enable the -f | --logfile parameter, but still default to logging to stderr. Also, add a "debug" module that is automatically displayed if a bar is run in debug mode (thanks to @bbernhard for the excellent suggestion) fixes #614
90 lines
2.4 KiB
Python
Executable file
90 lines
2.4 KiB
Python
Executable file
#!/usr/bin/env python
|
|
|
|
import os
|
|
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
|
|
core.event.trigger('update', modules.keys())
|
|
core.event.trigger('draw')
|
|
|
|
poll.unregister(sys.stdin.fileno())
|
|
|
|
def main():
|
|
config = core.config.Config(sys.argv[1:])
|
|
|
|
level = logging.DEBUG if config.debug() else logging.ERROR
|
|
if config.logfile():
|
|
logging.basicConfig(
|
|
level=level,
|
|
format="[%(asctime)s] %(module)-16s %(levelname)-8s %(message)s",
|
|
filename=os.path.abspath(os.path.expanduser(config.logfile()))
|
|
)
|
|
else:
|
|
logging.basicConfig(
|
|
level=level,
|
|
format="[%(asctime)s] %(module)-16s %(levelname)-8s %(message)s",
|
|
stream=sys.stderr
|
|
)
|
|
|
|
theme = core.theme.Theme(config.theme(), config.iconset())
|
|
output = core.output.i3(theme, config)
|
|
modules = []
|
|
|
|
input_thread = threading.Thread(target=handle_input, args=(output,))
|
|
input_thread.daemon = True
|
|
input_thread.start()
|
|
|
|
if config.debug():
|
|
modules.append(core.module.load('debug', config, theme))
|
|
|
|
for module in config.modules():
|
|
modules.append(core.module.load(module, config, theme))
|
|
output.modules(modules)
|
|
core.event.trigger('start')
|
|
while True:
|
|
core.event.trigger('update')
|
|
core.event.trigger('draw')
|
|
output.wait(config.interval())
|
|
core.event.trigger('stop')
|
|
|
|
if __name__ == "__main__":
|
|
main()
|
|
try:
|
|
main()
|
|
except Exception as e:
|
|
output = core.output.i3()
|
|
output.modules(core.module.Error(module='main', error=e))
|
|
output.draw('start')
|
|
output.update()
|
|
output.draw('statusline')
|
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|