automated fixes by pyupgrade to py 3.8

This commit is contained in:
Kovid Goyal 2023-11-23 09:05:12 +05:30
parent c08be4e428
commit 85a0056e6d
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
36 changed files with 28 additions and 49 deletions

View File

@ -13,7 +13,7 @@ CSIDL_PROGRAM_FILES = 38
CSIDL_PROGRAM_FILESX86 = 42
@lru_cache()
@lru_cache
def get_program_files_location(which=CSIDL_PROGRAM_FILESX86):
SHGFP_TYPE_CURRENT = 0
buf = ctypes.create_unicode_buffer(ctypes.wintypes.MAX_PATH)
@ -22,7 +22,7 @@ def get_program_files_location(which=CSIDL_PROGRAM_FILESX86):
return buf.value
@lru_cache()
@lru_cache
def find_vswhere():
for which in (CSIDL_PROGRAM_FILESX86, CSIDL_PROGRAM_FILES):
root = get_program_files_location(which)
@ -37,7 +37,7 @@ def get_output(*cmd):
return subprocess.check_output(cmd, encoding='mbcs', errors='strict')
@lru_cache()
@lru_cache
def find_visual_studio():
path = get_output(
find_vswhere(),
@ -52,7 +52,7 @@ def find_visual_studio():
return os.path.join(path, "VC", "Auxiliary", "Build")
@lru_cache()
@lru_cache
def find_msbuild():
base_path = get_output(
find_vswhere(),
@ -113,12 +113,12 @@ def query_process(cmd, is64bit):
return result
@lru_cache()
@lru_cache
def query_vcvarsall(is64bit=True):
plat = 'amd64' if is64bit else 'amd64_x86'
vcvarsall = find_vcvarsall()
env = query_process(f'"{vcvarsall}" {plat} & set', is64bit)
pat = re.compile('vs(\d+)comntools', re.I)
pat = re.compile(r'vs(\d+)comntools', re.I)
comn_tools = {}

View File

@ -759,7 +759,7 @@ def initialize_plugins(perf=False):
if perf:
from collections import defaultdict
import time
times = defaultdict(lambda:0)
times = defaultdict(int)
for zfp, installation_type in chain(
zip_value(external_plugins.items(), PluginInstallationType.EXTERNAL),

View File

@ -59,7 +59,7 @@ class BackupProgress:
)
else:
prints(
'{:.1f}% {} failed'.format((self.count * 100) / float(self.total), book_id))
f'{(self.count * 100) / float(self.total):.1f}% {book_id} failed')
def main(opts, args, dbctx):

View File

@ -1,5 +1,4 @@
#!/usr/bin/env python
# vim:fileencoding=utf-8
# License: GPL v3 Copyright: 2022, Kovid Goyal <kovid at kovidgoyal.net>

View File

@ -1,5 +1,4 @@
#!/usr/bin/env python
# vim:fileencoding=utf-8
# License: GPL v3 Copyright: 2022, Kovid Goyal <kovid at kovidgoyal.net>

View File

@ -1,5 +1,4 @@
#!/usr/bin/env python
# vim:fileencoding=utf-8
# License: GPL v3 Copyright: 2022, Kovid Goyal <kovid at kovidgoyal.net>
from calibre.utils.resources import get_path as P

View File

@ -1,5 +1,4 @@
#!/usr/bin/env python
# vim:fileencoding=utf-8
# License: GPL v3 Copyright: 2022, Kovid Goyal <kovid at kovidgoyal.net>

View File

@ -102,7 +102,7 @@ class FTSAPITest(BaseTest):
# check enabling scans pre-exisintg
cache = self.new_library()
cache.add_format(1, 'TXTZ', self.make_txtz('a test te\u00adxt'.encode('utf-8')))
cache.add_format(1, 'TXTZ', self.make_txtz('a test te\u00adxt'.encode()))
fts = cache.enable_fts()
self.wait_for_fts_to_finish(fts)
check(id=1, book=1, format='TXTZ', searchable_text='a test text')

View File

@ -54,4 +54,4 @@ class PageGroup:
values = str(self.__first_value)
else:
values = "|".join(self.__page_number_labels)
return "(%s,%s,%s)" % (starting_location, self.__page_number_type.value, values)
return "({},{},{})".format(starting_location, self.__page_number_type.value, values)

View File

@ -212,7 +212,7 @@ class KOBO(USBMS):
debug_print(f"device_version_info - version_file={version_file}")
if os.path.isfile(version_file):
debug_print("device_version_info - have opened version_file")
vf = open(version_file, "r")
vf = open(version_file)
self._device_version_info = vf.read().strip().split(",")
vf.close()
debug_print("device_version_info - self._device_version_info=", self._device_version_info)

View File

@ -87,7 +87,7 @@ def option_recommendation_to_cli_option(add_option, rec):
switches.append('--'+opt.long_switch)
attrs = dict(dest=opt.name, help=opt.help,
choices=opt.choices, default=rec.recommended_value)
if isinstance(rec.recommended_value, type(True)):
if isinstance(rec.recommended_value, bool):
attrs['action'] = 'store_false' if rec.recommended_value else \
'store_true'
else:

View File

@ -277,7 +277,7 @@ class HTMLInput(InputFormatPlugin):
if not q.startswith(self.root_dir_of_input):
if not self.opts.allow_local_files_outside_root:
if os.path.exists(q):
self.log.warn('Not adding {} as it is outside the document root: {}'.format(q, self.root_dir_of_input))
self.log.warn(f'Not adding {q} as it is outside the document root: {self.root_dir_of_input}')
return None, None
return link, frag

View File

@ -133,7 +133,7 @@ def mi_to_html(
note = ''
item_id = None if item_id_if_has_note is None else item_id_if_has_note(field, field_value)
if item_id is not None:
note = ' <a title="{0}" href="{1}">{2}</a>'.format(
note = ' <a title="{}" href="{}">{}</a>'.format(
_('Show notes for: {}').format(field_value), notes_action(field=field, value=field_value, item_id=item_id), note_markup)
return link + note
return ''

View File

@ -12,7 +12,7 @@ from calibre.ebooks.metadata import MetaInformation, string_to_authors
# The priorities for loading metadata from different file types
# Higher values should be used to update metadata from lower values
METADATA_PRIORITIES = collections.defaultdict(lambda:0)
METADATA_PRIORITIES = collections.defaultdict(int)
for i, ext in enumerate((
'html', 'htm', 'xhtml', 'xhtm',
'rtf', 'fb2', 'pdf', 'prc', 'odt',

View File

@ -38,7 +38,7 @@ class FDST:
def __str__(self):
ans = ['FDST record']
def a(k, v):
return ans.append('%s: %s' % (k, v))
return ans.append('{}: {}'.format(k, v))
a('Offset to sections', self.sec_off)
a('Number of section records', self.num_sections)
ans.append('**** %d Sections ****'% len(self.sections))

View File

@ -1,5 +1,4 @@
#!/usr/bin/env python
# vim:fileencoding=utf-8
# License: GPL v3 Copyright: 2022, Kovid Goyal <kovid at kovidgoyal.net>

View File

@ -1,5 +1,4 @@
#!/usr/bin/env python
# vim:fileencoding=utf-8
# License: GPL v3 Copyright: 2022, Kovid Goyal <kovid at kovidgoyal.net>
import os

View File

@ -1,5 +1,4 @@
#!/usr/bin/env python
# vim:fileencoding=utf-8
# License: GPL v3 Copyright: 2022, Kovid Goyal <kovid at kovidgoyal.net>

View File

@ -1,5 +1,4 @@
#!/usr/bin/env python
# vim:fileencoding=utf-8
# License: GPL v3 Copyright: 2022, Kovid Goyal <kovid at kovidgoyal.net>

View File

@ -1,5 +1,4 @@
#!/usr/bin/env python
# vim:fileencoding=utf-8
# License: GPL v3 Copyright: 2022, Kovid Goyal <kovid at kovidgoyal.net>

View File

@ -1,5 +1,4 @@
#!/usr/bin/env python
# vim:fileencoding=utf-8
# License: GPL v3 Copyright: 2021, Kovid Goyal <kovid at kovidgoyal.net>
# Workaround a bunch of brain dead changes in PyQt6 that break backwards compat

View File

@ -1,5 +1,4 @@
#!/usr/bin/env python
# vim:fileencoding=utf-8
# License: GPL v3 Copyright: 2022, Kovid Goyal <kovid at kovidgoyal.net>
from qt.core import QUrl
@ -48,8 +47,7 @@ class AmazonStore:
open_url(QUrl(store_link))
def search(self, query, max_results=10, timeout=60):
for result in get_method('search_amazon')(self, query, max_results=max_results, timeout=timeout):
yield result
yield from get_method('search_amazon')(self, query, max_results=max_results, timeout=timeout)
def get_details(self, search_result, timeout):
return get_method('get_details_amazon')(self, search_result, timeout)

View File

@ -1,5 +1,4 @@
#!/usr/bin/env python
# vim:fileencoding=utf-8
# License: GPL v3 Copyright: 2022, Kovid Goyal <kovid at kovidgoyal.net>
@ -19,7 +18,7 @@ def search_amazon(self, query, max_results=10, timeout=60, write_html_to=None):
uquery[field_keywords] = query
def asbytes(x):
if isinstance(x, type('')):
if isinstance(x, str):
x = x.encode('utf-8')
return x
uquery = {asbytes(k):asbytes(v) for k, v in uquery.items()}
@ -32,7 +31,7 @@ def search_amazon(self, query, max_results=10, timeout=60, write_html_to=None):
f.write(raw)
doc = html.fromstring(raw)
for result in doc.xpath('//div[contains(@class, "s-result-list")]//div[@data-index and @data-asin]'):
kformat = ''.join(result.xpath('.//a[contains(text(), "{}")]//text()'.format(self.KINDLE_EDITION)))
kformat = ''.join(result.xpath(f'.//a[contains(text(), "{self.KINDLE_EDITION}")]//text()'))
# Even though we are searching digital-text only Amazon will still
# put in results for non Kindle books (author pages). So we need
# to explicitly check if the item is a Kindle book and ignore it

View File

@ -1,5 +1,4 @@
#!/usr/bin/env python
# vim:fileencoding=utf-8
# License: GPL v3 Copyright: 2022, Kovid Goyal <kovid at kovidgoyal.net>

View File

@ -1,5 +1,4 @@
#!/usr/bin/env python
# vim:fileencoding=utf-8
# License: GPL v3 Copyright: 2022, Kovid Goyal <kovid at kovidgoyal.net>
import json

View File

@ -1,5 +1,4 @@
#!/usr/bin/env python
# vim:fileencoding=utf-8
# License: GPL v3 Copyright: 2022, Kovid Goyal <kovid at kovidgoyal.net>
import re

View File

@ -1,5 +1,4 @@
#!/usr/bin/env python
# vim:fileencoding=utf-8
# License: GPL v3 Copyright: 2022, Kovid Goyal <kovid at kovidgoyal.net>
import apsw

View File

@ -845,7 +845,7 @@ class StopException(Exception):
super().__init__('Template evaluation stopped')
class PythonTemplateContext(object):
class PythonTemplateContext:
def __init__(self):
# Set attributes we already know must exist.

View File

@ -1,5 +1,4 @@
#!/usr/bin/env python
# vim:fileencoding=utf-8
# License: GPL v3 Copyright: 2022, Kovid Goyal <kovid at kovidgoyal.net>
import os, io

View File

@ -189,7 +189,7 @@ class TestImports(unittest.TestCase):
if not isbsd:
exclude_modules.add('calibre.devices.usbms.hal')
d = os.path.dirname
SRC = d(d(d((os.path.abspath(__file__)))))
SRC = d(d(d(os.path.abspath(__file__))))
self.assertGreater(self.base_check(os.path.join(SRC, 'odf'), exclude_packages, exclude_modules), 10)
base = os.path.join(SRC, 'calibre')
self.assertGreater(self.base_check(base, exclude_packages, exclude_modules), 1000)

View File

@ -1,5 +1,4 @@
#!/usr/bin/env python
# vim:fileencoding=utf-8
# License: GPL v3 Copyright: 2022, Kovid Goyal <kovid at kovidgoyal.net>

View File

@ -1,5 +1,4 @@
#!/usr/bin/env python
# vim:fileencoding=utf-8
# License: GPL v3 Copyright: 2021, Kovid Goyal <kovid at kovidgoyal.net>

View File

@ -401,7 +401,7 @@ class WinSpeech:
if DEBUG:
with suppress(Exception):
print('winspeech:\x1b[31m->\x1b[39m', cmd, flush=True)
w.stdin.write(f'{cmd}\n'.encode('utf-8'))
w.stdin.write(f'{cmd}\n'.encode())
w.stdin.flush()
return cmd_id

View File

@ -800,7 +800,7 @@ class BasicNewsRecipe(Recipe):
`weights`: A dictionary that maps weights to titles. If any titles
in index are not in weights, they are assumed to have a weight of 0.
'''
weights = defaultdict(lambda: 0, weights)
weights = defaultdict(int, weights)
index.sort(key=lambda x: weights[x])
return index

View File

@ -96,7 +96,7 @@ def serialize_collection(mapping_of_recipe_classes):
return f'''<?xml version='1.0' encoding='utf-8'?>
<recipe_collection xmlns="http://calibre-ebook.com/recipe_collection" count="{len(collection)}">
{items}
</recipe_collection>'''.encode('utf-8')
</recipe_collection>'''.encode()
def serialize_builtin_recipes():

View File

@ -1,5 +1,4 @@
#!/usr/bin/env python
# vim:fileencoding=utf-8
# License: GPL v3 Copyright: 2022, Kovid Goyal <kovid at kovidgoyal.net>
import json
@ -45,8 +44,8 @@ def process_paragraph(lines, block, content_key='content'):
else:
tag = 'p'
ta = block.get('textAlign') or 'LEFT'
style = 'text-align: {}'.format(ta.lower())
lines.append('<{} style="{}">'.format(tag, style))
style = f'text-align: {ta.lower()}'
lines.append(f'<{tag} style="{style}">')
for item in block[content_key]:
tn = item['__typename']
if tn in ('TextInline', 'Byline'):
@ -93,7 +92,7 @@ def process_image_block(lines, block):
if 'web.archive.org' in img:
img = img.partition('/')[-1]
img = img[img.find('https://'):]
lines.append('<div style="text-align: center"><div style="text-align: center"><img src={}/></div><div style="font-size: smaller">'.format(quoteattr(img)))
lines.append(f'<div style="text-align: center"><div style="text-align: center"><img src={quoteattr(img)}/></div><div style="font-size: smaller">')
lines.extend(caption_lines)
lines.append('</div></div>')
@ -181,7 +180,7 @@ def live_json_to_html(data):
def extract_html(soup):
script = soup.findAll('script', text=lambda x: x and 'window.__preloadedData' in x)[0]
script = type(u'')(script)
script = str(script)
raw = script[script.find('{'):script.rfind(';')].strip().rstrip(';')
return json_to_html(raw)