From 176b7253a131c61e30fa12d6811a99bd9fe3f92b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20F=C3=A4hrmann?= Date: Wed, 27 Feb 2019 16:52:15 +0100 Subject: [PATCH] update function signature for config.load() --- gallery_dl/__init__.py | 4 ++-- gallery_dl/config.py | 31 +++++++++++++++---------------- test/test_config.py | 2 +- 3 files changed, 18 insertions(+), 19 deletions(-) diff --git a/gallery_dl/__init__.py b/gallery_dl/__init__.py index bcdd660f..48d847d3 100644 --- a/gallery_dl/__init__.py +++ b/gallery_dl/__init__.py @@ -118,9 +118,9 @@ def main(): if args.load_config: config.load() if args.cfgfiles: - config.load(*args.cfgfiles, strict=True) + config.load(args.cfgfiles, strict=True) if args.yamlfiles: - config.load(*args.yamlfiles, format="yaml", strict=True) + config.load(args.yamlfiles, strict=True, fmt="yaml") for key, value in args.options: config.set(key, value) diff --git a/gallery_dl/config.py b/gallery_dl/config.py index 17e8e41c..58a1c83f 100644 --- a/gallery_dl/config.py +++ b/gallery_dl/config.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- -# Copyright 2015-2018 Mike Fährmann +# Copyright 2015-2019 Mike Fährmann # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License version 2 as @@ -39,11 +39,9 @@ else: # -------------------------------------------------------------------- # public interface -def load(*files, format="json", strict=False): +def load(files=None, strict=False, fmt="json"): """Load JSON configuration files""" - configfiles = files or _default_configs - - if format == "yaml": + if fmt == "yaml": try: import yaml parsefunc = yaml.safe_load @@ -53,23 +51,24 @@ def load(*files, format="json", strict=False): else: parsefunc = json.load - for conf in configfiles: + for path in files or _default_configs: + path = util.expand_path(path) try: - path = util.expand_path(conf) with open(path, encoding="utf-8") as file: confdict = parsefunc(file) + except OSError as exc: + if strict: + log.error("%s", exc) + sys.exit(1) + except Exception as exc: + log.warning("Could not parse '%s': %s", path, exc) + if strict: + sys.exit(2) + else: if not _config: _config.update(confdict) else: util.combine_dict(_config, confdict) - except FileNotFoundError: - if strict: - log.error("Configuration file '%s' not found", path) - sys.exit(1) - except Exception as exc: - log.warning("Could not parse '%s': %s", path, exc) - if strict: - sys.exit(2) def clear(): @@ -137,7 +136,7 @@ def unset(keys, conf=_config): class apply(): - """Context Manager to temporarily apply a collection of key-value pairs""" + """Context Manager: apply a collection of key-value pairs""" _sentinel = object() def __init__(self, kvlist): diff --git a/test/test_config.py b/test/test_config.py index 442e8794..8cdb3da9 100644 --- a/test/test_config.py +++ b/test/test_config.py @@ -19,7 +19,7 @@ class TestConfig(unittest.TestCase): fd, self._configfile = tempfile.mkstemp() with os.fdopen(fd, "w") as file: file.write('{"a": "1", "b": {"a": 2, "c": "text"}}') - config.load(self._configfile) + config.load((self._configfile,)) def tearDown(self): config.clear()