[options] add '--config-type' command-line option

can also be set via 'GDL_CONFIG_TYPE' environment variable
9fd732afe8
This commit is contained in:
Mike Fährmann
2026-02-07 21:40:30 +01:00
parent 8eafa1564a
commit 56f2790626
4 changed files with 63 additions and 39 deletions

View File

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

View File

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

View File

@@ -21,39 +21,51 @@ log = logging.getLogger("config")
_config = {}
_files = []
_type = os.environ.get("GDL_CONFIG_TYPE")
_type = "json"
_load = util.json_loads
_default_configs = ()
if not _type or (_type := _type.lower()) == "json":
_type = "json"
# --------------------------------------------------------------------
# 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":
elif type == "yaml":
_type = "yaml"
from yaml import safe_load as _load
elif _type == "toml":
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 "
f"'{os.environ['GDL_CONFIG_TYPE']}'")
else:
raise ValueError(f"Unsupported config file type '{type}'")
if util.WINDOWS:
if util.WINDOWS:
_default_configs = [
r"%APPDATA%\gallery-dl\config." + _type,
r"%USERPROFILE%\gallery-dl\config." + _type,
r"%APPDATA%\gallery-dl\config." + type,
r"%USERPROFILE%\gallery-dl\config." + type,
r"%USERPROFILE%\gallery-dl.conf",
]
else:
else:
_default_configs = [
"/etc/gallery-dl.conf",
"${XDG_CONFIG_HOME}/gallery-dl/config." + _type
"${XDG_CONFIG_HOME}/gallery-dl/config." + type
if os.environ.get("XDG_CONFIG_HOME") else
"${HOME}/.config/gallery-dl/config." + _type,
"${HOME}/.config/gallery-dl/config." + type,
"${HOME}/.gallery-dl.conf",
]
if util.EXECUTABLE:
if util.EXECUTABLE:
# look for config file in PyInstaller executable directory (#682)
_default_configs.append(os.path.join(
os.path.dirname(sys.executable),
@@ -61,10 +73,6 @@ if util.EXECUTABLE:
))
# --------------------------------------------------------------------
# public interface
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"))

View File

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