mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Restore prints and use it instead of polyglot write
This commit is contained in:
parent
4cce5c7006
commit
e1fdbd3982
@ -22,6 +22,7 @@ from calibre.constants import (iswindows, isosx, islinux, isfrozen,
|
|||||||
filesystem_encoding, plugins, config_dir)
|
filesystem_encoding, plugins, config_dir)
|
||||||
from calibre.startup import winutil, winutilerror
|
from calibre.startup import winutil, winutilerror
|
||||||
from calibre.utils.icu import safe_chr
|
from calibre.utils.icu import safe_chr
|
||||||
|
from calibre.prints import prints
|
||||||
|
|
||||||
if False:
|
if False:
|
||||||
# Prevent pyflakes from complaining
|
# Prevent pyflakes from complaining
|
||||||
@ -138,7 +139,6 @@ def sanitize_file_name(name, substitute='_'):
|
|||||||
|
|
||||||
|
|
||||||
sanitize_file_name2 = sanitize_file_name_unicode = sanitize_file_name
|
sanitize_file_name2 = sanitize_file_name_unicode = sanitize_file_name
|
||||||
prints = print
|
|
||||||
|
|
||||||
|
|
||||||
class CommandLineError(Exception):
|
class CommandLineError(Exception):
|
||||||
|
31
src/calibre/prints.py
Normal file
31
src/calibre/prints.py
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
# vim:fileencoding=utf-8
|
||||||
|
# License: GPL v3 Copyright: 2019, Kovid Goyal <kovid at kovidgoyal.net>
|
||||||
|
|
||||||
|
import io
|
||||||
|
import sys
|
||||||
|
|
||||||
|
from polyglot.builtins import as_bytes, as_unicode
|
||||||
|
|
||||||
|
|
||||||
|
def is_binary(stream):
|
||||||
|
mode = getattr(stream, 'mode', None)
|
||||||
|
if mode:
|
||||||
|
return 'b' in mode
|
||||||
|
return not isinstance(stream, io.TextIOBase)
|
||||||
|
|
||||||
|
|
||||||
|
def prints(*a, **kw):
|
||||||
|
' Print either unicode or bytes to either binary or text mode streams '
|
||||||
|
stream = kw.get('file', sys.stdout)
|
||||||
|
if is_binary(stream):
|
||||||
|
encoding = getattr(stream, 'encoding', None) or 'utf-8'
|
||||||
|
a = tuple(as_bytes(x, encoding=encoding) for x in a)
|
||||||
|
kw['sep'] = as_bytes(kw.get('sep', b' '))
|
||||||
|
kw['end'] = as_bytes(kw.get('end', b'\n'))
|
||||||
|
else:
|
||||||
|
a = tuple(as_unicode(x, errors='replace') for x in a)
|
||||||
|
kw['sep'] = as_unicode(kw.get('sep', ' '))
|
||||||
|
kw['end'] = as_unicode(kw.get('end', '\n'))
|
||||||
|
|
||||||
|
return print(*a, **kw)
|
@ -36,7 +36,6 @@ class LoopTest(BaseTest):
|
|||||||
|
|
||||||
def log_size():
|
def log_size():
|
||||||
ssize = l.outputs[0].stream.tell()
|
ssize = l.outputs[0].stream.tell()
|
||||||
self.ae(ssize, l.outputs[0].current_pos)
|
|
||||||
self.ae(ssize, os.path.getsize(fname))
|
self.ae(ssize, os.path.getsize(fname))
|
||||||
return ssize
|
return ssize
|
||||||
|
|
||||||
|
@ -12,7 +12,6 @@ from operator import itemgetter
|
|||||||
|
|
||||||
from calibre import prints
|
from calibre import prints
|
||||||
from calibre.constants import iswindows
|
from calibre.constants import iswindows
|
||||||
from calibre.utils.terminal import polyglot_write
|
|
||||||
from calibre.srv.errors import HTTPNotFound
|
from calibre.srv.errors import HTTPNotFound
|
||||||
from calibre.utils.localization import get_translator
|
from calibre.utils.localization import get_translator
|
||||||
from calibre.utils.socket_inheritance import set_socket_inherit
|
from calibre.utils.socket_inheritance import set_socket_inherit
|
||||||
@ -314,36 +313,22 @@ class RotatingStream(object):
|
|||||||
|
|
||||||
def set_output(self):
|
def set_output(self):
|
||||||
if iswindows:
|
if iswindows:
|
||||||
self.stream = share_open(self.filename, 'ab')
|
self.stream = share_open(self.filename, 'a')
|
||||||
else:
|
else:
|
||||||
# see https://bugs.python.org/issue27805
|
# see https://bugs.python.org/issue27805
|
||||||
self.stream = open(os.open(self.filename, os.O_WRONLY|os.O_APPEND|os.O_CREAT|os.O_CLOEXEC), 'wb')
|
self.stream = open(os.open(self.filename, os.O_WRONLY|os.O_APPEND|os.O_CREAT|os.O_CLOEXEC), 'w')
|
||||||
try:
|
try:
|
||||||
self.current_pos = self.stream.tell()
|
self.stream.tell()
|
||||||
except EnvironmentError:
|
except EnvironmentError:
|
||||||
# Happens if filename is /dev/stdout for example
|
# Happens if filename is /dev/stdout for example
|
||||||
self.current_pos = 0
|
|
||||||
self.max_size = None
|
self.max_size = None
|
||||||
|
|
||||||
def flush(self):
|
def flush(self):
|
||||||
self.stream.flush()
|
self.stream.flush()
|
||||||
|
|
||||||
def write(self, x):
|
|
||||||
return polyglot_write(self.stream, True, 'utf-8', x)
|
|
||||||
|
|
||||||
def prints(self, level, *args, **kwargs):
|
def prints(self, level, *args, **kwargs):
|
||||||
kwargs['file'] = self
|
kwargs['file'] = self.stream
|
||||||
prints(*args, **kwargs)
|
prints(*args, **kwargs)
|
||||||
try:
|
|
||||||
self.current_pos = self.stream.tell()
|
|
||||||
except EnvironmentError:
|
|
||||||
self.current_pos = 0
|
|
||||||
# line bufferring only works with text mode streams
|
|
||||||
end = kwargs.get('end', b'\n')
|
|
||||||
if isinstance(end, unicode_type):
|
|
||||||
end = end.encode('utf-8')
|
|
||||||
if b'\n' in end:
|
|
||||||
self.flush()
|
|
||||||
self.rollover()
|
self.rollover()
|
||||||
|
|
||||||
def rename(self, src, dest):
|
def rename(self, src, dest):
|
||||||
@ -361,7 +346,7 @@ class RotatingStream(object):
|
|||||||
raise
|
raise
|
||||||
|
|
||||||
def rollover(self):
|
def rollover(self):
|
||||||
if not self.max_size or self.current_pos <= self.max_size or self.filename in ('/dev/stdout', '/dev/stderr'):
|
if not self.max_size or self.stream.tell() <= self.max_size:
|
||||||
return
|
return
|
||||||
self.stream.close()
|
self.stream.close()
|
||||||
for i in range(self.history - 1, 0, -1):
|
for i in range(self.history - 1, 0, -1):
|
||||||
|
@ -15,9 +15,8 @@ import traceback
|
|||||||
from functools import partial
|
from functools import partial
|
||||||
from threading import Lock
|
from threading import Lock
|
||||||
|
|
||||||
from calibre import as_unicode, force_unicode, isbytestring
|
from calibre.prints import prints
|
||||||
from calibre.utils.terminal import is_binary, polyglot_write
|
from polyglot.builtins import as_unicode
|
||||||
from polyglot.builtins import iteritems, unicode_type
|
|
||||||
|
|
||||||
|
|
||||||
class Stream(object):
|
class Stream(object):
|
||||||
@ -26,12 +25,11 @@ class Stream(object):
|
|||||||
if stream is None:
|
if stream is None:
|
||||||
stream = io.StringIO()
|
stream = io.StringIO()
|
||||||
self.stream = stream
|
self.stream = stream
|
||||||
self.is_binary = is_binary(self.stream)
|
|
||||||
self.encoding = getattr(self.stream, 'encoding', None) or 'utf-8'
|
self.encoding = getattr(self.stream, 'encoding', None) or 'utf-8'
|
||||||
self._prints = partial(print, file=self)
|
self._prints = partial(prints, file=self.stream)
|
||||||
|
|
||||||
def write(self, text):
|
def write(self, text):
|
||||||
return polyglot_write(self.stream, self.is_binary, self.encoding, text)
|
self._prints(text, end='')
|
||||||
|
|
||||||
def flush(self):
|
def flush(self):
|
||||||
self.stream.flush()
|
self.stream.flush()
|
||||||
@ -72,21 +70,20 @@ class FileStream(Stream):
|
|||||||
class HTMLStream(Stream):
|
class HTMLStream(Stream):
|
||||||
|
|
||||||
color = {
|
color = {
|
||||||
DEBUG: b'<span style="color:green">',
|
DEBUG: '<span style="color:green">',
|
||||||
INFO: b'<span>',
|
INFO: '<span>',
|
||||||
WARN: b'<span style="color:blue">',
|
WARN: '<span style="color:blue">',
|
||||||
ERROR: b'<span style="color:red">'
|
ERROR: '<span style="color:red">'
|
||||||
}
|
}
|
||||||
normal = b'</span>'
|
normal = '</span>'
|
||||||
|
|
||||||
def __init__(self, stream=sys.stdout):
|
def __init__(self, stream=sys.stdout):
|
||||||
Stream.__init__(self, stream)
|
Stream.__init__(self, stream)
|
||||||
|
|
||||||
def prints(self, level, *args, **kwargs):
|
def prints(self, level, *args, **kwargs):
|
||||||
self.stream.write(self.color[level])
|
self._prints(self.color[level], end='')
|
||||||
kwargs['file'] = self.stream
|
|
||||||
self._prints(*args, **kwargs)
|
self._prints(*args, **kwargs)
|
||||||
self.stream.write(self.normal)
|
self._prints(self.normal, end='')
|
||||||
|
|
||||||
def flush(self):
|
def flush(self):
|
||||||
self.stream.flush()
|
self.stream.flush()
|
||||||
@ -94,9 +91,6 @@ class HTMLStream(Stream):
|
|||||||
|
|
||||||
class UnicodeHTMLStream(HTMLStream):
|
class UnicodeHTMLStream(HTMLStream):
|
||||||
|
|
||||||
color = {k: v.decode('ascii') for k, v in iteritems(HTMLStream.color)}
|
|
||||||
normal = HTMLStream.normal.decode('ascii')
|
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.clear()
|
self.clear()
|
||||||
|
|
||||||
@ -111,13 +105,10 @@ class UnicodeHTMLStream(HTMLStream):
|
|||||||
self.data.append(col)
|
self.data.append(col)
|
||||||
self.last_col = col
|
self.last_col = col
|
||||||
|
|
||||||
sep = kwargs.get(u'sep', u' ')
|
sep = kwargs.get('sep', ' ')
|
||||||
end = kwargs.get(u'end', u'\n')
|
end = kwargs.get('end', '\n')
|
||||||
|
|
||||||
for arg in args:
|
for arg in args:
|
||||||
if isbytestring(arg):
|
|
||||||
arg = force_unicode(arg)
|
|
||||||
elif not isinstance(arg, unicode_type):
|
|
||||||
arg = as_unicode(arg)
|
arg = as_unicode(arg)
|
||||||
self.data.append(arg+sep)
|
self.data.append(arg+sep)
|
||||||
self.plain_text.append(arg+sep)
|
self.plain_text.append(arg+sep)
|
||||||
@ -131,8 +122,8 @@ class UnicodeHTMLStream(HTMLStream):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def html(self):
|
def html(self):
|
||||||
end = self.normal if self.data else u''
|
end = self.normal if self.data else ''
|
||||||
return u''.join(self.data) + end
|
return ''.join(self.data) + end
|
||||||
|
|
||||||
def dump(self):
|
def dump(self):
|
||||||
return [self.data, self.plain_text, self.last_col]
|
return [self.data, self.plain_text, self.last_col]
|
||||||
|
@ -5,8 +5,9 @@ __license__ = 'GPL v3'
|
|||||||
__copyright__ = '2012, Kovid Goyal <kovid at kovidgoyal.net>'
|
__copyright__ = '2012, Kovid Goyal <kovid at kovidgoyal.net>'
|
||||||
__docformat__ = 'restructuredtext en'
|
__docformat__ = 'restructuredtext en'
|
||||||
|
|
||||||
import os, sys, re, io
|
import os, sys, re
|
||||||
|
|
||||||
|
from calibre.prints import is_binary
|
||||||
from calibre.constants import iswindows
|
from calibre.constants import iswindows
|
||||||
from polyglot.builtins import iteritems, range, zip
|
from polyglot.builtins import iteritems, range, zip
|
||||||
|
|
||||||
@ -101,13 +102,6 @@ def colored(text, fg=None, bg=None, bold=False):
|
|||||||
return prefix + text + suffix
|
return prefix + text + suffix
|
||||||
|
|
||||||
|
|
||||||
def is_binary(stream):
|
|
||||||
mode = getattr(stream, 'mode', None)
|
|
||||||
if mode:
|
|
||||||
return 'b' in mode
|
|
||||||
return not isinstance(stream, io.TextIOBase)
|
|
||||||
|
|
||||||
|
|
||||||
class Detect(object):
|
class Detect(object):
|
||||||
|
|
||||||
def __init__(self, stream):
|
def __init__(self, stream):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user