Ensure unicode output to fix encoding errors.

This commit is contained in:
Bastian Kleineidam 2013-04-12 21:02:31 +02:00
parent 30ca959c02
commit 3a03554d26

View file

@ -15,7 +15,7 @@ class Output(object):
def __init__(self, stream=sys.stdout): def __init__(self, stream=sys.stdout):
"""Initialize context and indentation.""" """Initialize context and indentation."""
self.context = '' self.context = u''
self.level = 0 self.level = 0
self.timestamps = False self.timestamps = False
self.setStream(stream) self.setStream(stream)
@ -34,11 +34,11 @@ class Output(object):
def warn(self, s): def warn(self, s):
"""Write a warning message.""" """Write a warning message."""
self.write("WARN: %s" % s, color='bold;yellow') self.write(u"WARN: %s" % s, color='bold;yellow')
def error(self, s, tb=None): def error(self, s, tb=None):
"""Write an error message.""" """Write an error message."""
self.write("ERROR: %s" % s, color='light;red') self.write(u"ERROR: %s" % s, color='light;red')
#if tb is not None: #if tb is not None:
# self.write('Traceback (most recent call last):', 1) # self.write('Traceback (most recent call last):', 1)
@ -55,20 +55,24 @@ class Output(object):
if level > self.level: if level > self.level:
return return
if self.level > 1 or self.timestamps: if self.level > 1 or self.timestamps:
timestamp = time.strftime('%H:%M:%S ') timestamp = time.strftime(u'%H:%M:%S ')
else: else:
timestamp = '' timestamp = u''
with lock: with lock:
if self.context or timestamp: if self.context or timestamp:
self.stream.write('%s%s> ' % (timestamp, self.context)) self.stream.write(u'%s%s> ' % (timestamp, self.context))
self.stream.write('%s' % s, color=color) self.stream.write(u'%s' % s, color=color)
self.stream.write(os.linesep) try:
text_type = unicode
except NameError:
text_type = str
self.stream.write(text_type(os.linesep))
self.stream.flush() self.stream.flush()
def writelines(self, lines, level=0): def writelines(self, lines, level=0):
"""Write multiple messages.""" """Write multiple messages."""
for line in lines: for line in lines:
for line in line.rstrip('\n').split('\n'): for line in line.rstrip(u'\n').split(u'\n'):
self.write(line.rstrip('\n'), level=level) self.write(line.rstrip(u'\n'), level=level)
out = Output() out = Output()