Various fixes for the latest py3 commit

This commit is contained in:
Kovid Goyal 2019-03-21 18:33:19 +05:30
parent 9f31662571
commit 47ed2df137
No known key found for this signature in database
GPG key ID: 06BC317B515ACE7C
5 changed files with 22 additions and 12 deletions

View file

@ -58,7 +58,9 @@ def build_images(self):
for s in sources:
files.append('<file>%s</file>'%s)
manifest = '<RCC>\n<qresource prefix="/">\n%s\n</qresource>\n</RCC>'%'\n'.join(sorted(files))
with open('images.qrc', 'w') as f:
if not isinstance(manifest, bytes):
manifest = manifest.encode('utf-8')
with open('images.qrc', 'wb') as f:
f.write(manifest)
finally:
os.chdir(cwd)

View file

@ -145,7 +145,7 @@ def run(self, opts):
if self.newer(dest, src):
self.info('\tGenerating Kanwadict')
for line in open(src, "r"):
for line in open(src, "rb"):
self.parsekdict(line)
self.kanwaout(dest)
@ -167,8 +167,8 @@ def run(self, opts):
def mkitaiji(self, src, dst):
dic = {}
for line in open(src, "r"):
line = line.strip()
for line in open(src, "rb"):
line = line.decode('utf-8').strip()
if line.startswith(';;'): # skip comment
continue
if re.match(r"^$",line):
@ -181,8 +181,8 @@ def mkitaiji(self, src, dst):
def mkkanadict(self, src, dst):
dic = {}
for line in open(src, "r"):
line = line.strip()
for line in open(src, "rb"):
line = line.decode('utf-8').strip()
if line.startswith(';;'): # skip comment
continue
if re.match(r"^$",line):
@ -194,7 +194,7 @@ def mkkanadict(self, src, dst):
f.write(msgpack_dumps(dic))
def parsekdict(self, line):
line = line.strip()
line = line.decode('utf-8').strip()
if line.startswith(';;'): # skip comment
return
(yomi, kanji) = line.split(' ')

View file

@ -11,7 +11,7 @@
from calibre import relpath, guess_type, remove_bracketed_text, prints, force_unicode
from calibre.utils.config_base import tweaks
from polyglot.builtins import codepoint_to_chr, unicode_type
from polyglot.builtins import codepoint_to_chr, unicode_type, range
from polyglot.urllib import quote, unquote, urlparse

View file

@ -1283,12 +1283,19 @@ def sub(match):
# the qt5 migration
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:
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 not summary:
info('\tCompiling form', form)
buf = io.StringIO()
buf = PolyglotStringIO()
compileUi(form, buf)
dat = buf.getvalue()
dat = dat.replace('import images_rc', '')
@ -1297,8 +1304,9 @@ def sub(match):
dat = dat.replace('_("MMM yyyy")', '"MMM yyyy"')
dat = dat.replace('_("d MMM yyyy")', '"d MMM yyyy"')
dat = pat.sub(sub, dat)
open(compiled_form, 'w').write(dat)
if not isinstance(dat, bytes):
dat = dat.encode('utf-8')
open(compiled_form, 'wb').write(dat)
num += 1
if num:
info('Compiled %d forms' % num)

View file

@ -1000,7 +1000,7 @@ def refresh_ids(self, db, ids):
except IndexError:
return None
try:
return map(self.row, ids)
return list(map(self.row, ids))
except ValueError:
pass
return None