[scripts/generate_result] add '-o/--option' command-line option
This commit is contained in:
@@ -11,9 +11,10 @@
|
||||
|
||||
import logging
|
||||
import argparse
|
||||
import json
|
||||
import util
|
||||
from pyprint import pyprint
|
||||
from gallery_dl import extractor, job
|
||||
from gallery_dl import extractor, job, config
|
||||
|
||||
LOG = logging.getLogger("gen-test")
|
||||
|
||||
@@ -31,19 +32,32 @@ def generate_test_result(args):
|
||||
if args.only_matching:
|
||||
opts = meta = None
|
||||
else:
|
||||
if args.options:
|
||||
args.options_parsed = options = {}
|
||||
for opt in args.options:
|
||||
key, _, value = opt.partition("=")
|
||||
try:
|
||||
value = json.loads(value)
|
||||
except ValueError:
|
||||
pass
|
||||
options[key] = value
|
||||
config.set((), key, value)
|
||||
|
||||
djob = job.DataJob(args.extr, file=None)
|
||||
djob.filter = dict.copy
|
||||
djob.run()
|
||||
|
||||
opts = generate_opts(args, djob.data_urls)
|
||||
ool = (len(opts) > 1 or "#options" in opts)
|
||||
|
||||
if args.metadata:
|
||||
meta = generate_meta(args, djob.data_meta)
|
||||
else:
|
||||
meta = None
|
||||
|
||||
result = pyprint(head)
|
||||
result = pyprint(head, oneline=False, lmin=9)
|
||||
if opts:
|
||||
result = result[:-2] + pyprint(opts)[1:]
|
||||
result = result[:-2] + pyprint(opts, oneline=ool, lmin=9)[1:]
|
||||
if meta:
|
||||
result = result[:-1] + pyprint(meta)[1:]
|
||||
return result + ",\n\n"
|
||||
@@ -66,6 +80,9 @@ def generate_head(args):
|
||||
def generate_opts(args, urls):
|
||||
opts = {}
|
||||
|
||||
if args.options:
|
||||
opts["#options"] = args.options_parsed
|
||||
|
||||
if not urls:
|
||||
opts["#count"] = 0
|
||||
elif len(urls) == 1:
|
||||
@@ -112,7 +129,8 @@ def parse_args(args=None):
|
||||
parser.add_argument("-C", dest="comment", action="store_const", const="")
|
||||
parser.add_argument("-l", "--limit_urls", type=int, default=10)
|
||||
parser.add_argument("-m", "--metadata", action="store_true")
|
||||
parser.add_argument("-o", "--only-matching", action="store_true")
|
||||
parser.add_argument("-o", "--option", dest="options", action="append")
|
||||
parser.add_argument("-O", "--only-matching", action="store_true")
|
||||
parser.add_argument("URL")
|
||||
|
||||
return parser.parse_args()
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
import re
|
||||
|
||||
|
||||
def pyprint(obj, indent=0, sort=None, lmin=9, lmax=16):
|
||||
def pyprint(obj, indent=0, sort=None, oneline=True, lmin=0, lmax=16):
|
||||
|
||||
if isinstance(obj, str):
|
||||
if obj.startswith("lit:"):
|
||||
@@ -46,6 +46,9 @@ def pyprint(obj, indent=0, sort=None, lmin=9, lmax=16):
|
||||
if isinstance(obj, dict):
|
||||
if not obj:
|
||||
return "{}"
|
||||
if len(obj) == 1 and oneline:
|
||||
key, value = next(iter(obj.items()))
|
||||
return f'''{{"{key}": {pyprint(value, indent, sort)}}}'''
|
||||
|
||||
if sort:
|
||||
if callable(sort):
|
||||
@@ -79,7 +82,7 @@ def pyprint(obj, indent=0, sort=None, lmin=9, lmax=16):
|
||||
if isinstance(obj, list):
|
||||
if not obj:
|
||||
return "[]"
|
||||
if len(obj) == 1:
|
||||
if len(obj) == 1 and oneline:
|
||||
return f'''[{pyprint(obj[0], indent, sort)}]'''
|
||||
|
||||
ws = " " * indent
|
||||
|
||||
Reference in New Issue
Block a user