mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-06-23 15:30:45 -04:00
Use a polyglot IO class for device debug output
This commit is contained in:
parent
57e11977b7
commit
0db9c42360
@ -8,7 +8,6 @@ Device drivers.
|
|||||||
|
|
||||||
import sys, time, pprint
|
import sys, time, pprint
|
||||||
from functools import partial
|
from functools import partial
|
||||||
from io import BytesIO
|
|
||||||
|
|
||||||
DAY_MAP = dict(Sun=0, Mon=1, Tue=2, Wed=3, Thu=4, Fri=5, Sat=6)
|
DAY_MAP = dict(Sun=0, Mon=1, Tue=2, Wed=3, Thu=4, Fri=5, Sat=6)
|
||||||
MONTH_MAP = dict(Jan=1, Feb=2, Mar=3, Apr=4, May=5, Jun=6, Jul=7, Aug=8, Sep=9, Oct=10, Nov=11, Dec=12)
|
MONTH_MAP = dict(Jan=1, Feb=2, Mar=3, Apr=4, May=5, Jun=6, Jul=7, Aug=8, Sep=9, Oct=10, Nov=11, Dec=12)
|
||||||
@ -74,10 +73,11 @@ def debug(ioreg_to_tmp=False, buf=None, plugins=None,
|
|||||||
from calibre.devices.scanner import DeviceScanner
|
from calibre.devices.scanner import DeviceScanner
|
||||||
from calibre.constants import iswindows, isosx
|
from calibre.constants import iswindows, isosx
|
||||||
from calibre import prints
|
from calibre import prints
|
||||||
|
from polyglot.io import PolyglotBytesIO
|
||||||
oldo, olde = sys.stdout, sys.stderr
|
oldo, olde = sys.stdout, sys.stderr
|
||||||
|
|
||||||
if buf is None:
|
if buf is None:
|
||||||
buf = BytesIO()
|
buf = PolyglotBytesIO()
|
||||||
sys.stdout = sys.stderr = buf
|
sys.stdout = sys.stderr = buf
|
||||||
out = partial(prints, file=buf)
|
out = partial(prints, file=buf)
|
||||||
|
|
||||||
|
@ -1264,8 +1264,9 @@ def form_to_compiled_form(form):
|
|||||||
|
|
||||||
|
|
||||||
def build_forms(srcdir, info=None, summary=False, check_for_migration=False):
|
def build_forms(srcdir, info=None, summary=False, check_for_migration=False):
|
||||||
import re, io
|
import re
|
||||||
from PyQt5.uic import compileUi
|
from PyQt5.uic import compileUi
|
||||||
|
from polyglot.io import PolyglotStringIO
|
||||||
forms = find_forms(srcdir)
|
forms = find_forms(srcdir)
|
||||||
if info is None:
|
if info is None:
|
||||||
from calibre import prints
|
from calibre import prints
|
||||||
@ -1284,13 +1285,6 @@ def build_forms(srcdir, info=None, summary=False, check_for_migration=False):
|
|||||||
# the qt5 migration
|
# the qt5 migration
|
||||||
force_compile = check_for_migration and not gprefs.get('migrated_forms_to_qt5', False)
|
force_compile = check_for_migration and not gprefs.get('migrated_forms_to_qt5', False)
|
||||||
|
|
||||||
class PolyglotStringIO(io.StringIO):
|
|
||||||
|
|
||||||
def write(self, x):
|
|
||||||
if isinstance(x, bytes):
|
|
||||||
x = x.decode('utf-8')
|
|
||||||
io.StringIO.write(self, x)
|
|
||||||
|
|
||||||
for form in forms:
|
for form in forms:
|
||||||
compiled_form = form_to_compiled_form(form)
|
compiled_form = form_to_compiled_form(form)
|
||||||
if force_compile or not os.path.exists(compiled_form) or os.stat(form).st_mtime > os.stat(compiled_form).st_mtime:
|
if force_compile or not os.path.exists(compiled_form) or os.stat(form).st_mtime > os.stat(compiled_form).st_mtime:
|
||||||
|
35
src/polyglot/io.py
Normal file
35
src/polyglot/io.py
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
#!/usr/bin/env python2
|
||||||
|
# vim:fileencoding=utf-8
|
||||||
|
# License: GPL v3 Copyright: 2019, Kovid Goyal <kovid at kovidgoyal.net>
|
||||||
|
|
||||||
|
from __future__ import absolute_import, division, print_function, unicode_literals
|
||||||
|
|
||||||
|
from io import StringIO, BytesIO
|
||||||
|
|
||||||
|
|
||||||
|
class PolyglotStringIO(StringIO):
|
||||||
|
|
||||||
|
def __init__(self, initial_data=None, encoding='utf-8'):
|
||||||
|
StringIO.__init__(self)
|
||||||
|
self._encoding_for_bytes = encoding
|
||||||
|
if initial_data is not None:
|
||||||
|
self.write(initial_data)
|
||||||
|
|
||||||
|
def write(self, x):
|
||||||
|
if isinstance(x, bytes):
|
||||||
|
x = x.decode(self._encoding_for_bytes)
|
||||||
|
StringIO.write(self, x)
|
||||||
|
|
||||||
|
|
||||||
|
class PolyglotBytesIO(BytesIO):
|
||||||
|
|
||||||
|
def __init__(self, initial_data=None, encoding='utf-8'):
|
||||||
|
BytesIO.__init__(self)
|
||||||
|
self._encoding_for_bytes = encoding
|
||||||
|
if initial_data is not None:
|
||||||
|
self.write(initial_data)
|
||||||
|
|
||||||
|
def write(self, x):
|
||||||
|
if not isinstance(x, bytes):
|
||||||
|
x = x.encode(self._encoding_for_bytes)
|
||||||
|
BytesIO.write(self, x)
|
Loading…
x
Reference in New Issue
Block a user