mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Clean up logging to use python3 printing as well
This commit is contained in:
parent
e9ff40031f
commit
55713682f2
@ -1,7 +1,6 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
__license__ = 'GPL 3'
|
# vim:fileencoding=utf-8
|
||||||
__copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
|
# License: GPLv3 Copyright: 2009, Kovid Goyal <kovid at kovidgoyal.net>
|
||||||
__docformat__ = 'restructuredtext en'
|
|
||||||
|
|
||||||
'A simplified logging system'
|
'A simplified logging system'
|
||||||
|
|
||||||
@ -10,21 +9,29 @@ INFO = 1
|
|||||||
WARN = 2
|
WARN = 2
|
||||||
ERROR = 3
|
ERROR = 3
|
||||||
|
|
||||||
import sys, traceback, io
|
import io
|
||||||
|
import sys
|
||||||
|
import traceback
|
||||||
from functools import partial
|
from functools import partial
|
||||||
from threading import Lock
|
from threading import Lock
|
||||||
|
|
||||||
from calibre import isbytestring, force_unicode, as_unicode, prints
|
from calibre import as_unicode, force_unicode, isbytestring
|
||||||
from polyglot.builtins import unicode_type, iteritems
|
from calibre.utils.terminal import is_binary, polyglot_write
|
||||||
|
from polyglot.builtins import iteritems, unicode_type
|
||||||
|
|
||||||
|
|
||||||
class Stream(object):
|
class Stream(object):
|
||||||
|
|
||||||
def __init__(self, stream=None):
|
def __init__(self, stream=None):
|
||||||
if stream is None:
|
if stream is None:
|
||||||
stream = io.BytesIO()
|
stream = io.StringIO()
|
||||||
self.stream = getattr(stream, 'buffer', stream)
|
self.stream = stream
|
||||||
self._prints = partial(prints, file=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):
|
def flush(self):
|
||||||
self.stream.flush()
|
self.stream.flush()
|
||||||
@ -38,10 +45,10 @@ class ANSIStream(Stream):
|
|||||||
def __init__(self, stream=sys.stdout):
|
def __init__(self, stream=sys.stdout):
|
||||||
Stream.__init__(self, stream)
|
Stream.__init__(self, stream)
|
||||||
self.color = {
|
self.color = {
|
||||||
DEBUG: u'green',
|
DEBUG: 'green',
|
||||||
INFO: None,
|
INFO: None,
|
||||||
WARN: u'yellow',
|
WARN: 'yellow',
|
||||||
ERROR: u'red',
|
ERROR: 'red',
|
||||||
}
|
}
|
||||||
|
|
||||||
def prints(self, level, *args, **kwargs):
|
def prints(self, level, *args, **kwargs):
|
||||||
@ -260,7 +267,7 @@ class GUILog(ThreadSafeLog):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def plain_text(self):
|
def plain_text(self):
|
||||||
return u''.join(self.outputs[0].plain_text)
|
return ''.join(self.outputs[0].plain_text)
|
||||||
|
|
||||||
def dump(self):
|
def dump(self):
|
||||||
return self.outputs[0].dump()
|
return self.outputs[0].dump()
|
||||||
|
@ -27,6 +27,20 @@ def fmt(code):
|
|||||||
return '\033[%dm' % 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(
|
RATTRIBUTES = dict(
|
||||||
zip(range(1, 9), (
|
zip(range(1, 9), (
|
||||||
'bold',
|
'bold',
|
||||||
@ -173,18 +187,7 @@ class ANSIStream(Detect):
|
|||||||
return self.strip_and_write(text)
|
return self.strip_and_write(text)
|
||||||
|
|
||||||
def polyglot_write(self, text):
|
def polyglot_write(self, text):
|
||||||
binary = isinstance(text, bytes)
|
return polyglot_write(self.stream, self.is_binary, self.encoding, text)
|
||||||
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)
|
|
||||||
|
|
||||||
def strip_and_write(self, text):
|
def strip_and_write(self, text):
|
||||||
binary = isinstance(text, bytes)
|
binary = isinstance(text, bytes)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user