diff options
Diffstat (limited to 'gallery_dl/config.py')
| -rw-r--r-- | gallery_dl/config.py | 60 |
1 files changed, 45 insertions, 15 deletions
diff --git a/gallery_dl/config.py b/gallery_dl/config.py index 0f2d1f1..d014293 100644 --- a/gallery_dl/config.py +++ b/gallery_dl/config.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- -# Copyright 2015-2021 Mike Fährmann +# Copyright 2015-2023 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 @@ -9,7 +9,6 @@ """Global configuration module""" import sys -import json import os.path import logging from . import util @@ -39,7 +38,7 @@ else: ] -if getattr(sys, "frozen", False): +if util.EXECUTABLE: # look for config file in PyInstaller executable directory (#682) _default_configs.append(os.path.join( os.path.dirname(sys.executable), @@ -50,23 +49,54 @@ if getattr(sys, "frozen", False): # -------------------------------------------------------------------- # public interface -def load(files=None, strict=False, fmt="json"): - """Load JSON configuration files""" - if fmt == "yaml": + +def initialize(): + paths = list(map(util.expand_path, _default_configs)) + + for path in paths: + if os.access(path, os.R_OK | os.W_OK): + log.error("There is already a configuration file at '%s'", path) + return 1 + + for path in paths: try: - import yaml - parsefunc = yaml.safe_load - except ImportError: - log.error("Could not import 'yaml' module") - return + os.makedirs(os.path.dirname(path), exist_ok=True) + with open(path, "x", encoding="utf-8") as fp: + fp.write("""\ +{ + "extractor": { + + }, + "downloader": { + + }, + "output": { + + }, + "postprocessor": { + + } +} +""") + break + except OSError as exc: + log.debug("%s: %s", exc.__class__.__name__, exc) else: - parsefunc = json.load + log.error("Unable to create a new configuration file " + "at any of the default paths") + return 1 + log.info("Created a basic configuration file at '%s'", path) + return 0 + + +def load(files=None, strict=False, load=util.json_loads): + """Load JSON configuration files""" for pathfmt in files or _default_configs: path = util.expand_path(pathfmt) try: with open(path, encoding="utf-8") as file: - confdict = parsefunc(file) + conf = load(file.read()) except OSError as exc: if strict: log.error(exc) @@ -77,9 +107,9 @@ def load(files=None, strict=False, fmt="json"): sys.exit(2) else: if not _config: - _config.update(confdict) + _config.update(conf) else: - util.combine_dict(_config, confdict) + util.combine_dict(_config, conf) _files.append(pathfmt) |
