Clean up logging to use python3 printing as well

This commit is contained in:
Kovid Goyal 2019-12-04 19:52:22 +05:30
parent e9ff40031f
commit 55713682f2
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 36 additions and 26 deletions

View File

@ -1,7 +1,6 @@
__license__ = 'GPL 3'
__copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
__docformat__ = 'restructuredtext en'
#!/usr/bin/env python
# vim:fileencoding=utf-8
# License: GPLv3 Copyright: 2009, Kovid Goyal <kovid at kovidgoyal.net>
'A simplified logging system'
@ -10,21 +9,29 @@ INFO = 1
WARN = 2
ERROR = 3
import sys, traceback, io
import io
import sys
import traceback
from functools import partial
from threading import Lock
from calibre import isbytestring, force_unicode, as_unicode, prints
from polyglot.builtins import unicode_type, iteritems
from calibre import as_unicode, force_unicode, isbytestring
from calibre.utils.terminal import is_binary, polyglot_write
from polyglot.builtins import iteritems, unicode_type
class Stream(object):
def __init__(self, stream=None):
if stream is None:
stream = io.BytesIO()
self.stream = getattr(stream, 'buffer', stream)
self._prints = partial(prints, file=stream)
stream = io.StringIO()
self.stream = stream
self.is_binary = is_binary(self.stream)
self.encoding = getattr(self.stream, 'encoding', None) or 'utf-8'
self._prints = partial(print, file=self)
def write(self, text):
return polyglot_write(self.stream, self.is_binary, self.encoding, text)
def flush(self):
self.stream.flush()
@ -38,10 +45,10 @@ class ANSIStream(Stream):
def __init__(self, stream=sys.stdout):
Stream.__init__(self, stream)
self.color = {
DEBUG: u'green',
DEBUG: 'green',
INFO: None,
WARN: u'yellow',
ERROR: u'red',
WARN: 'yellow',
ERROR: 'red',
}
def prints(self, level, *args, **kwargs):
@ -260,7 +267,7 @@ class GUILog(ThreadSafeLog):
@property
def plain_text(self):
return u''.join(self.outputs[0].plain_text)
return ''.join(self.outputs[0].plain_text)
def dump(self):
return self.outputs[0].dump()

View File

@ -27,6 +27,20 @@ def fmt(code):
return '\033[%dm' % code
def polyglot_write(stream, is_binary, encoding, text):
binary = isinstance(text, bytes)
if binary:
if is_binary:
return stream.write(text)
buffer = getattr(stream, 'buffer', None)
if buffer is None:
return stream.write(text.decode('utf-8', 'replace'))
return buffer.write(text)
if is_binary:
text = text.encode(encoding, 'replace')
return stream.write(text)
RATTRIBUTES = dict(
zip(range(1, 9), (
'bold',
@ -173,18 +187,7 @@ class ANSIStream(Detect):
return self.strip_and_write(text)
def polyglot_write(self, text):
binary = isinstance(text, bytes)
stream = self.stream
if binary:
if self.is_binary:
return stream.write(text)
buffer = getattr(stream, 'buffer', None)
if buffer is None:
return stream.write(text.decode('utf-8', 'replace'))
return buffer.write(text)
if self.is_binary:
text = text.encode(self.encoding, 'replace')
return stream.write(text)
return polyglot_write(self.stream, self.is_binary, self.encoding, text)
def strip_and_write(self, text):
binary = isinstance(text, bytes)