[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-create Create a basic configuration file
|
||||||
--config-status Show configuration file status
|
--config-status Show configuration file status
|
||||||
--config-open Open configuration file in external application
|
--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
|
--config-ignore Do not read default configuration files
|
||||||
|
|
||||||
## Authentication Options:
|
## Authentication Options:
|
||||||
|
|||||||
@@ -26,6 +26,11 @@ def main():
|
|||||||
log = output.initialize_logging(args.loglevel)
|
log = output.initialize_logging(args.loglevel)
|
||||||
|
|
||||||
# configuration
|
# configuration
|
||||||
|
if args.config_type:
|
||||||
|
try:
|
||||||
|
config.default(args.config_type)
|
||||||
|
except Exception as exc:
|
||||||
|
config.log.error(exc)
|
||||||
if args.config_load:
|
if args.config_load:
|
||||||
config.load()
|
config.load()
|
||||||
if args.configs_json:
|
if args.configs_json:
|
||||||
|
|||||||
@@ -21,50 +21,58 @@ log = logging.getLogger("config")
|
|||||||
|
|
||||||
_config = {}
|
_config = {}
|
||||||
_files = []
|
_files = []
|
||||||
_type = os.environ.get("GDL_CONFIG_TYPE")
|
_type = "json"
|
||||||
|
_load = util.json_loads
|
||||||
if not _type or (_type := _type.lower()) == "json":
|
_default_configs = ()
|
||||||
_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",
|
|
||||||
))
|
|
||||||
|
|
||||||
|
|
||||||
# --------------------------------------------------------------------
|
# --------------------------------------------------------------------
|
||||||
# public interface
|
# 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():
|
def initialize():
|
||||||
paths = list(map(util.expand_path, _default_configs))
|
paths = list(map(util.expand_path, _default_configs))
|
||||||
|
|
||||||
@@ -366,3 +374,6 @@ class apply():
|
|||||||
unset(path, key)
|
unset(path, key)
|
||||||
else:
|
else:
|
||||||
set(path, key, value)
|
set(path, key, value)
|
||||||
|
|
||||||
|
|
||||||
|
default(os.environ.get("GDL_CONFIG_TYPE"))
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- 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
|
# 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
|
# 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",
|
dest="config", action="store_const", const="open",
|
||||||
help="Open configuration file in external application",
|
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(
|
configuration.add_argument(
|
||||||
"--config-ignore",
|
"--config-ignore",
|
||||||
dest="config_load", action="store_false",
|
dest="config_load", action="store_false",
|
||||||
|
|||||||
Reference in New Issue
Block a user