py3: Use unicode_literals and no str() calls

This commit is contained in:
Kovid Goyal 2019-05-16 12:19:41 +05:30
parent cddb7d873c
commit fab8c8f2d4
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
7 changed files with 20 additions and 20 deletions

View File

@ -8,7 +8,7 @@ __copyright__ = '2013, Kovid Goyal <kovid at kovidgoyal.net>'
import os, time, re import os, time, re
from collections import defaultdict from collections import defaultdict
from polyglot.builtins import itervalues, map, unicode_type from polyglot.builtins import itervalues, map as it_map, unicode_type
from contextlib import contextmanager from contextlib import contextmanager
from functools import partial from functools import partial
@ -69,7 +69,7 @@ def metadata_extensions():
# but not actually added) # but not actually added)
global _metadata_extensions global _metadata_extensions
if _metadata_extensions is None: if _metadata_extensions is None:
_metadata_extensions = frozenset(map(unicode_type, BOOK_EXTENSIONS)) | {'opf'} _metadata_extensions = frozenset(it_map(unicode_type, BOOK_EXTENSIONS)) | {'opf'}
return _metadata_extensions return _metadata_extensions

View File

@ -11,7 +11,7 @@ import os, traceback, random, shutil, operator
from io import BytesIO from io import BytesIO
from collections import defaultdict, Set, MutableSet from collections import defaultdict, Set, MutableSet
from functools import wraps, partial from functools import wraps, partial
from polyglot.builtins import iteritems, itervalues, unicode_type, zip, string_or_bytes, cmp, filter from polyglot.builtins import iteritems, itervalues, unicode_type, zip, string_or_bytes, cmp
from time import time from time import time
from calibre import isbytestring, as_unicode from calibre import isbytestring, as_unicode
@ -186,7 +186,7 @@ class Cache(object):
# There is a chance that these can be duplicates of an existing # There is a chance that these can be duplicates of an existing
# user category. Print the exception and continue. # user category. Print the exception and continue.
try: try:
self.field_metadata.add_user_category(label=u'@' + cat, name=cat) self.field_metadata.add_user_category(label='@' + cat, name=cat)
except ValueError: except ValueError:
traceback.print_exc() traceback.print_exc()
self._ensure_has_search_category() self._ensure_has_search_category()
@ -1513,7 +1513,7 @@ class Cache(object):
for aut in authors: for aut in authors:
aid = rmap.get(key_func(aut), None) aid = rmap.get(key_func(aut), None)
result.append(author_to_author_sort(aut) if aid is None else table.asort_map[aid]) result.append(author_to_author_sort(aut) if aid is None else table.asort_map[aid])
return ' & '.join(filter(None, result)) return ' & '.join(_f for _f in result if _f)
@read_api @read_api
def data_for_has_book(self): def data_for_has_book(self):
@ -1975,7 +1975,7 @@ class Cache(object):
title (title is fuzzy matched). See also :meth:`data_for_find_identical_books`. ''' title (title is fuzzy matched). See also :meth:`data_for_find_identical_books`. '''
from calibre.db.utils import fuzzy_title from calibre.db.utils import fuzzy_title
identical_book_ids = set() identical_book_ids = set()
langq = tuple(filter(lambda x: x and x != 'und', map(canonicalize_lang, mi.languages or ()))) langq = tuple(x for x in map(canonicalize_lang, mi.languages or ()) if x and x != 'und')
if mi.authors: if mi.authors:
try: try:
quathors = mi.authors[:20] # Too many authors causes parsing of the search expression to fail quathors = mi.authors[:20] # Too many authors causes parsing of the search expression to fail

View File

@ -165,8 +165,8 @@ def reinit_db(dbpath):
def debug_device_driver(): def debug_device_driver():
from calibre.devices import debug from calibre.devices import debug
debug(ioreg_to_tmp=True, buf=sys.stdout) debug(ioreg_to_tmp=True, buf=sys.stdout)
if iswindows: if iswindows: # no2to3
raw_input('Press Enter to continue...') raw_input('Press Enter to continue...') # no2to3
def add_simple_plugin(path_to_plugin): def add_simple_plugin(path_to_plugin):

View File

@ -782,7 +782,7 @@ class PostInstall:
self.manifest.append(bash_comp_dest) self.manifest.append(bash_comp_dest)
write_completion(bash_comp_dest, zsh) write_completion(bash_comp_dest, zsh)
except TypeError as err: except TypeError as err:
if 'resolve_entities' in str(err): if 'resolve_entities' in unicode_type(err):
print('You need python-lxml >= 2.0.5 for calibre') print('You need python-lxml >= 2.0.5 for calibre')
sys.exit(1) sys.exit(1)
raise raise
@ -824,11 +824,11 @@ class PostInstall:
def install_single_icon(iconsrc, basename, size, context, is_last_icon=False): def install_single_icon(iconsrc, basename, size, context, is_last_icon=False):
filename = '%s-%s.png' % (basename, size) filename = '%s-%s.png' % (basename, size)
render_img(iconsrc, filename, width=int(size), height=int(size)) render_img(iconsrc, filename, width=int(size), height=int(size))
cmd = ['xdg-icon-resource', 'install', '--noupdate', '--context', context, '--size', str(size), filename, basename] cmd = ['xdg-icon-resource', 'install', '--noupdate', '--context', context, '--size', unicode_type(size), filename, basename]
if is_last_icon: if is_last_icon:
del cmd[2] del cmd[2]
cc(cmd) cc(cmd)
self.icon_resources.append((context, basename, str(size))) self.icon_resources.append((context, basename, unicode_type(size)))
def install_icons(iconsrc, basename, context, is_last_icon=False): def install_icons(iconsrc, basename, context, is_last_icon=False):
sizes = (16, 32, 48, 64, 128, 256) sizes = (16, 32, 48, 64, 128, 256)
@ -1133,7 +1133,7 @@ def write_appdata(key, entry, base, translators):
fpath = os.path.join(base, '%s.appdata.xml' % key) fpath = os.path.join(base, '%s.appdata.xml' % key)
screenshots = E.screenshots() screenshots = E.screenshots()
for w, h, url in entry['screenshots']: for w, h, url in entry['screenshots']:
s = E.screenshot(E.image(url, width=str(w), height=str(h))) s = E.screenshot(E.image(url, width=unicode_type(w), height=unicode_type(h)))
screenshots.append(s) screenshots.append(s)
screenshots[0].set('type', 'default') screenshots[0].set('type', 'default')
description = E.description() description = E.description()

