From 9c65db2a920d86300728842c2147e9d876b1e975 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20F=C3=A4hrmann?= Date: Fri, 14 Jun 2024 01:22:00 +0200 Subject: [PATCH] =?UTF-8?q?consistent=20'with=20open(=E2=80=A6)=20as=20fp:?= =?UTF-8?q?'=20syntax?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gallery_dl/config.py | 4 ++-- gallery_dl/cookies.py | 8 ++++---- gallery_dl/downloader/text.py | 4 ++-- gallery_dl/postprocessor/ugoira.py | 8 ++++---- scripts/completion_bash.py | 4 ++-- scripts/completion_fish.py | 4 ++-- scripts/completion_zsh.py | 4 ++-- scripts/export_tests.py | 4 ++-- scripts/man.py | 12 ++++++------ scripts/options.py | 4 ++-- scripts/supportedsites.py | 4 ++-- setup.py | 4 ++-- test/test_config.py | 12 ++++++------ test/test_cookies.py | 8 ++++---- test/test_downloader.py | 8 ++++---- 15 files changed, 46 insertions(+), 46 deletions(-) diff --git a/gallery_dl/config.py b/gallery_dl/config.py index b9a55255..0a187c14 100644 --- a/gallery_dl/config.py +++ b/gallery_dl/config.py @@ -167,8 +167,8 @@ def load(files=None, strict=False, loads=util.json_loads): for pathfmt in files or _default_configs: path = util.expand_path(pathfmt) try: - with open(path, encoding="utf-8") as file: - conf = loads(file.read()) + with open(path, encoding="utf-8") as fp: + conf = loads(fp.read()) except OSError as exc: if strict: log.error(exc) diff --git a/gallery_dl/cookies.py b/gallery_dl/cookies.py index b4986c1e..47f78a75 100644 --- a/gallery_dl/cookies.py +++ b/gallery_dl/cookies.py @@ -188,8 +188,8 @@ def _firefox_cookies_database(profile=None, container=None): os.path.dirname(path), "containers.json") try: - with open(containers_path) as file: - identities = util.json_loads(file.read())["identities"] + with open(containers_path) as fp: + identities = util.json_loads(fp.read())["identities"] except OSError: _log_error("Unable to read Firefox container database at '%s'", containers_path) @@ -745,8 +745,8 @@ def _get_windows_v10_key(browser_root): _log_error("Unable to find Local State file") return None _log_debug("Found Local State file at '%s'", path) - with open(path, encoding="utf-8") as file: - data = util.json_loads(file.read()) + with open(path, encoding="utf-8") as fp: + data = util.json_loads(fp.read()) try: base64_key = data["os_crypt"]["encrypted_key"] except KeyError: diff --git a/gallery_dl/downloader/text.py b/gallery_dl/downloader/text.py index c57fbd0c..a668d62a 100644 --- a/gallery_dl/downloader/text.py +++ b/gallery_dl/downloader/text.py @@ -18,8 +18,8 @@ class TextDownloader(DownloaderBase): if self.part: pathfmt.part_enable(self.partdir) self.out.start(pathfmt.path) - with pathfmt.open("wb") as file: - file.write(url.encode()[5:]) + with pathfmt.open("wb") as fp: + fp.write(url.encode()[5:]) return True diff --git a/gallery_dl/postprocessor/ugoira.py b/gallery_dl/postprocessor/ugoira.py index c63a3d94..4455f75c 100644 --- a/gallery_dl/postprocessor/ugoira.py +++ b/gallery_dl/postprocessor/ugoira.py @@ -266,8 +266,8 @@ class UgoiraPP(PostProcessor): append("") ffconcat = tempdir + "/ffconcat.txt" - with open(ffconcat, "w") as file: - file.write("\n".join(content)) + with open(ffconcat, "w") as fp: + fp.write("\n".join(content)) return ffconcat def _write_mkvmerge_timecodes(self, tempdir): @@ -282,8 +282,8 @@ class UgoiraPP(PostProcessor): append("") timecodes = tempdir + "/timecodes.tc" - with open(timecodes, "w") as file: - file.write("\n".join(content)) + with open(timecodes, "w") as fp: + fp.write("\n".join(content)) return timecodes def calculate_framerate(self, frames): diff --git a/scripts/completion_bash.py b/scripts/completion_bash.py index d8ceef82..b9921dc0 100755 --- a/scripts/completion_bash.py +++ b/scripts/completion_bash.py @@ -48,8 +48,8 @@ for action in option.build_parser()._actions: opts.append(opt) PATH = util.path("data/completion/gallery-dl") -with util.lazy(PATH) as file: - file.write(TEMPLATE % { +with util.lazy(PATH) as fp: + fp.write(TEMPLATE % { "opts" : " ".join(opts), "diropts" : "|".join(diropts), "fileopts": "|".join(fileopts), diff --git a/scripts/completion_fish.py b/scripts/completion_fish.py index 7ed4cb09..a5dd47b9 100755 --- a/scripts/completion_fish.py +++ b/scripts/completion_fish.py @@ -41,5 +41,5 @@ for action in option.build_parser()._actions: opts.append(opt) PATH = util.path("data/completion/gallery-dl.fish") -with util.lazy(PATH) as file: - file.write(TEMPLATE % {"opts": "\n".join(opts)}) +with util.lazy(PATH) as fp: + fp.write(TEMPLATE % {"opts": "\n".join(opts)}) diff --git a/scripts/completion_zsh.py b/scripts/completion_zsh.py index 78f8dfd0..d96ed733 100755 --- a/scripts/completion_zsh.py +++ b/scripts/completion_zsh.py @@ -53,5 +53,5 @@ for action in option.build_parser()._actions: PATH = util.path("data/completion/_gallery-dl") -with util.lazy(PATH) as file: - file.write(TEMPLATE % {"opts": " \\\n".join(opts)}) +with util.lazy(PATH) as fp: + fp.write(TEMPLATE % {"opts": " \\\n".join(opts)}) diff --git a/scripts/export_tests.py b/scripts/export_tests.py index 285f2206..7ea62852 100755 --- a/scripts/export_tests.py +++ b/scripts/export_tests.py @@ -239,8 +239,8 @@ def main(): os.makedirs(args.target, exist_ok=True) for name, tests in collect_tests(args.category).items(): name = name.replace(".", "") - with util.lazy(f"{args.target}/{name}.py") as file: - file.write(export_tests(tests)) + with util.lazy(f"{args.target}/{name}.py") as fp: + fp.write(export_tests(tests)) if __name__ == "__main__": diff --git a/scripts/man.py b/scripts/man.py index 47968f15..2aff221d 100755 --- a/scripts/man.py +++ b/scripts/man.py @@ -100,8 +100,8 @@ and https://github.com/mikf/gallery-dl/graphs/contributors if not path: path = util.path("data/man/gallery-dl.1") - with util.lazy(path) as file: - file.write(TEMPLATE.lstrip() % { + with util.lazy(path) as fp: + fp.write(TEMPLATE.lstrip() % { "options": "\n".join(options), "version": gallery_dl.version.__version__, "date" : datetime.datetime.now().strftime("%Y-%m-%d"), @@ -218,8 +218,8 @@ and https://github.com/mikf/gallery-dl/graphs/contributors if not path: path = util.path("data/man/gallery-dl.conf.5") - with util.lazy(path) as file: - file.write(TEMPLATE.lstrip() % { + with util.lazy(path) as fp: + fp.write(TEMPLATE.lstrip() % { "options": "\n".join(content), "version": gallery_dl.version.__version__, "date" : datetime.datetime.now().strftime("%Y-%m-%d"), @@ -229,8 +229,8 @@ and https://github.com/mikf/gallery-dl/graphs/contributors def parse_docs_configuration(): doc_path = util.path("docs", "configuration.rst") - with open(doc_path, encoding="utf-8") as file: - doc_lines = file.readlines() + with open(doc_path, encoding="utf-8") as fp: + doc_lines = fp.readlines() sections = {} sec_name = None diff --git a/scripts/options.py b/scripts/options.py index 65d22a02..f9f0ace1 100755 --- a/scripts/options.py +++ b/scripts/options.py @@ -38,8 +38,8 @@ opts = opts.replace("\n ", "\n ") # indent by 4 PATH = (sys.argv[1] if len(sys.argv) > 1 else util.path("docs", "options.md")) -with util.lazy(PATH) as file: - file.write(TEMPLATE.format( +with util.lazy(PATH) as fp: + fp.write(TEMPLATE.format( "/".join(os.path.normpath(__file__).split(os.sep)[-2:]), opts, )) diff --git a/scripts/supportedsites.py b/scripts/supportedsites.py index 3e5f21ce..6073cd92 100755 --- a/scripts/supportedsites.py +++ b/scripts/supportedsites.py @@ -593,5 +593,5 @@ Consider all listed sites to potentially be NSFW. categories, domains = build_extractor_list() PATH = (sys.argv[1] if len(sys.argv) > 1 else util.path("docs", "supportedsites.md")) -with util.lazy(PATH) as file: - file.write(generate_output(COLUMNS, categories, domains)) +with util.lazy(PATH) as fp: + fp.write(generate_output(COLUMNS, categories, domains)) diff --git a/setup.py b/setup.py index ee66f5f5..5f9dbc87 100644 --- a/setup.py +++ b/setup.py @@ -9,8 +9,8 @@ import warnings def read(fname): path = os.path.join(os.path.dirname(__file__), fname) - with open(path, encoding="utf-8") as file: - return file.read() + with open(path, encoding="utf-8") as fp: + return fp.read() def check_file(fname): diff --git a/test/test_config.py b/test/test_config.py index 06780bea..bbe288ff 100644 --- a/test/test_config.py +++ b/test/test_config.py @@ -165,12 +165,12 @@ class TestConfig(unittest.TestCase): def test_load(self): with tempfile.TemporaryDirectory() as base: path1 = os.path.join(base, "cfg1") - with open(path1, "w") as file: - file.write('{"a": 1, "b": {"a": 2, "c": "text"}}') + with open(path1, "w") as fp: + fp.write('{"a": 1, "b": {"a": 2, "c": "text"}}') path2 = os.path.join(base, "cfg2") - with open(path2, "w") as file: - file.write('{"a": 7, "b": {"a": 8, "e": "foo"}}') + with open(path2, "w") as fp: + fp.write('{"a": 7, "b": {"a": 8, "e": "foo"}}') config.clear() config.load((path1,)) @@ -208,8 +208,8 @@ class TestConfigFiles(unittest.TestCase): def _load(name): path = os.path.join(ROOTDIR, "docs", name) try: - with open(path) as file: - return util.json_loads(file.read()) + with open(path) as fp: + return util.json_loads(fp.read()) except FileNotFoundError: raise unittest.SkipTest(path + " not available") diff --git a/test/test_cookies.py b/test/test_cookies.py index 208645d1..60c83ffb 100644 --- a/test/test_cookies.py +++ b/test/test_cookies.py @@ -28,14 +28,14 @@ class TestCookiejar(unittest.TestCase): cls.path = tempfile.TemporaryDirectory() cls.cookiefile = join(cls.path.name, "cookies.txt") - with open(cls.cookiefile, "w") as file: - file.write("""# HTTP Cookie File + with open(cls.cookiefile, "w") as fp: + fp.write("""# HTTP Cookie File .example.org\tTRUE\t/\tFALSE\t253402210800\tNAME\tVALUE """) cls.invalid_cookiefile = join(cls.path.name, "invalid.txt") - with open(cls.invalid_cookiefile, "w") as file: - file.write("""# asd + with open(cls.invalid_cookiefile, "w") as fp: + fp.write("""# asd .example.org\tTRUE/FALSE\t253402210800\tNAME\tVALUE """) diff --git a/test/test_downloader.py b/test/test_downloader.py index 9f9fb3b8..f88b2c0d 100644 --- a/test/test_downloader.py +++ b/test/test_downloader.py @@ -136,8 +136,8 @@ class TestDownloaderBase(unittest.TestCase): if content: mode = "w" + ("b" if isinstance(content, bytes) else "") - with pathfmt.open(mode) as file: - file.write(content) + with pathfmt.open(mode) as fp: + fp.write(content) return pathfmt @@ -151,8 +151,8 @@ class TestDownloaderBase(unittest.TestCase): # test content mode = "r" + ("b" if isinstance(output, bytes) else "") - with pathfmt.open(mode) as file: - content = file.read() + with pathfmt.open(mode) as fp: + content = fp.read() self.assertEqual(content, output) # test filename extension