[options] add '--config-type' command-line option
can also be set via 'GDL_CONFIG_TYPE' environment variable
9fd732afe8
This commit is contained in:
@@ -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:
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -21,38 +21,50 @@ 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":
|
||||
_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":
|
||||
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']}'")
|
||||
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"%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
|
||||
"${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:
|
||||
# look for config file in PyInstaller executable directory (#682)
|
||||
_default_configs.append(os.path.join(
|
||||
@@ -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"))
|
||||
|
||||
@@ -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",
|
||||
|
||||
Reference in New Issue
Block a user