py3: Port more files to not use str() andhave unicode literals

This commit is contained in:
Kovid Goyal 2019-05-20 06:56:45 +05:30
parent a1e75e9bc2
commit f32ea26bf1
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
10 changed files with 29 additions and 22 deletions

View File

@ -141,14 +141,20 @@ class UnicodeCheck(Base):
str_pat = re.compile(r'\bstr\(') str_pat = re.compile(r'\bstr\(')
has_unicode_literals = False has_unicode_literals = False
has_str_calls = False has_str_calls = False
num_lines = 0
for i, line in enumerate(open(f, 'rb')): for i, line in enumerate(open(f, 'rb')):
line = line.decode('utf-8') line = line.decode('utf-8')
if not line.strip():
continue
num_lines += 1
if not has_unicode_literals and uni_pat.match(line) is not None: if not has_unicode_literals and uni_pat.match(line) is not None:
has_unicode_literals = True has_unicode_literals = True
if not has_str_calls and str_pat.search(line) is not None: if not has_str_calls and str_pat.search(line) is not None:
has_str_calls = True has_str_calls = True
if has_unicode_literals and has_str_calls: if has_unicode_literals and has_str_calls:
break break
if num_lines < 1:
return
ans = None ans = None
if not has_unicode_literals: if not has_unicode_literals:
if has_str_calls: if has_str_calls:

View File

@ -17,7 +17,7 @@ from calibre.srv.standalone import create_option_parser
from calibre.srv.utils import create_sock_pair from calibre.srv.utils import create_sock_pair
from calibre.srv.web_socket import DummyHandler from calibre.srv.web_socket import DummyHandler
from calibre.utils.monotonic import monotonic from calibre.utils.monotonic import monotonic
from polyglot.builtins import itervalues, error_message from polyglot.builtins import itervalues, error_message, native_string_type
from polyglot.queue import Queue, Empty from polyglot.queue import Queue, Empty
MAX_RETRIES = 10 MAX_RETRIES = 10
@ -384,7 +384,7 @@ class ReloadServer(Thread):
while not self.loop.ready and self.is_alive(): while not self.loop.ready and self.is_alive():
time.sleep(0.01) time.sleep(0.01)
self.address = self.loop.bound_address[:2] self.address = self.loop.bound_address[:2]
os.environ['CALIBRE_AUTORELOAD_PORT'] = str(self.address[1]) os.environ['CALIBRE_AUTORELOAD_PORT'] = native_string_type(self.address[1])
return self return self
def __exit__(self, *args): def __exit__(self, *args):

View File

@ -63,10 +63,10 @@ def build_search_box(num, search, sort, order, ctx, field_metadata, library_id):
num_select = E.select(name='num') num_select = E.select(name='num')
for option in (5, 10, 25, 100): for option in (5, 10, 25, 100):
kwargs = {'value':str(option)} kwargs = {'value':unicode_type(option)}
if option == num: if option == num:
kwargs['SELECTED'] = 'SELECTED' kwargs['SELECTED'] = 'SELECTED'
num_select.append(E.option(str(option), **kwargs)) num_select.append(E.option(unicode_type(option), **kwargs))
num_select.tail = ' books matching ' num_select.tail = ' books matching '
form.append(num_select) form.append(num_select)

View File

@ -9,7 +9,7 @@ from functools import partial
from calibre import prints from calibre import prints
from calibre.constants import preferred_encoding from calibre.constants import preferred_encoding
from polyglot.builtins import iteritems, raw_input, filter from polyglot.builtins import iteritems, raw_input, filter, unicode_type
# Manage users CLI {{{ # Manage users CLI {{{
@ -39,7 +39,7 @@ def manage_users_cli(path=None):
len(choices), _('default'), default + 1) len(choices), _('default'), default + 1)
reply = get_input(prompt) reply = get_input(prompt)
if not reply and default is not None: if not reply and default is not None:
reply = str(default + 1) reply = unicode_type(default + 1)
if not reply: if not reply:
prints(_('No choice selected, exiting...')) prints(_('No choice selected, exiting...'))
raise SystemExit(0) raise SystemExit(0)

