mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-06-23 15:30:45 -04:00
Get rid of prints()
Not needed in python3
This commit is contained in:
parent
6e774c6831
commit
e9ff40031f
@ -138,79 +138,7 @@ 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
|
||||||
|
|
||||||
def prints(*args, **kwargs):
|
|
||||||
'''
|
|
||||||
Print unicode arguments safely by encoding them to preferred_encoding
|
|
||||||
Has the same signature as the print function from Python 3, except for the
|
|
||||||
additional keyword argument safe_encode, which if set to True will cause the
|
|
||||||
function to use repr when encoding fails.
|
|
||||||
|
|
||||||
Returns the number of bytes written.
|
|
||||||
'''
|
|
||||||
file = kwargs.get('file', sys.stdout)
|
|
||||||
file = getattr(file, 'buffer', file)
|
|
||||||
enc = 'utf-8' if hasenv('CALIBRE_WORKER') else preferred_encoding
|
|
||||||
sep = kwargs.get('sep', ' ')
|
|
||||||
if not isinstance(sep, bytes):
|
|
||||||
sep = sep.encode(enc)
|
|
||||||
end = kwargs.get('end', '\n')
|
|
||||||
if not isinstance(end, bytes):
|
|
||||||
end = end.encode(enc)
|
|
||||||
safe_encode = kwargs.get('safe_encode', False)
|
|
||||||
count = 0
|
|
||||||
for i, arg in enumerate(args):
|
|
||||||
if isinstance(arg, unicode_type):
|
|
||||||
if iswindows:
|
|
||||||
from calibre.utils.terminal import Detect
|
|
||||||
cs = Detect(file)
|
|
||||||
if cs.is_console:
|
|
||||||
cs.write_unicode_text(arg)
|
|
||||||
count += len(arg)
|
|
||||||
if i != len(args)-1:
|
|
||||||
file.write(sep)
|
|
||||||
count += len(sep)
|
|
||||||
continue
|
|
||||||
try:
|
|
||||||
arg = arg.encode(enc)
|
|
||||||
except UnicodeEncodeError:
|
|
||||||
try:
|
|
||||||
arg = arg.encode('utf-8')
|
|
||||||
except:
|
|
||||||
if not safe_encode:
|
|
||||||
raise
|
|
||||||
arg = repr(arg)
|
|
||||||
if not isinstance(arg, bytes):
|
|
||||||
try:
|
|
||||||
arg = native_string_type(arg)
|
|
||||||
except ValueError:
|
|
||||||
arg = unicode_type(arg)
|
|
||||||
if isinstance(arg, unicode_type):
|
|
||||||
try:
|
|
||||||
arg = arg.encode(enc)
|
|
||||||
except UnicodeEncodeError:
|
|
||||||
try:
|
|
||||||
arg = arg.encode('utf-8')
|
|
||||||
except:
|
|
||||||
if not safe_encode:
|
|
||||||
raise
|
|
||||||
arg = repr(arg)
|
|
||||||
|
|
||||||
try:
|
|
||||||
file.write(arg)
|
|
||||||
count += len(arg)
|
|
||||||
except:
|
|
||||||
from polyglot import reprlib
|
|
||||||
arg = reprlib.repr(arg)
|
|
||||||
file.write(arg)
|
|
||||||
count += len(arg)
|
|
||||||
if i != len(args)-1:
|
|
||||||
file.write(sep)
|
|
||||||
count += len(sep)
|
|
||||||
file.write(end)
|
|
||||||
count += len(end)
|
|
||||||
return count
|
|
||||||
|
|
||||||
|
|
||||||
class CommandLineError(Exception):
|
class CommandLineError(Exception):
|
||||||
|
@ -186,7 +186,7 @@ def main(args=sys.argv):
|
|||||||
metadata = unicode_type(mi)
|
metadata = unicode_type(mi)
|
||||||
if trying_to_set:
|
if trying_to_set:
|
||||||
metadata = '\t'+'\n\t'.join(metadata.split('\n'))
|
metadata = '\t'+'\n\t'.join(metadata.split('\n'))
|
||||||
prints(metadata, safe_encode=True)
|
prints(metadata)
|
||||||
|
|
||||||
if trying_to_set:
|
if trying_to_set:
|
||||||
with open(path, 'r+b') as stream:
|
with open(path, 'r+b') as stream:
|
||||||
@ -202,7 +202,7 @@ def main(args=sys.argv):
|
|||||||
prints('\n' + _('Changed metadata') + '::')
|
prints('\n' + _('Changed metadata') + '::')
|
||||||
metadata = unicode_type(mi)
|
metadata = unicode_type(mi)
|
||||||
metadata = '\t'+'\n\t'.join(metadata.split('\n'))
|
metadata = '\t'+'\n\t'.join(metadata.split('\n'))
|
||||||
prints(metadata, safe_encode=True)
|
prints(metadata)
|
||||||
if lrf is not None:
|
if lrf is not None:
|
||||||
prints('\tBookID:', lrf.book_id)
|
prints('\tBookID:', lrf.book_id)
|
||||||
|
|
||||||
|
@ -59,8 +59,7 @@ class DeviceJob(BaseJob): # {{{
|
|||||||
|
|
||||||
def start_work(self):
|
def start_work(self):
|
||||||
if DEBUG:
|
if DEBUG:
|
||||||
prints('Job:', self.id, self.description, 'started',
|
prints('Job:', self.id, self.description, 'started')
|
||||||
safe_encode=True)
|
|
||||||
self.start_time = time.time()
|
self.start_time = time.time()
|
||||||
self.job_manager.changed_queue.put(self)
|
self.job_manager.changed_queue.put(self)
|
||||||
|
|
||||||
@ -69,7 +68,7 @@ class DeviceJob(BaseJob): # {{{
|
|||||||
self.percent = 1
|
self.percent = 1
|
||||||
if DEBUG:
|
if DEBUG:
|
||||||
prints('DeviceJob:', self.id, self.description,
|
prints('DeviceJob:', self.id, self.description,
|
||||||
'done, calling callback', safe_encode=True)
|
'done, calling callback')
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.callback_on_done(self)
|
self.callback_on_done(self)
|
||||||
@ -77,7 +76,7 @@ class DeviceJob(BaseJob): # {{{
|
|||||||
pass
|
pass
|
||||||
if DEBUG:
|
if DEBUG:
|
||||||
prints('DeviceJob:', self.id, self.description,
|
prints('DeviceJob:', self.id, self.description,
|
||||||
'callback returned', safe_encode=True)
|
'callback returned')
|
||||||
self.job_manager.changed_queue.put(self)
|
self.job_manager.changed_queue.put(self)
|
||||||
|
|
||||||
def report_progress(self, percent, msg=''):
|
def report_progress(self, percent, msg=''):
|
||||||
|
@ -328,7 +328,6 @@ class RotatingStream(object):
|
|||||||
self.stream.flush()
|
self.stream.flush()
|
||||||
|
|
||||||
def prints(self, level, *args, **kwargs):
|
def prints(self, level, *args, **kwargs):
|
||||||
kwargs['safe_encode'] = True
|
|
||||||
kwargs['file'] = self.stream
|
kwargs['file'] = self.stream
|
||||||
self.current_pos += prints(*args, **kwargs)
|
self.current_pos += prints(*args, **kwargs)
|
||||||
# line bufferring only works with text mode streams
|
# line bufferring only works with text mode streams
|
||||||
|
@ -60,10 +60,8 @@ class BaseJob(object):
|
|||||||
self._status_text = _('Error') if self.failed else _('Finished')
|
self._status_text = _('Error') if self.failed else _('Finished')
|
||||||
if DEBUG:
|
if DEBUG:
|
||||||
try:
|
try:
|
||||||
prints('Job:', self.id, self.description, 'finished',
|
prints('Job:', self.id, self.description, 'finished')
|
||||||
safe_encode=True)
|
prints('\t'.join(self.details.splitlines(True)))
|
||||||
prints('\t'.join(self.details.splitlines(True)),
|
|
||||||
safe_encode=True)
|
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
if not self._done_called:
|
if not self._done_called:
|
||||||
|
@ -24,7 +24,7 @@ class Stream(object):
|
|||||||
if stream is None:
|
if stream is None:
|
||||||
stream = io.BytesIO()
|
stream = io.BytesIO()
|
||||||
self.stream = getattr(stream, 'buffer', stream)
|
self.stream = getattr(stream, 'buffer', stream)
|
||||||
self._prints = partial(prints, safe_encode=True, file=stream)
|
self._prints = partial(prints, file=stream)
|
||||||
|
|
||||||
def flush(self):
|
def flush(self):
|
||||||
self.stream.flush()
|
self.stream.flush()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user