From f58215705acd3807e617725d8a2e772f3d0ea504 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20F=C3=A4hrmann?= Date: Thu, 26 Jan 2023 14:59:24 +0100 Subject: [PATCH] add '-O/--postprocessor-option' command-line option (#3565) --- docs/configuration.rst | 18 ++++++++++++++++++ docs/options.md | 3 +++ gallery_dl/__init__.py | 4 +++- gallery_dl/job.py | 9 +++++++-- gallery_dl/option.py | 29 +++++++++++++++++++++++------ 5 files changed, 54 insertions(+), 9 deletions(-) diff --git a/docs/configuration.rst b/docs/configuration.rst index 7de85045..9d4730da 100644 --- a/docs/configuration.rst +++ b/docs/configuration.rst @@ -796,6 +796,24 @@ Description for each downloaded ``pixiv`` file. +extractor.*.postprocessor-options +--------------------------------- +Type + ``object`` (`name` -> `value`) +Example + .. code:: json + + { + "archive": null, + "keep-files": true + } + +Description + Additional `Postprocessor Options`_ that get added to each individual + `post processor object `__ + before initializing it and evaluating filters. + + extractor.*.retries ------------------- Type diff --git a/docs/options.md b/docs/options.md index dddb6001..7bffbd41 100644 --- a/docs/options.md +++ b/docs/options.md @@ -119,3 +119,6 @@ successfully. Example: --exec-after "cd {} && convert * ../doc.pdf" -P, --postprocessor NAME Activate the specified post processor + -O, --postprocessor-option OPT + Additional '=' post processor + options diff --git a/gallery_dl/__init__.py b/gallery_dl/__init__.py index 611b2b92..245dbf8b 100644 --- a/gallery_dl/__init__.py +++ b/gallery_dl/__init__.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- -# Copyright 2014-2022 Mike Fährmann +# Copyright 2014-2023 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 @@ -72,6 +72,8 @@ def main(): else: profile, _, container = profile.partition("::") config.set((), "cookies", (browser, profile, keyring, container)) + if args.options_pp: + config.set((), "postprocessor-options", args.options_pp) for opts in args.options: config.set(*opts) diff --git a/gallery_dl/job.py b/gallery_dl/job.py index e1a67671..f7d84f03 100644 --- a/gallery_dl/job.py +++ b/gallery_dl/job.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- -# Copyright 2015-2022 Mike Fährmann +# Copyright 2015-2023 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 @@ -473,13 +473,18 @@ class DownloadJob(Job): postprocessors = extr.config_accumulate("postprocessors") if postprocessors: self.hooks = collections.defaultdict(list) + pp_log = self.get_logger("postprocessor") + pp_conf = config.get((), "postprocessor") or {} + pp_opts = cfg("postprocessor-options") pp_list = [] - pp_conf = config.get((), "postprocessor") or {} for pp_dict in postprocessors: if isinstance(pp_dict, str): pp_dict = pp_conf.get(pp_dict) or {"name": pp_dict} + if pp_opts: + pp_dict = pp_dict.copy() + pp_dict.update(pp_opts) clist = pp_dict.get("whitelist") if clist is not None: diff --git a/gallery_dl/option.py b/gallery_dl/option.py index 32cac791..213cd2d6 100644 --- a/gallery_dl/option.py +++ b/gallery_dl/option.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- -# Copyright 2017-2022 Mike Fährmann +# Copyright 2017-2023 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 @@ -48,15 +48,18 @@ class DeprecatedConfigConstAction(argparse.Action): class ParseAction(argparse.Action): """Parse = options and set them as config values""" def __call__(self, parser, namespace, values, option_string=None): - key, _, value = values.partition("=") - try: - value = json.loads(value) - except ValueError: - pass + key, value = _parse_option(values) key = key.split(".") # splitting an empty string becomes [""] namespace.options.append((key[:-1], key[-1], value)) +class OptionAction(argparse.Action): + """Parse = options for """ + def __call__(self, parser, namespace, values, option_string=None): + key, value = _parse_option(values) + namespace.options_pp[key] = value + + class Formatter(argparse.HelpFormatter): """Custom HelpFormatter class to customize help output""" def __init__(self, *args, **kwargs): @@ -73,6 +76,15 @@ class Formatter(argparse.HelpFormatter): return self._metavar_formatter(action, action.dest)(1)[0] +def _parse_option(opt): + key, _, value = opt.partition("=") + try: + value = json.loads(value) + except ValueError: + pass + return key, value + + def build_parser(): """Build and configure an ArgumentParser object""" parser = argparse.ArgumentParser( @@ -488,6 +500,11 @@ def build_parser(): dest="postprocessors", metavar="NAME", action="append", help="Activate the specified post processor", ) + postprocessor.add_argument( + "-O", "--postprocessor-option", + dest="options_pp", metavar="OPT", action=OptionAction, default={}, + help="Additional '=' post processor options", + ) parser.add_argument( "urls",