1
0
mirror of https://github.com/mikf/gallery-dl.git synced 2024-11-25 04:02:32 +01:00

add '--config-create' command-line option

(#2333)
This commit is contained in:
Mike Fährmann 2023-03-01 14:49:40 +01:00
parent 26d06e0bb2
commit 075c965512
No known key found for this signature in database
GPG Key ID: 5680CA389D365A88
4 changed files with 54 additions and 3 deletions

View File

@ -76,6 +76,7 @@
-c, --config FILE Additional configuration files
--config-yaml FILE Additional configuration files in YAML format
--config-toml FILE Additional configuration files in TOML format
--config-create Create a basic configuration file
--config-ignore Do not read default configuration files
## Authentication Options:

View File

@ -38,7 +38,7 @@ def main():
log = output.initialize_logging(args.loglevel)
# configuration
if args.load_config:
if args.config_load:
config.load()
if args.configs_json:
config.load(args.configs_json, strict=True)
@ -218,6 +218,10 @@ def main():
"Deleted %d %s from '%s'",
cnt, "entry" if cnt == 1 else "entries", cache._path(),
)
elif args.config_init:
return config.initialize()
else:
if not args.urls and not args.inputfiles:
parser.error(

View File

@ -49,6 +49,47 @@ if util.EXECUTABLE:
# --------------------------------------------------------------------
# public interface
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:
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:
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:

View File

@ -341,14 +341,19 @@ def build_parser():
dest="configs_toml", metavar="FILE", action="append",
help="Additional configuration files in TOML format",
)
configuration.add_argument(
"--config-create",
dest="config_init", action="store_true",
help="Create a basic configuration file",
)
configuration.add_argument(
"--config-ignore",
dest="load_config", action="store_false",
dest="config_load", action="store_false",
help="Do not read default configuration files",
)
configuration.add_argument(
"--ignore-config",
dest="load_config", action="store_false",
dest="config_load", action="store_false",
help=argparse.SUPPRESS,
)