mirror of
https://github.com/mikf/gallery-dl.git
synced 2024-11-22 02:32:33 +01:00
consistent 'with open(…) as fp:' syntax
This commit is contained in:
parent
3fc2e61818
commit
9c65db2a92
@ -167,8 +167,8 @@ def load(files=None, strict=False, loads=util.json_loads):
|
|||||||
for pathfmt in files or _default_configs:
|
for pathfmt in files or _default_configs:
|
||||||
path = util.expand_path(pathfmt)
|
path = util.expand_path(pathfmt)
|
||||||
try:
|
try:
|
||||||
with open(path, encoding="utf-8") as file:
|
with open(path, encoding="utf-8") as fp:
|
||||||
conf = loads(file.read())
|
conf = loads(fp.read())
|
||||||
except OSError as exc:
|
except OSError as exc:
|
||||||
if strict:
|
if strict:
|
||||||
log.error(exc)
|
log.error(exc)
|
||||||
|
@ -188,8 +188,8 @@ def _firefox_cookies_database(profile=None, container=None):
|
|||||||
os.path.dirname(path), "containers.json")
|
os.path.dirname(path), "containers.json")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with open(containers_path) as file:
|
with open(containers_path) as fp:
|
||||||
identities = util.json_loads(file.read())["identities"]
|
identities = util.json_loads(fp.read())["identities"]
|
||||||
except OSError:
|
except OSError:
|
||||||
_log_error("Unable to read Firefox container database at '%s'",
|
_log_error("Unable to read Firefox container database at '%s'",
|
||||||
containers_path)
|
containers_path)
|
||||||
@ -745,8 +745,8 @@ def _get_windows_v10_key(browser_root):
|
|||||||
_log_error("Unable to find Local State file")
|
_log_error("Unable to find Local State file")
|
||||||
return None
|
return None
|
||||||
_log_debug("Found Local State file at '%s'", path)
|
_log_debug("Found Local State file at '%s'", path)
|
||||||
with open(path, encoding="utf-8") as file:
|
with open(path, encoding="utf-8") as fp:
|
||||||
data = util.json_loads(file.read())
|
data = util.json_loads(fp.read())
|
||||||
try:
|
try:
|
||||||
base64_key = data["os_crypt"]["encrypted_key"]
|
base64_key = data["os_crypt"]["encrypted_key"]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
|
@ -18,8 +18,8 @@ class TextDownloader(DownloaderBase):
|
|||||||
if self.part:
|
if self.part:
|
||||||
pathfmt.part_enable(self.partdir)
|
pathfmt.part_enable(self.partdir)
|
||||||
self.out.start(pathfmt.path)
|
self.out.start(pathfmt.path)
|
||||||
with pathfmt.open("wb") as file:
|
with pathfmt.open("wb") as fp:
|
||||||
file.write(url.encode()[5:])
|
fp.write(url.encode()[5:])
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
@ -266,8 +266,8 @@ class UgoiraPP(PostProcessor):
|
|||||||
append("")
|
append("")
|
||||||
|
|
||||||
ffconcat = tempdir + "/ffconcat.txt"
|
ffconcat = tempdir + "/ffconcat.txt"
|
||||||
with open(ffconcat, "w") as file:
|
with open(ffconcat, "w") as fp:
|
||||||
file.write("\n".join(content))
|
fp.write("\n".join(content))
|
||||||
return ffconcat
|
return ffconcat
|
||||||
|
|
||||||
def _write_mkvmerge_timecodes(self, tempdir):
|
def _write_mkvmerge_timecodes(self, tempdir):
|
||||||
@ -282,8 +282,8 @@ class UgoiraPP(PostProcessor):
|
|||||||
append("")
|
append("")
|
||||||
|
|
||||||
timecodes = tempdir + "/timecodes.tc"
|
timecodes = tempdir + "/timecodes.tc"
|
||||||
with open(timecodes, "w") as file:
|
with open(timecodes, "w") as fp:
|
||||||
file.write("\n".join(content))
|
fp.write("\n".join(content))
|
||||||
return timecodes
|
return timecodes
|
||||||
|
|
||||||
def calculate_framerate(self, frames):
|
def calculate_framerate(self, frames):
|
||||||
|
@ -48,8 +48,8 @@ for action in option.build_parser()._actions:
|
|||||||
opts.append(opt)
|
opts.append(opt)
|
||||||
|
|
||||||
PATH = util.path("data/completion/gallery-dl")
|
PATH = util.path("data/completion/gallery-dl")
|
||||||
with util.lazy(PATH) as file:
|
with util.lazy(PATH) as fp:
|
||||||
file.write(TEMPLATE % {
|
fp.write(TEMPLATE % {
|
||||||
"opts" : " ".join(opts),
|
"opts" : " ".join(opts),
|
||||||
"diropts" : "|".join(diropts),
|
"diropts" : "|".join(diropts),
|
||||||
"fileopts": "|".join(fileopts),
|
"fileopts": "|".join(fileopts),
|
||||||
|
@ -41,5 +41,5 @@ for action in option.build_parser()._actions:
|
|||||||
opts.append(opt)
|
opts.append(opt)
|
||||||
|
|
||||||
PATH = util.path("data/completion/gallery-dl.fish")
|
PATH = util.path("data/completion/gallery-dl.fish")
|
||||||
with util.lazy(PATH) as file:
|
with util.lazy(PATH) as fp:
|
||||||
file.write(TEMPLATE % {"opts": "\n".join(opts)})
|
fp.write(TEMPLATE % {"opts": "\n".join(opts)})
|
||||||
|
@ -53,5 +53,5 @@ for action in option.build_parser()._actions:
|
|||||||
|
|
||||||
|
|
||||||
PATH = util.path("data/completion/_gallery-dl")
|
PATH = util.path("data/completion/_gallery-dl")
|
||||||
with util.lazy(PATH) as file:
|
with util.lazy(PATH) as fp:
|
||||||
file.write(TEMPLATE % {"opts": " \\\n".join(opts)})
|
fp.write(TEMPLATE % {"opts": " \\\n".join(opts)})
|
||||||
|
@ -239,8 +239,8 @@ def main():
|
|||||||
os.makedirs(args.target, exist_ok=True)
|
os.makedirs(args.target, exist_ok=True)
|
||||||
for name, tests in collect_tests(args.category).items():
|
for name, tests in collect_tests(args.category).items():
|
||||||
name = name.replace(".", "")
|
name = name.replace(".", "")
|
||||||
with util.lazy(f"{args.target}/{name}.py") as file:
|
with util.lazy(f"{args.target}/{name}.py") as fp:
|
||||||
file.write(export_tests(tests))
|
fp.write(export_tests(tests))
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
@ -100,8 +100,8 @@ and https://github.com/mikf/gallery-dl/graphs/contributors
|
|||||||
|
|
||||||
if not path:
|
if not path:
|
||||||
path = util.path("data/man/gallery-dl.1")
|
path = util.path("data/man/gallery-dl.1")
|
||||||
with util.lazy(path) as file:
|
with util.lazy(path) as fp:
|
||||||
file.write(TEMPLATE.lstrip() % {
|
fp.write(TEMPLATE.lstrip() % {
|
||||||
"options": "\n".join(options),
|
"options": "\n".join(options),
|
||||||
"version": gallery_dl.version.__version__,
|
"version": gallery_dl.version.__version__,
|
||||||
"date" : datetime.datetime.now().strftime("%Y-%m-%d"),
|
"date" : datetime.datetime.now().strftime("%Y-%m-%d"),
|
||||||
@ -218,8 +218,8 @@ and https://github.com/mikf/gallery-dl/graphs/contributors
|
|||||||
|
|
||||||
if not path:
|
if not path:
|
||||||
path = util.path("data/man/gallery-dl.conf.5")
|
path = util.path("data/man/gallery-dl.conf.5")
|
||||||
with util.lazy(path) as file:
|
with util.lazy(path) as fp:
|
||||||
file.write(TEMPLATE.lstrip() % {
|
fp.write(TEMPLATE.lstrip() % {
|
||||||
"options": "\n".join(content),
|
"options": "\n".join(content),
|
||||||
"version": gallery_dl.version.__version__,
|
"version": gallery_dl.version.__version__,
|
||||||
"date" : datetime.datetime.now().strftime("%Y-%m-%d"),
|
"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():
|
def parse_docs_configuration():
|
||||||
|
|
||||||
doc_path = util.path("docs", "configuration.rst")
|
doc_path = util.path("docs", "configuration.rst")
|
||||||
with open(doc_path, encoding="utf-8") as file:
|
with open(doc_path, encoding="utf-8") as fp:
|
||||||
doc_lines = file.readlines()
|
doc_lines = fp.readlines()
|
||||||
|
|
||||||
sections = {}
|
sections = {}
|
||||||
sec_name = None
|
sec_name = None
|
||||||
|
@ -38,8 +38,8 @@ opts = opts.replace("\n ", "\n ") # indent by 4
|
|||||||
|
|
||||||
PATH = (sys.argv[1] if len(sys.argv) > 1 else
|
PATH = (sys.argv[1] if len(sys.argv) > 1 else
|
||||||
util.path("docs", "options.md"))
|
util.path("docs", "options.md"))
|
||||||
with util.lazy(PATH) as file:
|
with util.lazy(PATH) as fp:
|
||||||
file.write(TEMPLATE.format(
|
fp.write(TEMPLATE.format(
|
||||||
"/".join(os.path.normpath(__file__).split(os.sep)[-2:]),
|
"/".join(os.path.normpath(__file__).split(os.sep)[-2:]),
|
||||||
opts,
|
opts,
|
||||||
))
|
))
|
||||||
|
@ -593,5 +593,5 @@ Consider all listed sites to potentially be NSFW.
|
|||||||
categories, domains = build_extractor_list()
|
categories, domains = build_extractor_list()
|
||||||
PATH = (sys.argv[1] if len(sys.argv) > 1 else
|
PATH = (sys.argv[1] if len(sys.argv) > 1 else
|
||||||
util.path("docs", "supportedsites.md"))
|
util.path("docs", "supportedsites.md"))
|
||||||
with util.lazy(PATH) as file:
|
with util.lazy(PATH) as fp:
|
||||||
file.write(generate_output(COLUMNS, categories, domains))
|
fp.write(generate_output(COLUMNS, categories, domains))
|
||||||
|
4
setup.py
4
setup.py
@ -9,8 +9,8 @@ import warnings
|
|||||||
|
|
||||||
def read(fname):
|
def read(fname):
|
||||||
path = os.path.join(os.path.dirname(__file__), fname)
|
path = os.path.join(os.path.dirname(__file__), fname)
|
||||||
with open(path, encoding="utf-8") as file:
|
with open(path, encoding="utf-8") as fp:
|
||||||
return file.read()
|
return fp.read()
|
||||||
|
|
||||||
|
|
||||||
def check_file(fname):
|
def check_file(fname):
|
||||||
|
@ -165,12 +165,12 @@ class TestConfig(unittest.TestCase):
|
|||||||
def test_load(self):
|
def test_load(self):
|
||||||
with tempfile.TemporaryDirectory() as base:
|
with tempfile.TemporaryDirectory() as base:
|
||||||
path1 = os.path.join(base, "cfg1")
|
path1 = os.path.join(base, "cfg1")
|
||||||
with open(path1, "w") as file:
|
with open(path1, "w") as fp:
|
||||||
file.write('{"a": 1, "b": {"a": 2, "c": "text"}}')
|
fp.write('{"a": 1, "b": {"a": 2, "c": "text"}}')
|
||||||
|
|
||||||
path2 = os.path.join(base, "cfg2")
|
path2 = os.path.join(base, "cfg2")
|
||||||
with open(path2, "w") as file:
|
with open(path2, "w") as fp:
|
||||||
file.write('{"a": 7, "b": {"a": 8, "e": "foo"}}')
|
fp.write('{"a": 7, "b": {"a": 8, "e": "foo"}}')
|
||||||
|
|
||||||
config.clear()
|
config.clear()
|
||||||
config.load((path1,))
|
config.load((path1,))
|
||||||
@ -208,8 +208,8 @@ class TestConfigFiles(unittest.TestCase):
|
|||||||
def _load(name):
|
def _load(name):
|
||||||
path = os.path.join(ROOTDIR, "docs", name)
|
path = os.path.join(ROOTDIR, "docs", name)
|
||||||
try:
|
try:
|
||||||
with open(path) as file:
|
with open(path) as fp:
|
||||||
return util.json_loads(file.read())
|
return util.json_loads(fp.read())
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
raise unittest.SkipTest(path + " not available")
|
raise unittest.SkipTest(path + " not available")
|
||||||
|
|
||||||
|
@ -28,14 +28,14 @@ class TestCookiejar(unittest.TestCase):
|
|||||||
cls.path = tempfile.TemporaryDirectory()
|
cls.path = tempfile.TemporaryDirectory()
|
||||||
|
|
||||||
cls.cookiefile = join(cls.path.name, "cookies.txt")
|
cls.cookiefile = join(cls.path.name, "cookies.txt")
|
||||||
with open(cls.cookiefile, "w") as file:
|
with open(cls.cookiefile, "w") as fp:
|
||||||
file.write("""# HTTP Cookie File
|
fp.write("""# HTTP Cookie File
|
||||||
.example.org\tTRUE\t/\tFALSE\t253402210800\tNAME\tVALUE
|
.example.org\tTRUE\t/\tFALSE\t253402210800\tNAME\tVALUE
|
||||||
""")
|
""")
|
||||||
|
|
||||||
cls.invalid_cookiefile = join(cls.path.name, "invalid.txt")
|
cls.invalid_cookiefile = join(cls.path.name, "invalid.txt")
|
||||||
with open(cls.invalid_cookiefile, "w") as file:
|
with open(cls.invalid_cookiefile, "w") as fp:
|
||||||
file.write("""# asd
|
fp.write("""# asd
|
||||||
.example.org\tTRUE/FALSE\t253402210800\tNAME\tVALUE
|
.example.org\tTRUE/FALSE\t253402210800\tNAME\tVALUE
|
||||||
""")
|
""")
|
||||||
|
|
||||||
|
@ -136,8 +136,8 @@ class TestDownloaderBase(unittest.TestCase):
|
|||||||
|
|
||||||
if content:
|
if content:
|
||||||
mode = "w" + ("b" if isinstance(content, bytes) else "")
|
mode = "w" + ("b" if isinstance(content, bytes) else "")
|
||||||
with pathfmt.open(mode) as file:
|
with pathfmt.open(mode) as fp:
|
||||||
file.write(content)
|
fp.write(content)
|
||||||
|
|
||||||
return pathfmt
|
return pathfmt
|
||||||
|
|
||||||
@ -151,8 +151,8 @@ class TestDownloaderBase(unittest.TestCase):
|
|||||||
|
|
||||||
# test content
|
# test content
|
||||||
mode = "r" + ("b" if isinstance(output, bytes) else "")
|
mode = "r" + ("b" if isinstance(output, bytes) else "")
|
||||||
with pathfmt.open(mode) as file:
|
with pathfmt.open(mode) as fp:
|
||||||
content = file.read()
|
content = fp.read()
|
||||||
self.assertEqual(content, output)
|
self.assertEqual(content, output)
|
||||||
|
|
||||||
# test filename extension
|
# test filename extension
|
||||||
|
Loading…
Reference in New Issue
Block a user