1
0
mirror of https://github.com/mikf/gallery-dl.git synced 2024-11-21 18:22:30 +01:00

consistent 'with open(…) as fp:' syntax

This commit is contained in:
Mike Fährmann 2024-06-14 01:22:00 +02:00
parent 3fc2e61818
commit 9c65db2a92
No known key found for this signature in database
GPG Key ID: 5680CA389D365A88
15 changed files with 46 additions and 46 deletions

View File

@ -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)

View File

@ -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:

View File

@ -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

View File

@ -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):

View File

@ -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),

View File

@ -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)})

View File

@ -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)})

View File

@ -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__":

View File

@ -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

View File

@ -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,
))

View File

@ -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))

View File

@ -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):

View File

@ -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")

View File

@ -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
""")

View File

@ -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