Always write UTF-8 string

This commit is contained in:
ZyX 2015-01-25 16:59:36 +03:00
parent 1e7dc7900d
commit ea9eae676f
5 changed files with 39 additions and 14 deletions

View File

@ -10,6 +10,7 @@ from argparse import ArgumentParser
from powerline import Powerline
from powerline.lib.monotonic import monotonic
from powerline.lib.encoding import get_unicode_writer
if __name__ == '__main__':
@ -25,10 +26,13 @@ if __name__ == '__main__':
interval = 0.5
lock = Lock()
write = get_unicode_writer(encoding='utf-8')
def render(event=None, data=None, sub=None):
global lock
with lock:
print(powerline.render())
write(powerline.render())
write('\n')
sys.stdout.flush()
if args.i3:

View File

@ -150,7 +150,7 @@ def get_argparser(ArgumentParser=argparse.ArgumentParser):
return parser
def write_output(args, powerline, segment_info, write, encoding):
def write_output(args, powerline, segment_info, write):
if args.renderer_arg:
segment_info.update(args.renderer_arg)
if args.side.startswith('above'):
@ -159,8 +159,7 @@ def write_output(args, powerline, segment_info, write, encoding):
segment_info=segment_info,
mode=segment_info.get('mode', None),
):
write(line.encode(encoding, 'replace'))
write(b'\n')
write(line + '\n')
args.side = args.side[len('above'):]
if args.side:
@ -170,4 +169,4 @@ def write_output(args, powerline, segment_info, write, encoding):
segment_info=segment_info,
mode=segment_info.get('mode', None),
)
write(rendered.encode(encoding, 'replace'))
write(rendered)

View File

@ -86,3 +86,28 @@ def get_preferred_environment_encoding():
locale.getpreferredencoding()
or 'utf-8'
)
def get_unicode_writer(stream=sys.stdout, encoding=None, errors='replace'):
'''Get function which will write unicode string to the given stream
Writing is done using encoding returned by
:py:func:`get_preferred_output_encoding`.
:param file stream:
Stream to write to. Default value is :py:attr:`sys.stdout`.
:param str encoding:
Determines which encoding to use. If this argument is specified then
:py:func:`get_preferred_output_encoding` is not used.
:param str errors:
Determines what to do with characters which cannot be encoded. See
``errors`` argument of :py:func:`codecs.encode`.
:return: Callable which writes unicode string to the given stream using
the preferred output encoding.
'''
encoding = encoding or get_preferred_output_encoding()
if sys.version_info < (3,) or not hasattr(stream, 'buffer'):
return lambda s: stream.write(s.encode(encoding, errors))
else:
return lambda s: stream.buffer.write(s.encode(encoding, errors))

View File

@ -17,7 +17,7 @@ from io import BytesIO
from powerline.shell import ShellPowerline
from powerline.commands.main import finish_args, write_output
from powerline.lib.monotonic import monotonic
from powerline.lib.encoding import get_preferred_output_encoding
from powerline.lib.encoding import get_preferred_output_encoding, get_preferred_arguments_encoding, get_unicode_writer
from powerline.commands.main import get_argparser as get_main_argparser
from powerline.commands.daemon import get_argparser as get_daemon_argparser
@ -107,7 +107,7 @@ def render(args, environ, cwd):
else:
return 'Failed to render {0}: {1}'.format(str(key), str(e))
s = BytesIO()
write_output(args, powerline, segment_info, s.write, encoding)
write_output(args, powerline, segment_info, get_unicode_writer(stream=s))
s.seek(0)
return s.read()
@ -150,10 +150,7 @@ def do_write(conn, result):
pass
encoding = get_preferred_output_encoding()
def safe_bytes(o, encoding=encoding):
def safe_bytes(o, encoding=get_preferred_output_encoding()):
'''Return bytes instance without ever throwing an exception.'''
try:
try:
@ -171,7 +168,7 @@ def safe_bytes(o, encoding=encoding):
return safe_bytes(str(e), encoding)
def parse_args(req):
def parse_args(req, encoding=get_preferred_arguments_encoding()):
args = [x.decode(encoding) for x in req.split(b'\0') if x]
numargs = int(args[0], 16)
shell_args = parser.parse_args(args[1:numargs + 1])

View File

@ -13,7 +13,7 @@ except ImportError:
from powerline.shell import ShellPowerline
from powerline.commands.main import get_argparser, finish_args, write_output
from powerline.lib.unicode import get_preferred_output_encoding
from powerline.lib.encoding import get_unicode_writer
if sys.version_info < (3,):
@ -27,4 +27,4 @@ if __name__ == '__main__':
finish_args(os.environ, args)
powerline = ShellPowerline(args, run_once=True)
segment_info = {'args': args, 'environ': os.environ}
write_output(args, powerline, segment_info, write, get_preferred_output_encoding())
write_output(args, powerline, segment_info, get_unicode_writer())