1
0
mirror of https://github.com/mikf/gallery-dl.git synced 2024-11-22 10:42:34 +01:00

Merge branch 'options'

This commit is contained in:
Mike Fährmann 2015-11-14 17:23:24 +01:00
commit 5db2aa29cf
No known key found for this signature in database
GPG Key ID: 5680CA389D365A88
2 changed files with 40 additions and 19 deletions

View File

@ -19,47 +19,66 @@ import sys
import argparse import argparse
from . import config, jobs from . import config, jobs
def parse_cmdline_options(): def build_cmdline_parser():
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
description='Download images from various sources') description='Download images from various sources')
parser.add_argument( parser.add_argument(
"-c", "--config", "-c", "--config",
default="~/.config/gallery/config", metavar="CFG", metavar="CFG", dest="cfgfiles", action="append",
help="alternate configuration file" help="additional configuration files",
) )
parser.add_argument( parser.add_argument(
"-d", "--dest", "-d", "--dest",
metavar="DEST", metavar="DEST",
help="destination directory" help="destination directory",
) )
parser.add_argument( parser.add_argument(
"-o", "--option", "-o", "--option",
metavar="OPT", action="append", default=[], metavar="OPT", action="append", default=[],
help="option value", help="option value",
) )
parser.add_argument(
"--list-modules", dest="list_modules", action="store_true",
help="print a list of available modules/supported sites",
)
parser.add_argument( parser.add_argument(
"--list-keywords", dest="keywords", action="store_true", "--list-keywords", dest="keywords", action="store_true",
help="print a list of available keywords", help="print a list of available keywords for the given URLs",
) )
parser.add_argument( parser.add_argument(
"urls", "urls",
nargs="+", metavar="URL", nargs="*", metavar="URL",
help="url to download images from" help="url to download images from"
) )
return parser.parse_args() return parser
def main(): def main():
config.load()
args = parse_cmdline_options()
for opt in args.option:
try:
key, value = opt.split("=", 1)
config.set(key.split("."), value)
except TypeError:
pass
jobtype = jobs.KeywordJob if args.keywords else jobs.DownloadJob
try: try:
for url in args.urls: config.load()
jobtype(url).run() parser = build_cmdline_parser()
args = parser.parse_args()
if args.cfgfiles:
config.load(*args.cfgfiles, strict=True)
if args.dest:
config.set(("base-directory",), args.dest)
for opt in args.option:
try:
key, value = opt.split("=", 1)
config.set(key.split("."), value)
except TypeError:
pass
if args.list_modules:
for module_name in extractor.modules:
print(module_name)
else:
if not args.urls:
parser.error("the following arguments are required: URL")
jobtype = jobs.KeywordJob if args.keywords else jobs.DownloadJob
for url in args.urls:
jobtype(url).run()
except KeyboardInterrupt: except KeyboardInterrupt:
print("\nKeyboardInterrupt") print("\nKeyboardInterrupt")

View File

@ -16,7 +16,7 @@ import platform
# -------------------------------------------------------------------- # --------------------------------------------------------------------
# public interface # public interface
def load(*files): def load(*files, strict=False):
"""Load JSON configuration files""" """Load JSON configuration files"""
configfiles = files or _default_configs configfiles = files or _default_configs
for conf in configfiles: for conf in configfiles:
@ -26,6 +26,8 @@ def load(*files):
confdict = json.load(file) confdict = json.load(file)
_config.update(confdict) _config.update(confdict)
except FileNotFoundError: except FileNotFoundError:
if strict:
raise
continue continue
except json.decoder.JSONDecodeError as exception: except json.decoder.JSONDecodeError as exception:
print("Error while loading '", path, "':", sep="", file=sys.stderr) print("Error while loading '", path, "':", sep="", file=sys.stderr)