From 85a0056e6dc18d8119120a29299ab3ba9f465655 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 23 Nov 2023 09:05:12 +0530 Subject: [PATCH] automated fixes by pyupgrade to py 3.8 --- setup/vcvars.py | 12 ++++++------ src/calibre/customize/ui.py | 2 +- src/calibre/db/cli/cmd_backup_metadata.py | 2 +- src/calibre/db/fts/connect.py | 1 - src/calibre/db/fts/pool.py | 1 - src/calibre/db/fts/schema_upgrade.py | 1 - src/calibre/db/fts/text.py | 1 - src/calibre/db/tests/fts_api.py | 2 +- .../devices/kindle/apnx_page_generator/page_group.py | 2 +- src/calibre/devices/kobo/driver.py | 2 +- src/calibre/ebooks/conversion/cli.py | 2 +- src/calibre/ebooks/conversion/plugins/html_input.py | 2 +- src/calibre/ebooks/metadata/book/render.py | 2 +- src/calibre/ebooks/metadata/meta.py | 2 +- src/calibre/ebooks/mobi/debug/mobi8.py | 2 +- src/calibre/gui2/fts/dialog.py | 1 - src/calibre/gui2/fts/scan.py | 1 - src/calibre/gui2/fts/search.py | 1 - src/calibre/gui2/fts/utils.py | 1 - src/calibre/gui2/geometry.py | 1 - src/calibre/gui2/pyqt6_compat.py | 1 - src/calibre/gui2/store/amazon_base.py | 4 +--- src/calibre/gui2/store/amazon_live.py | 5 ++--- src/calibre/scraper/simple.py | 1 - src/calibre/scraper/simple_backend.py | 1 - src/calibre/srv/fts.py | 1 - src/calibre/srv/last_read.py | 1 - src/calibre/utils/formatter.py | 2 +- src/calibre/utils/rcc/__init__.py | 1 - src/calibre/utils/run_tests.py | 2 +- src/calibre/utils/shm.py | 1 - src/calibre/utils/webengine.py | 1 - src/calibre/utils/windows/winspeech.py | 2 +- src/calibre/web/feeds/news.py | 2 +- src/calibre/web/feeds/recipes/collection.py | 2 +- src/calibre/web/site_parsers/nytimes.py | 9 ++++----- 36 files changed, 28 insertions(+), 49 deletions(-) diff --git a/setup/vcvars.py b/setup/vcvars.py index 90e9de8482..2461e4fc9c 100644 --- a/setup/vcvars.py +++ b/setup/vcvars.py @@ -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 = {} diff --git a/src/calibre/customize/ui.py b/src/calibre/customize/ui.py index 57d064f8f4..84a6169065 100644 --- a/src/calibre/customize/ui.py +++ b/src/calibre/customize/ui.py @@ -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), diff --git a/src/calibre/db/cli/cmd_backup_metadata.py b/src/calibre/db/cli/cmd_backup_metadata.py index c784951bb7..334939d76c 100644 --- a/src/calibre/db/cli/cmd_backup_metadata.py +++ b/src/calibre/db/cli/cmd_backup_metadata.py @@ -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): diff --git a/src/calibre/db/fts/connect.py b/src/calibre/db/fts/connect.py index 39ca819a31..e47cbc5f42 100644 --- a/src/calibre/db/fts/connect.py +++ b/src/calibre/db/fts/connect.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# vim:fileencoding=utf-8 # License: GPL v3 Copyright: 2022, Kovid Goyal diff --git a/src/calibre/db/fts/pool.py b/src/calibre/db/fts/pool.py index 34c3477c19..c5ef3c30f1 100644 --- a/src/calibre/db/fts/pool.py +++ b/src/calibre/db/fts/pool.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# vim:fileencoding=utf-8 # License: GPL v3 Copyright: 2022, Kovid Goyal diff --git a/src/calibre/db/fts/schema_upgrade.py b/src/calibre/db/fts/schema_upgrade.py index 5a31156cc2..34c79d5d21 100644 --- a/src/calibre/db/fts/schema_upgrade.py +++ b/src/calibre/db/fts/schema_upgrade.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# vim:fileencoding=utf-8 # License: GPL v3 Copyright: 2022, Kovid Goyal from calibre.utils.resources import get_path as P diff --git a/src/calibre/db/fts/text.py b/src/calibre/db/fts/text.py index 93664e79ee..0beb0a405a 100644 --- a/src/calibre/db/fts/text.py +++ b/src/calibre/db/fts/text.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# vim:fileencoding=utf-8 # License: GPL v3 Copyright: 2022, Kovid Goyal diff --git a/src/calibre/db/tests/fts_api.py b/src/calibre/db/tests/fts_api.py index 38ac1027cf..e482c3ade9 100644 --- a/src/calibre/db/tests/fts_api.py +++ b/src/calibre/db/tests/fts_api.py @@ -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') diff --git a/src/calibre/devices/kindle/apnx_page_generator/page_group.py b/src/calibre/devices/kindle/apnx_page_generator/page_group.py index 5c3b6bbb95..edcf384d3f 100644 --- a/src/calibre/devices/kindle/apnx_page_generator/page_group.py +++ b/src/calibre/devices/kindle/apnx_page_generator/page_group.py @@ -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) diff --git a/src/calibre/devices/kobo/driver.py b/src/calibre/devices/kobo/driver.py index a58664756d..0dff8de229 100644 --- a/src/calibre/devices/kobo/driver.py +++ b/src/calibre/devices/kobo/driver.py @@ -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) diff --git a/src/calibre/ebooks/conversion/cli.py b/src/calibre/ebooks/conversion/cli.py index 6929811b10..b7c5cdfd7b 100644 --- a/src/calibre/ebooks/conversion/cli.py +++ b/src/calibre/ebooks/conversion/cli.py @@ -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: diff --git a/src/calibre/ebooks/conversion/plugins/html_input.py b/src/calibre/ebooks/conversion/plugins/html_input.py index c3fa670ebd..56bb02c3e0 100644 --- a/src/calibre/ebooks/conversion/plugins/html_input.py +++ b/src/calibre/ebooks/conversion/plugins/html_input.py @@ -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 diff --git a/src/calibre/ebooks/metadata/book/render.py b/src/calibre/ebooks/metadata/book/render.py index 8d1bd7d426..14608452a1 100644 --- a/src/calibre/ebooks/metadata/book/render.py +++ b/src/calibre/ebooks/metadata/book/render.py @@ -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 = ' {2}'.format( + note = ' {}'.format( _('Show notes for: {}').format(field_value), notes_action(field=field, value=field_value, item_id=item_id), note_markup) return link + note return '' diff --git a/src/calibre/ebooks/metadata/meta.py b/src/calibre/ebooks/metadata/meta.py index e022d56b9a..cfebcd5910 100644 --- a/src/calibre/ebooks/metadata/meta.py +++ b/src/calibre/ebooks/metadata/meta.py @@ -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', diff --git a/src/calibre/ebooks/mobi/debug/mobi8.py b/src/calibre/ebooks/mobi/debug/mobi8.py index 928cae3589..a9aa0f5fc8 100644 --- a/src/calibre/ebooks/mobi/debug/mobi8.py +++ b/src/calibre/ebooks/mobi/debug/mobi8.py @@ -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)) diff --git a/src/calibre/gui2/fts/dialog.py b/src/calibre/gui2/fts/dialog.py index 8d26ba51b1..afc6d2865e 100644 --- a/src/calibre/gui2/fts/dialog.py +++ b/src/calibre/gui2/fts/dialog.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# vim:fileencoding=utf-8 # License: GPL v3 Copyright: 2022, Kovid Goyal diff --git a/src/calibre/gui2/fts/scan.py b/src/calibre/gui2/fts/scan.py index b179694919..dea28d6f74 100644 --- a/src/calibre/gui2/fts/scan.py +++ b/src/calibre/gui2/fts/scan.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# vim:fileencoding=utf-8 # License: GPL v3 Copyright: 2022, Kovid Goyal import os diff --git a/src/calibre/gui2/fts/search.py b/src/calibre/gui2/fts/search.py index 88cc891a50..d21be4bac1 100644 --- a/src/calibre/gui2/fts/search.py +++ b/src/calibre/gui2/fts/search.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# vim:fileencoding=utf-8 # License: GPL v3 Copyright: 2022, Kovid Goyal diff --git a/src/calibre/gui2/fts/utils.py b/src/calibre/gui2/fts/utils.py index 39ed884a39..8d3f182af0 100644 --- a/src/calibre/gui2/fts/utils.py +++ b/src/calibre/gui2/fts/utils.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# vim:fileencoding=utf-8 # License: GPL v3 Copyright: 2022, Kovid Goyal diff --git a/src/calibre/gui2/geometry.py b/src/calibre/gui2/geometry.py index dc39430083..91a3e5ab6d 100644 --- a/src/calibre/gui2/geometry.py +++ b/src/calibre/gui2/geometry.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# vim:fileencoding=utf-8 # License: GPL v3 Copyright: 2022, Kovid Goyal diff --git a/src/calibre/gui2/pyqt6_compat.py b/src/calibre/gui2/pyqt6_compat.py index 7a7e17e3c2..c90507efcc 100644 --- a/src/calibre/gui2/pyqt6_compat.py +++ b/src/calibre/gui2/pyqt6_compat.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# vim:fileencoding=utf-8 # License: GPL v3 Copyright: 2021, Kovid Goyal # Workaround a bunch of brain dead changes in PyQt6 that break backwards compat diff --git a/src/calibre/gui2/store/amazon_base.py b/src/calibre/gui2/store/amazon_base.py index 281815efb2..825b0314c1 100644 --- a/src/calibre/gui2/store/amazon_base.py +++ b/src/calibre/gui2/store/amazon_base.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# vim:fileencoding=utf-8 # License: GPL v3 Copyright: 2022, Kovid Goyal 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) diff --git a/src/calibre/gui2/store/amazon_live.py b/src/calibre/gui2/store/amazon_live.py index 848e21eae9..6a778c2bee 100644 --- a/src/calibre/gui2/store/amazon_live.py +++ b/src/calibre/gui2/store/amazon_live.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# vim:fileencoding=utf-8 # License: GPL v3 Copyright: 2022, Kovid Goyal @@ -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 diff --git a/src/calibre/scraper/simple.py b/src/calibre/scraper/simple.py index fdbf8a5af1..bef2a36d36 100644 --- a/src/calibre/scraper/simple.py +++ b/src/calibre/scraper/simple.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# vim:fileencoding=utf-8 # License: GPL v3 Copyright: 2022, Kovid Goyal diff --git a/src/calibre/scraper/simple_backend.py b/src/calibre/scraper/simple_backend.py index 5e1da83d10..1338f922bb 100644 --- a/src/calibre/scraper/simple_backend.py +++ b/src/calibre/scraper/simple_backend.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# vim:fileencoding=utf-8 # License: GPL v3 Copyright: 2022, Kovid Goyal import json diff --git a/src/calibre/srv/fts.py b/src/calibre/srv/fts.py index ef572e2dbb..1c14dd445c 100644 --- a/src/calibre/srv/fts.py +++ b/src/calibre/srv/fts.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# vim:fileencoding=utf-8 # License: GPL v3 Copyright: 2022, Kovid Goyal import re diff --git a/src/calibre/srv/last_read.py b/src/calibre/srv/last_read.py index 6d7597deb1..7215c39fee 100644 --- a/src/calibre/srv/last_read.py +++ b/src/calibre/srv/last_read.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# vim:fileencoding=utf-8 # License: GPL v3 Copyright: 2022, Kovid Goyal import apsw diff --git a/src/calibre/utils/formatter.py b/src/calibre/utils/formatter.py index 7d8d3e6bb0..ab7934cc6a 100644 --- a/src/calibre/utils/formatter.py +++ b/src/calibre/utils/formatter.py @@ -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. diff --git a/src/calibre/utils/rcc/__init__.py b/src/calibre/utils/rcc/__init__.py index e3253eac25..845ba9349e 100644 --- a/src/calibre/utils/rcc/__init__.py +++ b/src/calibre/utils/rcc/__init__.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# vim:fileencoding=utf-8 # License: GPL v3 Copyright: 2022, Kovid Goyal import os, io diff --git a/src/calibre/utils/run_tests.py b/src/calibre/utils/run_tests.py index 7a6669ae27..e84252334a 100644 --- a/src/calibre/utils/run_tests.py +++ b/src/calibre/utils/run_tests.py @@ -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) diff --git a/src/calibre/utils/shm.py b/src/calibre/utils/shm.py index 6857b18a44..0f1635af44 100644 --- a/src/calibre/utils/shm.py +++ b/src/calibre/utils/shm.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# vim:fileencoding=utf-8 # License: GPL v3 Copyright: 2022, Kovid Goyal diff --git a/src/calibre/utils/webengine.py b/src/calibre/utils/webengine.py index bf2a4c54c1..3234105ec5 100644 --- a/src/calibre/utils/webengine.py +++ b/src/calibre/utils/webengine.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# vim:fileencoding=utf-8 # License: GPL v3 Copyright: 2021, Kovid Goyal diff --git a/src/calibre/utils/windows/winspeech.py b/src/calibre/utils/windows/winspeech.py index 7f465b4e06..5e7cc9acc5 100644 --- a/src/calibre/utils/windows/winspeech.py +++ b/src/calibre/utils/windows/winspeech.py @@ -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 diff --git a/src/calibre/web/feeds/news.py b/src/calibre/web/feeds/news.py index 82f4733028..95ae792063 100644 --- a/src/calibre/web/feeds/news.py +++ b/src/calibre/web/feeds/news.py @@ -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 diff --git a/src/calibre/web/feeds/recipes/collection.py b/src/calibre/web/feeds/recipes/collection.py index 64c6c81cdf..2ad02dca40 100644 --- a/src/calibre/web/feeds/recipes/collection.py +++ b/src/calibre/web/feeds/recipes/collection.py @@ -96,7 +96,7 @@ def serialize_collection(mapping_of_recipe_classes): return f''' {items} -'''.encode('utf-8') +'''.encode() def serialize_builtin_recipes(): diff --git a/src/calibre/web/site_parsers/nytimes.py b/src/calibre/web/site_parsers/nytimes.py index 7b94f97ae9..c78e3edc08 100644 --- a/src/calibre/web/site_parsers/nytimes.py +++ b/src/calibre/web/site_parsers/nytimes.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# vim:fileencoding=utf-8 # License: GPL v3 Copyright: 2022, Kovid Goyal 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('
'.format(quoteattr(img))) + lines.append(f'
') lines.extend(caption_lines) lines.append('
') @@ -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)