From 9c3568c397646bd5d25e8e0a93046f7a9eaa11e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20F=C3=A4hrmann?= Date: Thu, 19 Nov 2020 01:31:52 +0100 Subject: [PATCH] [postprocessor:exec] add 'event' option and remove 'final' option -- use '"event": "finalize"' instead. --- gallery_dl/option.py | 3 +- gallery_dl/postprocessor/exec.py | 65 +++++++++++++++++--------------- 2 files changed, 37 insertions(+), 31 deletions(-) diff --git a/gallery_dl/option.py b/gallery_dl/option.py index 2a48c874..01537d6b 100644 --- a/gallery_dl/option.py +++ b/gallery_dl/option.py @@ -376,7 +376,8 @@ def build_parser(): postprocessor.add_argument( "--exec-after", dest="postprocessors", metavar="CMD", - action=AppendCommandAction, const={"name": "exec", "final": True}, + action=AppendCommandAction, const={ + "name": "exec", "event": "finalize"}, help=("Execute CMD after all files were downloaded successfully. " "Example: --exec-after 'cd {} && convert * ../doc.pdf'"), ) diff --git a/gallery_dl/postprocessor/exec.py b/gallery_dl/postprocessor/exec.py index 7c259a35..7886d6fa 100644 --- a/gallery_dl/postprocessor/exec.py +++ b/gallery_dl/postprocessor/exec.py @@ -24,49 +24,54 @@ class ExecPP(PostProcessor): def __init__(self, job, options): PostProcessor.__init__(self, job) - args = options["command"] - final = options.get("final", False) - - if isinstance(args, str): - if final: - self._format = self._format_args_directory - else: - self._format = self._format_args_path - if "{}" not in args: - args += " {}" - self.args = args - self.shell = True - else: - self._format = self._format_args_list - self.args = [util.Formatter(arg) for arg in args] - self.shell = False if options.get("async", False): self._exec = self._exec_async - event = "finalize" if final else "after" - job.hooks[event].append(self.run) + args = options["command"] + if isinstance(args, str): + if "{}" not in args: + args += " {}" + self.args = args + execute = self.exec_string + else: + self.args = [util.Formatter(arg) for arg in args] + execute = self.exec_list - def run(self, pathfmt, status=0): - if status == 0: - self._exec(self._format(pathfmt)) + events = options.get("event") + if events is None: + events = ("after",) + elif isinstance(events, str): + events = events.split(",") + for event in events: + job.hooks[event].append(execute) - def _format_args_path(self, pathfmt): - return self.args.replace("{}", quote(pathfmt.realpath)) + def exec_list(self, pathfmt, status=None): + if status: + return - def _format_args_directory(self, pathfmt): - return self.args.replace("{}", quote(pathfmt.realdirectory)) - - def _format_args_list(self, pathfmt): kwdict = pathfmt.kwdict kwdict["_directory"] = pathfmt.realdirectory kwdict["_filename"] = pathfmt.filename kwdict["_path"] = pathfmt.realpath - return [arg.format_map(kwdict) for arg in self.args] - def _exec(self, args): + args = [arg.format_map(kwdict) for arg in self.args] + self._exec(args, False) + + def exec_string(self, pathfmt, status=None): + if status: + return + + if status is None and pathfmt.realpath: + args = self.args.replace("{}", quote(pathfmt.realpath)) + else: + args = self.args.replace("{}", quote(pathfmt.realdirectory)) + + self._exec(args, True) + + def _exec(self, args, shell): self.log.debug("Running '%s'", args) - retcode = subprocess.Popen(args, shell=self.shell).wait() + retcode = subprocess.Popen(args, shell=shell).wait() if retcode: self.log.warning( "Executing '%s' returned with non-zero exit status (%d)",