1
0
mirror of https://github.com/gryf/ebook-converter.git synced 2026-03-10 03:25:46 +01:00

Removed prints and extract function from init module

This commit is contained in:
2020-07-13 21:38:06 +02:00
parent 025878dfe5
commit 4216fef20e
20 changed files with 97 additions and 336 deletions

View File

@@ -4,10 +4,10 @@ A simplified logging system
import sys
import traceback
import io
import os
from functools import partial
from threading import Lock
from ebook_converter import force_unicode, prints
from ebook_converter import constants_old
DEBUG = 0
@@ -22,13 +22,13 @@ class Stream(object):
if stream is None:
stream = io.BytesIO()
self.stream = getattr(stream, 'buffer', stream)
self._prints = partial(prints, safe_encode=True, file=stream)
# self._prints = partial(prints, safe_encode=True, file=stream)
def flush(self):
self.stream.flush()
def prints(self, level, *args, **kwargs):
self._prints(*args, **kwargs)
raise NotImplementedError()
class ANSIStream(Stream):
@@ -43,97 +43,34 @@ class ANSIStream(Stream):
}
def prints(self, level, *args, **kwargs):
from ebook_converter.utils.terminal import ColoredStream
with ColoredStream(self.stream, self.color[level]):
self._prints(*args, **kwargs)
def flush(self):
self.stream.flush()
class FileStream(Stream):
def __init__(self, stream=None):
Stream.__init__(self, stream)
def prints(self, level, *args, **kwargs):
self._prints(*args, **kwargs)
class HTMLStream(Stream):
color = {DEBUG: b'<span style="color:green">',
INFO: b'<span>',
WARN: b'<span style="color:blue">',
ERROR: b'<span style="color:red">'}
normal = b'</span>'
def __init__(self, stream=sys.stdout):
Stream.__init__(self, stream)
def prints(self, level, *args, **kwargs):
self.stream.write(self.color[level])
kwargs['file'] = self.stream
self._prints(*args, **kwargs)
self.stream.write(self.normal)
def flush(self):
self.stream.flush()
class UnicodeHTMLStream(HTMLStream):
color = {k: v.decode('ascii') for k, v in HTMLStream.color.items()}
normal = HTMLStream.normal.decode('ascii')
def __init__(self):
self.clear()
def flush(self):
pass
def prints(self, level, *args, **kwargs):
col = self.color[level]
if col != self.last_col:
if self.data:
self.data.append(self.normal)
self.data.append(col)
self.last_col = col
fobj = kwargs.get('file', sys.stdout)
sep = kwargs.get('sep', ' ')
enc = ('utf-8' if os.getenv('CALIBRE_WORKER')
else constants_old.preferred_encoding)
if isinstance(sep, bytes):
sep = sep.decode(enc)
end = kwargs.get('end', '\n')
for arg in args:
if isinstance(end, bytes):
end = end.decode(enc)
count = 0
for i, arg in enumerate(args):
if isinstance(arg, bytes):
arg = force_unicode(arg)
elif not isinstance(arg, str):
arg = str(arg)
self.data.append(arg+sep)
self.plain_text.append(arg+sep)
self.data.append(end)
self.plain_text.append(end)
arg = arg.decode(enc)
arg = repr(arg)
try:
fobj.write(arg)
count += len(arg)
except Exception:
arg = repr(arg)
fobj.write(arg)
count += len(arg)
if i != len(args)-1:
fobj.write(sep)
count += len(sep)
count += len(end)
def clear(self):
self.data = []
self.plain_text = []
self.last_col = self.color[INFO]
@property
def html(self):
end = self.normal if self.data else ''
return ''.join(self.data) + end
def dump(self):
return [self.data, self.plain_text, self.last_col]
def load(self, dump):
self.data, self.plain_text, self.last_col = dump
def append_dump(self, dump):
d, p, lc = dump
self.data.extend(d)
self.plain_text.extend(p)
self.last_col = lc
def flush(self):
self.stream.flush()
class Log(object):
@@ -192,80 +129,4 @@ class Log(object):
o.close()
class DevNull(Log):
def __init__(self):
Log.__init__(self, level=Log.ERROR)
self.outputs = []
class ThreadSafeLog(Log):
exception_traceback_level = Log.DEBUG
def __init__(self, level=Log.INFO):
Log.__init__(self, level=level)
self._lock = Lock()
def prints(self, *args, **kwargs):
with self._lock:
Log.prints(self, *args, **kwargs)
def print_with_flush(self, *args, **kwargs):
with self._lock:
Log.print_with_flush(self, *args, **kwargs)
def exception(self, *args, **kwargs):
limit = kwargs.pop('limit', None)
with self._lock:
Log.print_with_flush(self, ERROR, *args, **kwargs)
Log.print_with_flush(self, self.exception_traceback_level,
traceback.format_exc(limit))
class ThreadSafeWrapper(Log):
def __init__(self, other_log):
Log.__init__(self, level=other_log.filter_level)
self.outputs = list(other_log.outputs)
self._lock = Lock()
def prints(self, *args, **kwargs):
with self._lock:
Log.prints(self, *args, **kwargs)
def print_with_flush(self, *args, **kwargs):
with self._lock:
Log.print_with_flush(self, *args, **kwargs)
class GUILog(ThreadSafeLog):
"""
Logs in HTML and plain text as unicode. Ideal for display in a GUI context.
"""
def __init__(self):
ThreadSafeLog.__init__(self, level=self.DEBUG)
self.outputs = [UnicodeHTMLStream()]
def clear(self):
self.outputs[0].clear()
@property
def html(self):
return self.outputs[0].html
@property
def plain_text(self):
return u''.join(self.outputs[0].plain_text)
def dump(self):
return self.outputs[0].dump()
def load(self, dump):
return self.outputs[0].load(dump)
def append_dump(self, dump):
return self.outputs[0].append_dump(dump)
default_log = Log()