View File

@ -22,7 +22,7 @@ from calibre.utils.icu import collation_order
from calibre.utils.localization import calibre_langcode_to_name from calibre.utils.localization import calibre_langcode_to_name
from calibre.library.comments import comments_to_html, markdown from calibre.library.comments import comments_to_html, markdown
from calibre.library.field_metadata import category_icon_map from calibre.library.field_metadata import category_icon_map
from polyglot.builtins import iteritems, itervalues, range, filter from polyglot.builtins import iteritems, itervalues, range, filter, unicode_type
from polyglot.urllib import quote from polyglot.urllib import quote
IGNORED_FIELDS = frozenset('cover ondevice path marked au_map size'.split()) IGNORED_FIELDS = frozenset('cover ondevice path marked au_map size'.split())
@ -117,7 +117,7 @@ def category_as_json(items, category, display_name, count, tooltip=None, parent=
ans['is_user_category'] = True ans['is_user_category'] = True
if is_first_letter: if is_first_letter:
ans['is_first_letter'] = True ans['is_first_letter'] = True
item_id = 'c' + str(len(items)) item_id = 'c' + unicode_type(len(items))
items[item_id] = ans items[item_id] = ans
return item_id return item_id

View File

@ -1,5 +1,6 @@
#!/usr/bin/env python2 #!/usr/bin/env python2
# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai # vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai
from __future__ import unicode_literals
__license__ = 'GPL v3' __license__ = 'GPL v3'
__copyright__ = '2010, Kovid Goyal <kovid@kovidgoyal.net>' __copyright__ = '2010, Kovid Goyal <kovid@kovidgoyal.net>'
@ -36,7 +37,7 @@ def atom(ctx, rd, endpoint, output):
rd.outheaders.set('Calibre-Instance-Id', force_unicode(prefs['installation_uuid'], 'utf-8'), replace_all=True) rd.outheaders.set('Calibre-Instance-Id', force_unicode(prefs['installation_uuid'], 'utf-8'), replace_all=True)
if isinstance(output, bytes): if isinstance(output, bytes):
ans = output # Assume output is already UTF-8 XML ans = output # Assume output is already UTF-8 XML
elif isinstance(output, type('')): elif isinstance(output, unicode_type):
ans = output.encode('utf-8') ans = output.encode('utf-8')
else: else:
from lxml import etree from lxml import etree
@ -116,7 +117,7 @@ PREVIOUS_LINK = partial(NAVLINK, rel='previous')
def html_to_lxml(raw): def html_to_lxml(raw):
raw = u'<div>%s</div>'%raw raw = '<div>%s</div>'%raw
root = html.fragment_fromstring(raw) root = html.fragment_fromstring(raw)
root.set('xmlns', "http://www.w3.org/1999/xhtml") root.set('xmlns', "http://www.w3.org/1999/xhtml")
raw = etree.tostring(root, encoding=None) raw = etree.tostring(root, encoding=None)
@ -143,7 +144,7 @@ def CATALOG_ENTRY(item, item_kind, request_context, updated, catalog_name,
id_ = 'calibre:category:'+item.name id_ = 'calibre:category:'+item.name
iid = 'N' + item.name iid = 'N' + item.name
if item.id is not None: if item.id is not None:
iid = 'I' + str(item.id) iid = 'I' + unicode_type(item.id)
iid += ':'+item_kind iid += ':'+item_kind
href = request_context.url_for('/opds/category', category=as_hex_unicode(catalog_name), which=as_hex_unicode(iid)) href = request_context.url_for('/opds/category', category=as_hex_unicode(catalog_name), which=as_hex_unicode(iid))
link = NAVLINK(href=href) link = NAVLINK(href=href)
@ -227,7 +228,7 @@ def ACQUISITION_ENTRY(book_id, updated, request_context):
link = E.link(type=mt, href=get(what=fmt), rel="http://opds-spec.org/acquisition") link = E.link(type=mt, href=get(what=fmt), rel="http://opds-spec.org/acquisition")
ffm = fm.get(fmt.upper()) ffm = fm.get(fmt.upper())
if ffm: if ffm:
link.set('length', str(ffm['size'])) link.set('length', unicode_type(ffm['size']))
link.set('mtime', ffm['mtime'].isoformat()) link.set('mtime', ffm['mtime'].isoformat())
ans.append(link) ans.append(link)
ans.append(E.link(type='image/jpeg', href=get(what='cover'), rel="http://opds-spec.org/cover")) ans.append(E.link(type='image/jpeg', href=get(what='cover'), rel="http://opds-spec.org/cover"))
@ -568,7 +569,7 @@ def opds_category(ctx, rd, category, which):
ids = rc.db.get_books_for_category(q, which) ids = rc.db.get_books_for_category(q, which)
sort_by = 'series' if category == 'series' else 'title' sort_by = 'series' if category == 'series' else 'title'
return get_acquisition_feed(rc, ids, offset, page_url, up_url, 'calibre-category:'+category+':'+str(which), sort_by=sort_by) return get_acquisition_feed(rc, ids, offset, page_url, up_url, 'calibre-category:'+category+':'+unicode_type(which), sort_by=sort_by)
@endpoint('/opds/categorygroup/{category}/{which}', postprocess=atom) @endpoint('/opds/categorygroup/{category}/{which}', postprocess=atom)

View File

@ -81,7 +81,7 @@ def transform_declaration(decl):
if unit in absolute_units: if unit in absolute_units:
changed = True changed = True
l = convert_fontsize(l, unit) l = convert_fontsize(l, unit)
decl.change_property(prop, parent_prop, str(l) + 'rem') decl.change_property(prop, parent_prop, unicode_type(l) + 'rem')
return changed return changed

View File

@ -24,7 +24,7 @@ from calibre.srv.utils import RotatingLog
from calibre.utils.config import prefs from calibre.utils.config import prefs
from calibre.utils.localization import localize_user_manual_link from calibre.utils.localization import localize_user_manual_link
from calibre.utils.lock import singleinstance from calibre.utils.lock import singleinstance
from polyglot.builtins import error_message from polyglot.builtins import error_message, unicode_type
def daemonize(): # {{{ def daemonize(): # {{{
@ -231,7 +231,7 @@ def main(args=sys.argv):
daemonize() daemonize()
if opts.pidfile: if opts.pidfile:
with lopen(opts.pidfile, 'wb') as f: with lopen(opts.pidfile, 'wb') as f:
f.write(str(os.getpid())) f.write(unicode_type(os.getpid()).encode('ascii'))
signal.signal(signal.SIGTERM, lambda s, f: server.stop()) signal.signal(signal.SIGTERM, lambda s, f: server.stop())
if not getattr(opts, 'daemonize', False) and not iswindows: if not getattr(opts, 'daemonize', False) and not iswindows:
signal.signal(signal.SIGHUP, lambda s, f: server.stop()) signal.signal(signal.SIGHUP, lambda s, f: server.stop())

View File

@ -15,7 +15,7 @@ from calibre.constants import ispy3
from calibre.srv.tests.base import BaseTest, TestServer from calibre.srv.tests.base import BaseTest, TestServer
from calibre.srv.utils import eintr_retry_call from calibre.srv.utils import eintr_retry_call
from calibre.utils.monotonic import monotonic from calibre.utils.monotonic import monotonic
from polyglot.builtins import iteritems, range from polyglot.builtins import iteritems, range, unicode_type
from polyglot import http_client from polyglot import http_client
is_ci = os.environ.get('CI', '').lower() == 'true' is_ci = os.environ.get('CI', '').lower() == 'true'
@ -172,7 +172,7 @@ class TestHTTP(BaseTest):
r = conn.getresponse() r = conn.getresponse()
self.ae(r.status, http_client.NOT_FOUND) self.ae(r.status, http_client.NOT_FOUND)
self.assertIsNotNone(r.getheader('Date', None)) self.assertIsNotNone(r.getheader('Date', None))
self.ae(r.getheader('Content-Length'), str(len(body))) self.ae(r.getheader('Content-Length'), unicode_type(len(body)))
self.ae(r.getheader('Content-Type'), 'text/plain; charset=UTF-8') self.ae(r.getheader('Content-Type'), 'text/plain; charset=UTF-8')
self.ae(len(r.getheaders()), 3) self.ae(len(r.getheaders()), 3)
self.ae(r.read(), b'') self.ae(r.read(), b'')
@ -338,7 +338,7 @@ class TestHTTP(BaseTest):
conn = server.connect() conn = server.connect()
conn.request('GET', '/an_etagged_path', headers={'Accept-Encoding':'gzip'}) conn.request('GET', '/an_etagged_path', headers={'Accept-Encoding':'gzip'})
r = conn.getresponse() r = conn.getresponse()
self.ae(str(len(raw)), r.getheader('Calibre-Uncompressed-Length')) self.ae(unicode_type(len(raw)), r.getheader('Calibre-Uncompressed-Length'))
self.ae(r.status, http_client.OK), self.ae(zlib.decompress(r.read(), 16+zlib.MAX_WBITS), raw) self.ae(r.status, http_client.OK), self.ae(zlib.decompress(r.read(), 16+zlib.MAX_WBITS), raw)
# Test dynamic etagged content # Test dynamic etagged content

View File

@ -18,7 +18,7 @@ from calibre.srv.tests.base import BaseTest, TestServer
from calibre.ptempfile import TemporaryDirectory from calibre.ptempfile import TemporaryDirectory
from calibre.utils.certgen import create_server_cert from calibre.utils.certgen import create_server_cert
from calibre.utils.monotonic import monotonic from calibre.utils.monotonic import monotonic
from polyglot.builtins import range from polyglot.builtins import range, unicode_type
from polyglot import http_client from polyglot import http_client
is_ci = os.environ.get('CI', '').lower() == 'true' is_ci = os.environ.get('CI', '').lower() == 'true'
@ -94,7 +94,7 @@ class LoopTest(BaseTest):
conn.request('GET', '/') conn.request('GET', '/')
with self.assertRaises(socket.timeout): with self.assertRaises(socket.timeout):
res = conn.getresponse() res = conn.getresponse()
if str(res.status) == str(http_client.REQUEST_TIMEOUT): if unicode_type(res.status) == unicode_type(http_client.REQUEST_TIMEOUT):
raise socket.timeout('Timeout') raise socket.timeout('Timeout')
raise Exception('Got unexpected response: code: %s %s headers: %r data: %r' % ( raise Exception('Got unexpected response: code: %s %s headers: %r data: %r' % (
res.status, res.reason, res.getheaders(), res.read())) res.status, res.reason, res.getheaders(), res.read()))
@ -227,7 +227,7 @@ class LoopTest(BaseTest):
s.bind(('localhost', 0)) s.bind(('localhost', 0))
port = s.getsockname()[1] port = s.getsockname()[1]
self.ae(s.fileno(), 3) self.ae(s.fileno(), 3)
os.environ['LISTEN_PID'] = str(os.getpid()) os.environ['LISTEN_PID'] = unicode_type(os.getpid())
os.environ['LISTEN_FDS'] = '1' os.environ['LISTEN_FDS'] = '1'
with TestServer(lambda data:(data.path[0] + data.read()), allow_socket_preallocation=True) as server: with TestServer(lambda data:(data.path[0] + data.read()), allow_socket_preallocation=True) as server:
conn = server.connect() conn = server.connect()