diff --git a/docs/options.md b/docs/options.md index c339d788..b6624e6d 100644 --- a/docs/options.md +++ b/docs/options.md @@ -134,6 +134,8 @@ --config-create Create a basic configuration file --config-status Show configuration file status --config-open Open configuration file in external application + --config-type TYPE Set filetype of default configuration files + (json, yaml, toml) --config-ignore Do not read default configuration files ## Authentication Options: diff --git a/gallery_dl/__init__.py b/gallery_dl/__init__.py index e018748b..b6a275ee 100644 --- a/gallery_dl/__init__.py +++ b/gallery_dl/__init__.py @@ -26,6 +26,11 @@ def main(): log = output.initialize_logging(args.loglevel) # configuration + if args.config_type: + try: + config.default(args.config_type) + except Exception as exc: + config.log.error(exc) if args.config_load: config.load() if args.configs_json: diff --git a/gallery_dl/config.py b/gallery_dl/config.py index d3d2d4db..1323bda3 100644 --- a/gallery_dl/config.py +++ b/gallery_dl/config.py @@ -21,50 +21,58 @@ log = logging.getLogger("config") _config = {} _files = [] -_type = os.environ.get("GDL_CONFIG_TYPE") - -if not _type or (_type := _type.lower()) == "json": - _type = "json" - _load = util.json_loads -elif _type == "yaml": - from yaml import safe_load as _load -elif _type == "toml": - try: - from tomllib import loads as _load - except ImportError: - from toml import loads as _load -else: - raise ValueError(f"Unsupported config file type " - f"'{os.environ['GDL_CONFIG_TYPE']}'") - -if util.WINDOWS: - _default_configs = [ - r"%APPDATA%\gallery-dl\config." + _type, - r"%USERPROFILE%\gallery-dl\config." + _type, - r"%USERPROFILE%\gallery-dl.conf", - ] -else: - _default_configs = [ - "/etc/gallery-dl.conf", - "${XDG_CONFIG_HOME}/gallery-dl/config." + _type - if os.environ.get("XDG_CONFIG_HOME") else - "${HOME}/.config/gallery-dl/config." + _type, - "${HOME}/.gallery-dl.conf", - ] - - -if util.EXECUTABLE: - # look for config file in PyInstaller executable directory (#682) - _default_configs.append(os.path.join( - os.path.dirname(sys.executable), - "gallery-dl.conf", - )) +_type = "json" +_load = util.json_loads +_default_configs = () # -------------------------------------------------------------------- # public interface +def default(type=None): + global _type + global _load + global _default_configs + + if not type or (type := type.lower()) == "json": + _type = type = "json" + _load = util.json_loads + elif type == "yaml": + _type = "yaml" + from yaml import safe_load as _load + elif type == "toml": + _type = "toml" + try: + from tomllib import loads as _load + except ImportError: + from toml import loads as _load + else: + raise ValueError(f"Unsupported config file type '{type}'") + + if util.WINDOWS: + _default_configs = [ + r"%APPDATA%\gallery-dl\config." + type, + r"%USERPROFILE%\gallery-dl\config." + type, + r"%USERPROFILE%\gallery-dl.conf", + ] + else: + _default_configs = [ + "/etc/gallery-dl.conf", + "${XDG_CONFIG_HOME}/gallery-dl/config." + type + if os.environ.get("XDG_CONFIG_HOME") else + "${HOME}/.config/gallery-dl/config." + type, + "${HOME}/.gallery-dl.conf", + ] + + if util.EXECUTABLE: + # look for config file in PyInstaller executable directory (#682) + _default_configs.append(os.path.join( + os.path.dirname(sys.executable), + "gallery-dl.conf", + )) + + def initialize(): paths = list(map(util.expand_path, _default_configs)) @@ -366,3 +374,6 @@ class apply(): unset(path, key) else: set(path, key, value) + + +default(os.environ.get("GDL_CONFIG_TYPE")) diff --git a/gallery_dl/option.py b/gallery_dl/option.py index 08bbe1bc..61cf092e 100644 --- a/gallery_dl/option.py +++ b/gallery_dl/option.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- -# Copyright 2017-2025 Mike Fährmann +# Copyright 2017-2026 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 @@ -633,6 +633,12 @@ def build_parser(): dest="config", action="store_const", const="open", help="Open configuration file in external application", ) + configuration.add_argument( + "--config-type", + dest="config_type", metavar="TYPE", + help=("Set filetype of default configuration files " + "(json, yaml, toml)"), + ) configuration.add_argument( "--config-ignore", dest="config_load", action="store_false",