View File

@ -74,7 +74,7 @@ def osx_cache_dir():
l = libc.confstr(65538, ctypes.byref(buf), len(buf)) # _CS_DARWIN_USER_CACHE_DIR = 65538 l = libc.confstr(65538, ctypes.byref(buf), len(buf)) # _CS_DARWIN_USER_CACHE_DIR = 65538
if 0 < l < len(buf): if 0 < l < len(buf):
try: try:
q = buf.value.decode('utf-8').rstrip(u'\0') q = buf.value.decode('utf-8').rstrip('\0')
except ValueError: except ValueError:
pass pass
if q and os.path.isdir(q) and os.access(q, os.R_OK | os.W_OK | os.X_OK): if q and os.path.isdir(q) and os.access(q, os.R_OK | os.W_OK | os.X_OK):
@ -103,7 +103,7 @@ def base_dir():
base = os.environ.get('CALIBRE_TEMP_DIR', None) base = os.environ.get('CALIBRE_TEMP_DIR', None)
if base is not None and iswindows: if base is not None and iswindows:
base = getenv('CALIBRE_TEMP_DIR') base = getenv('CALIBRE_TEMP_DIR')
prefix = app_prefix(u'tmp_') prefix = app_prefix('tmp_')
if base is None: if base is None:
if iswindows: if iswindows:
# On windows, if the TMP env var points to a path that # On windows, if the TMP env var points to a path that
@ -148,12 +148,12 @@ def force_unicode(x):
def _make_file(suffix, prefix, base): def _make_file(suffix, prefix, base):
suffix, prefix = map(force_unicode, (suffix, prefix)) suffix, prefix = map(force_unicode, (suffix, prefix)) # no2to3
return tempfile.mkstemp(suffix, prefix, dir=base) return tempfile.mkstemp(suffix, prefix, dir=base)
def _make_dir(suffix, prefix, base): def _make_dir(suffix, prefix, base):
suffix, prefix = map(force_unicode, (suffix, prefix)) suffix, prefix = map(force_unicode, (suffix, prefix)) # no2to3
return tempfile.mkdtemp(suffix, prefix, base) return tempfile.mkdtemp(suffix, prefix, base)

View File

@ -11,7 +11,7 @@ import pdb, socket, inspect, sys, select, os, atexit, time
from calibre import prints from calibre import prints
from calibre.utils.ipc import eintr_retry_call from calibre.utils.ipc import eintr_retry_call
from calibre.constants import cache_dir from calibre.constants import cache_dir
from polyglot.builtins import range, raw_input from polyglot.builtins import range, raw_input as rinput
PROMPT = b'(debug) ' PROMPT = b'(debug) '
QUESTION = b'\x00\x01\x02' QUESTION = b'\x00\x01\x02'
@ -141,7 +141,7 @@ def cli(port=4444):
stdout.write(recvd) stdout.write(recvd)
raw = b'' raw = b''
try: try:
raw = raw_input(PROMPT.decode('utf-8')) raw = rinput(PROMPT.decode('utf-8'))
except (EOFError, KeyboardInterrupt): except (EOFError, KeyboardInterrupt):
pass pass
else: else:

View File

@ -14,7 +14,7 @@ Test a binary calibre build to ensure that all needed binary images/libraries ha
import os, ctypes, sys, unittest, time import os, ctypes, sys, unittest, time
from calibre.constants import plugins, iswindows, islinux, isosx, ispy3 from calibre.constants import plugins, iswindows, islinux, isosx, ispy3
from polyglot.builtins import iteritems, map, unicode_type, getenv from polyglot.builtins import iteritems, map, unicode_type, getenv, native_string_type
is_ci = os.environ.get('CI', '').lower() == 'true' is_ci = os.environ.get('CI', '').lower() == 'true'
@ -28,7 +28,7 @@ class BuildTest(unittest.TestCase):
for x in os.listdir(base): for x in os.listdir(base):
if x.lower().endswith('.dll'): if x.lower().endswith('.dll'):
try: try:
ctypes.WinDLL(str(os.path.join(base, x))) ctypes.WinDLL(native_string_type(os.path.join(base, x)))
except Exception as err: except Exception as err:
self.assertTrue(False, 'Failed to load DLL %s with error: %s' % (x, err)) self.assertTrue(False, 'Failed to load DLL %s with error: %s' % (x, err))