2016-12-04 08:37:01 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
2017-04-02 08:31:23 +02:00
|
|
|
import os
|
2016-12-04 08:37:01 +01:00
|
|
|
import sys
|
2017-04-02 08:31:23 +02:00
|
|
|
import logging
|
2016-12-08 11:31:20 +01:00
|
|
|
import bumblebee.theme
|
2016-12-04 08:37:01 +01:00
|
|
|
import bumblebee.engine
|
|
|
|
import bumblebee.config
|
2016-12-04 16:14:43 +01:00
|
|
|
import bumblebee.output
|
2016-12-09 19:29:16 +01:00
|
|
|
import bumblebee.input
|
2017-04-02 08:56:44 +02:00
|
|
|
import bumblebee.modules.error
|
2016-12-04 08:37:01 +01:00
|
|
|
|
2017-09-16 12:22:20 +02:00
|
|
|
# taken from
|
|
|
|
# https://stackoverflow.com/a/42146082
|
|
|
|
# seems an excellent solution to me
|
|
|
|
class SmartStdout:
|
|
|
|
def __init__(self, encoding=None, org_stdout=None):
|
|
|
|
if org_stdout is None:
|
|
|
|
org_stdout = getattr(sys.stdout, 'org_stdout', sys.stdout)
|
|
|
|
self.org_stdout = org_stdout
|
|
|
|
self.encoding = encoding or getattr(org_stdout, 'encoding', None) or 'utf-8'
|
|
|
|
|
|
|
|
def write(self, s):
|
|
|
|
self.org_stdout.write(s.encode(self.encoding, 'backslashreplace'))
|
|
|
|
|
|
|
|
def __getattr__(self, name):
|
|
|
|
return getattr(self.org_stdout, name)
|
|
|
|
|
|
|
|
def set_defaultencoding_globally(encoding='utf-8'):
|
|
|
|
assert sys.getdefaultencoding() in ('ascii', 'mbcs', encoding)
|
|
|
|
import imp
|
|
|
|
_sys_org = imp.load_dynamic('_sys_org', 'sys')
|
|
|
|
_sys_org.setdefaultencoding(encoding)
|
|
|
|
|
2016-12-04 08:37:01 +01:00
|
|
|
def main():
|
|
|
|
config = bumblebee.config.Config(sys.argv[1:])
|
2017-04-04 15:15:50 +02:00
|
|
|
|
|
|
|
if config.debug():
|
|
|
|
logging.basicConfig(
|
|
|
|
level=logging.DEBUG,
|
|
|
|
format="[%(asctime)s] %(levelname)-8s %(message)s",
|
2017-08-20 10:59:39 +02:00
|
|
|
filename=config.logfile()
|
2017-04-04 15:15:50 +02:00
|
|
|
)
|
|
|
|
else:
|
|
|
|
logging.basicConfig(
|
|
|
|
level=logging.DEBUG,
|
|
|
|
format="[%(asctime)s] %(levelname)-8s %(message)s",
|
|
|
|
stream=sys.stderr
|
|
|
|
)
|
|
|
|
|
2016-12-08 11:31:20 +01:00
|
|
|
theme = bumblebee.theme.Theme(config.theme())
|
|
|
|
output = bumblebee.output.I3BarOutput(theme=theme)
|
2016-12-09 19:29:16 +01:00
|
|
|
inp = bumblebee.input.I3BarInput()
|
2017-04-02 08:31:23 +02:00
|
|
|
engine = None
|
2016-12-04 08:37:01 +01:00
|
|
|
|
2017-04-02 08:31:23 +02:00
|
|
|
try:
|
|
|
|
engine = bumblebee.engine.Engine(
|
|
|
|
config=config,
|
|
|
|
output=output,
|
|
|
|
inp=inp,
|
|
|
|
)
|
|
|
|
engine.run()
|
2017-05-13 20:28:11 +02:00
|
|
|
except KeyboardInterrupt as error:
|
|
|
|
inp.stop()
|
|
|
|
sys.exit(0)
|
2017-04-02 08:31:23 +02:00
|
|
|
except BaseException as e:
|
2017-06-16 15:36:56 +02:00
|
|
|
if not engine: raise
|
2017-06-10 14:08:49 +02:00
|
|
|
module = engine.current_module()
|
2017-04-02 08:31:23 +02:00
|
|
|
logging.exception(e)
|
|
|
|
if output.started():
|
|
|
|
output.flush()
|
|
|
|
output.end()
|
|
|
|
else:
|
|
|
|
output.start()
|
|
|
|
import time
|
|
|
|
while True:
|
|
|
|
output.begin()
|
|
|
|
error = bumblebee.modules.error.Module(engine, config)
|
2017-06-10 14:08:49 +02:00
|
|
|
error.set("exception occurred: {} in {}".format(e, module))
|
2017-04-02 08:31:23 +02:00
|
|
|
widget = error.widgets()[0]
|
|
|
|
widget.link_module(error)
|
|
|
|
output.draw(widget, error)
|
|
|
|
output.flush()
|
|
|
|
output.end()
|
|
|
|
time.sleep(1)
|
2016-12-09 19:29:16 +01:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2017-09-16 12:22:20 +02:00
|
|
|
if sys.stdout.isatty():
|
|
|
|
sys.stdout = sys.stderr = SmartStdout()
|
|
|
|
set_defaultencoding_globally('utf-8')
|
2016-12-09 19:29:16 +01:00
|
|
|
main()
|
2016-12-04 08:37:01 +01:00
|
|
|
|
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|