bumblebee-status/bumblebee-status
Tobias Witek dfb1e39421 [core + modules/cmus] Have another go at fixing unicode issues
Override sys.stdout and sys.stderr in an attempt to enforce utf-8
encoding. Probably this will cause all kinds of weird issues down the
line, but at least, it seems to solve the immediate issue.

fixes #176
2017-09-16 12:22:20 +02:00

93 lines
2.6 KiB
Python
Executable file

#!/usr/bin/env python
import os
import sys
import logging
import bumblebee.theme
import bumblebee.engine
import bumblebee.config
import bumblebee.output
import bumblebee.input
import bumblebee.modules.error
# 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)
def main():
config = bumblebee.config.Config(sys.argv[1:])
if config.debug():
logging.basicConfig(
level=logging.DEBUG,
format="[%(asctime)s] %(levelname)-8s %(message)s",
filename=config.logfile()
)
else:
logging.basicConfig(
level=logging.DEBUG,
format="[%(asctime)s] %(levelname)-8s %(message)s",
stream=sys.stderr
)
theme = bumblebee.theme.Theme(config.theme())
output = bumblebee.output.I3BarOutput(theme=theme)
inp = bumblebee.input.I3BarInput()
engine = None
try:
engine = bumblebee.engine.Engine(
config=config,
output=output,
inp=inp,
)
engine.run()
except KeyboardInterrupt as error:
inp.stop()
sys.exit(0)
except BaseException as e:
if not engine: raise
module = engine.current_module()
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)
error.set("exception occurred: {} in {}".format(e, module))
widget = error.widgets()[0]
widget.link_module(error)
output.draw(widget, error)
output.flush()
output.end()
time.sleep(1)
if __name__ == "__main__":
if sys.stdout.isatty():
sys.stdout = sys.stderr = SmartStdout()
set_defaultencoding_globally('utf-8')
main()
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4