py3: Use unicode_literals and migrate str() in a few more files

This commit is contained in:
Kovid Goyal 2019-05-19 14:29:14 +05:30
parent 052cb43ae1
commit 14b7f0b51e
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
3 changed files with 14 additions and 13 deletions

View File

@ -1,4 +1,5 @@
#!/usr/bin/env python2 #!/usr/bin/env python2
from __future__ import unicode_literals
__license__ = 'GPL v3' __license__ = 'GPL v3'
__copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>' __copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'
''' '''
@ -36,7 +37,7 @@ def compile_recipe(src):
enc = match.group(1).decode('utf-8') if match else 'utf-8' enc = match.group(1).decode('utf-8') if match else 'utf-8'
src = src.decode(enc) src = src.decode(enc)
# Python complains if there is a coding declaration in a unicode string # Python complains if there is a coding declaration in a unicode string
src = re.sub(r'^#.*coding\s*[:=]\s*([-\w.]+)', '#', src.lstrip(u'\ufeff'), flags=re.MULTILINE) src = re.sub(r'^#.*coding\s*[:=]\s*([-\w.]+)', '#', src.lstrip('\ufeff'), flags=re.MULTILINE)
# Translate newlines to \n # Translate newlines to \n
src = io.StringIO(src, newline=None).getvalue() src = io.StringIO(src, newline=None).getvalue()

View File

@ -1,6 +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 with_statement, print_function from __future__ import with_statement, print_function, unicode_literals
__license__ = 'GPL v3' __license__ = 'GPL v3'
__copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>' __copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
@ -53,7 +53,7 @@ def serialize_recipe(urn, recipe_class):
if ns is True: if ns is True:
ns = 'yes' ns = 'yes'
return E.recipe({ return E.recipe({
'id' : str(urn), 'id' : unicode_type(urn),
'title' : attr('title', _('Unknown')), 'title' : attr('title', _('Unknown')),
'author' : attr('__author__', default_author), 'author' : attr('__author__', default_author),
'language' : attr('language', 'und'), 'language' : attr('language', 'und'),
@ -80,7 +80,7 @@ def serialize_collection(mapping_of_recipe_classes):
traceback.print_exc() traceback.print_exc()
continue continue
collection.append(recipe) collection.append(recipe)
collection.set('count', str(len(collection))) collection.set('count', unicode_type(len(collection)))
return etree.tostring(collection, encoding='utf-8', xml_declaration=True, return etree.tostring(collection, encoding='utf-8', xml_declaration=True,
pretty_print=True) pretty_print=True)
@ -137,7 +137,7 @@ def update_custom_recipes(script_ids):
bdir = os.path.dirname(custom_recipes.file_path) bdir = os.path.dirname(custom_recipes.file_path)
for id_, title, script in script_ids: for id_, title, script in script_ids:
id_ = str(int(id_)) id_ = unicode_type(int(id_))
existing = custom_recipes.get(id_, None) existing = custom_recipes.get(id_, None)
if existing is None: if existing is None:
@ -170,7 +170,7 @@ def add_custom_recipes(script_map):
bdir = os.path.dirname(custom_recipes.file_path) bdir = os.path.dirname(custom_recipes.file_path)
with custom_recipes: with custom_recipes:
for title, script in iteritems(script_map): for title, script in iteritems(script_map):
fid = str(id_) fid = unicode_type(id_)
fname = custom_recipe_filename(fid, title) fname = custom_recipe_filename(fid, title)
if isinstance(script, unicode_type): if isinstance(script, unicode_type):
@ -188,7 +188,7 @@ def add_custom_recipes(script_map):
def remove_custom_recipe(id_): def remove_custom_recipe(id_):
from calibre.web.feeds.recipes import custom_recipes from calibre.web.feeds.recipes import custom_recipes
id_ = str(int(id_)) id_ = unicode_type(int(id_))
existing = custom_recipes.get(id_, None) existing = custom_recipes.get(id_, None)
if existing is not None: if existing is not None:
bdir = os.path.dirname(custom_recipes.file_path) bdir = os.path.dirname(custom_recipes.file_path)
@ -202,7 +202,7 @@ def remove_custom_recipe(id_):
def get_custom_recipe(id_): def get_custom_recipe(id_):
from calibre.web.feeds.recipes import custom_recipes from calibre.web.feeds.recipes import custom_recipes
id_ = str(int(id_)) id_ = unicode_type(int(id_))
existing = custom_recipes.get(id_, None) existing = custom_recipes.get(id_, None)
if existing is not None: if existing is not None:
bdir = os.path.dirname(custom_recipes.file_path) bdir = os.path.dirname(custom_recipes.file_path)
@ -400,7 +400,7 @@ class SchedulerConfig(object):
elif typ == 'day/time': elif typ == 'day/time':
text = '%d:%d:%d'%schedule text = '%d:%d:%d'%schedule
elif typ in ('days_of_week', 'days_of_month'): elif typ in ('days_of_week', 'days_of_month'):
dw = ','.join(map(str, map(int, schedule[0]))) dw = ','.join(map(unicode_type, map(int, schedule[0])))
text = '%s:%d:%d'%(dw, schedule[1], schedule[2]) text = '%s:%d:%d'%(dw, schedule[1], schedule[2])
else: else:
raise ValueError('Unknown schedule type: %r'%typ) raise ValueError('Unknown schedule type: %r'%typ)

View File

@ -1,6 +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 with_statement from __future__ import with_statement, unicode_literals
__license__ = 'GPL v3' __license__ = 'GPL v3'
__copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>' __copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
@ -125,7 +125,7 @@ class NewsItem(NewsTreeItem):
try: try:
with zipfile.ZipFile(self.zf, 'r') as zf: with zipfile.ZipFile(self.zf, 'r') as zf:
p.loadFromData(zf.read(self.favicons[icon])) p.loadFromData(zf.read(self.favicons[icon]))
except: except Exception:
pass pass
if not p.isNull(): if not p.isNull():
self.icon = (QIcon(p)) self.icon = (QIcon(p))
@ -215,7 +215,7 @@ class RecipeModel(QAbstractItemModel, AdaptSQP):
remove_custom_recipe(id_) remove_custom_recipe(id_)
self.custom_recipe_collection = get_custom_recipe_collection() self.custom_recipe_collection = get_custom_recipe_collection()
def do_refresh(self, restrict_to_urns=set([])): def do_refresh(self, restrict_to_urns=frozenset()):
self.custom_recipe_collection = get_custom_recipe_collection() self.custom_recipe_collection = get_custom_recipe_collection()
zf = P('builtin_recipes.zip', allow_user_override=False) zf = P('builtin_recipes.zip', allow_user_override=False)
@ -238,7 +238,7 @@ class RecipeModel(QAbstractItemModel, AdaptSQP):
scheduled = factory(NewsCategory, new_root, _('Scheduled')) scheduled = factory(NewsCategory, new_root, _('Scheduled'))
custom = factory(NewsCategory, new_root, _('Custom')) custom = factory(NewsCategory, new_root, _('Custom'))
lang_map = {} lang_map = {}
self.all_urns = set([]) self.all_urns = set()
self.showing_count = 0 self.showing_count = 0
self.builtin_count = 0 self.builtin_count = 0
for x in self.custom_recipe_collection: for x in self.custom_recipe_collection: