From 0ef1fcab2018426ddc2a294d7a9f5e07f72c4f67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20F=C3=A4hrmann?= Date: Thu, 10 Aug 2023 19:46:37 +0200 Subject: [PATCH] [postprocessor] update 'finalize' events Add 'finalize-error' and 'finalize-success' events that trigger depending on whether error(s) did or did not happen. 'finalize' itself now always triggers regardless of error status. (was supposed to have the same behavior as the new 'finalize-success') --- docs/configuration.rst | 4 ++++ gallery_dl/job.py | 12 ++++++++++-- gallery_dl/postprocessor/exec.py | 12 +++--------- gallery_dl/postprocessor/zip.py | 2 +- test/test_postprocessor.py | 8 ++++---- 5 files changed, 22 insertions(+), 16 deletions(-) diff --git a/docs/configuration.rst b/docs/configuration.rst index 549e456f..75d9c459 100644 --- a/docs/configuration.rst +++ b/docs/configuration.rst @@ -4512,6 +4512,10 @@ Description and before the first file download ``finalize`` On extractor shutdown, e.g. after all files were downloaded + ``finalize-success`` + On extractor shutdown when no error occurred + ``finalize-error`` + On extractor shutdown when at least one error occurred ``prepare`` Before a file download ``file`` diff --git a/gallery_dl/job.py b/gallery_dl/job.py index d33d35c1..fc379b35 100644 --- a/gallery_dl/job.py +++ b/gallery_dl/job.py @@ -407,10 +407,18 @@ class DownloadJob(Job): callback(pathfmt) self.extractor.cookies_store() + if "finalize" in hooks: - status = self.status for callback in hooks["finalize"]: - callback(pathfmt, status) + callback(pathfmt) + if self.status: + if "finalize-error" in hooks: + for callback in hooks["finalize-error"]: + callback(pathfmt) + else: + if "finalize-success" in hooks: + for callback in hooks["finalize-success"]: + callback(pathfmt) def handle_skip(self): pathfmt = self.pathfmt diff --git a/gallery_dl/postprocessor/exec.py b/gallery_dl/postprocessor/exec.py index 39188f16..afa828c0 100644 --- a/gallery_dl/postprocessor/exec.py +++ b/gallery_dl/postprocessor/exec.py @@ -46,10 +46,7 @@ class ExecPP(PostProcessor): self._init_archive(job, options) - def exec_list(self, pathfmt, status=None): - if status: - return - + def exec_list(self, pathfmt): archive = self.archive kwdict = pathfmt.kwdict @@ -67,15 +64,12 @@ class ExecPP(PostProcessor): if archive: archive.add(kwdict) - def exec_string(self, pathfmt, status=None): - if status: - return - + def exec_string(self, pathfmt): archive = self.archive if archive and archive.check(pathfmt.kwdict): return - if status is None and pathfmt.realpath: + if pathfmt.realpath: args = self.args.replace("{}", quote(pathfmt.realpath)) else: args = self.args.replace("{}", quote(pathfmt.realdirectory)) diff --git a/gallery_dl/postprocessor/zip.py b/gallery_dl/postprocessor/zip.py index 4f376fe6..ce36f2a7 100644 --- a/gallery_dl/postprocessor/zip.py +++ b/gallery_dl/postprocessor/zip.py @@ -88,7 +88,7 @@ class ZipPP(PostProcessor): if self.delete: util.remove_file(path) - def finalize(self, pathfmt, status): + def finalize(self, pathfmt): if self.zfile: self.zfile.close() diff --git a/test/test_postprocessor.py b/test/test_postprocessor.py index bcabdc8c..c00144e7 100644 --- a/test/test_postprocessor.py +++ b/test/test_postprocessor.py @@ -102,10 +102,10 @@ class BasePostprocessorTest(unittest.TestCase): pp = postprocessor.find(self.__class__.__name__[:-4].lower()) return pp(self.job, options) - def _trigger(self, events=None, *args): + def _trigger(self, events=None): for event in (events or ("prepare", "file")): for callback in self.job.hooks[event]: - callback(self.pathfmt, *args) + callback(self.pathfmt) class ClassifyTest(BasePostprocessorTest): @@ -679,7 +679,7 @@ class ZipTest(BasePostprocessorTest): self.assertEqual(len(pp.zfile.NameToInfo), 4) # close file - self._trigger(("finalize",), 0) + self._trigger(("finalize",)) # reopen to check persistence with zipfile.ZipFile(pp.zfile.filename) as file: @@ -712,7 +712,7 @@ class ZipTest(BasePostprocessorTest): self._trigger() # close file - self._trigger(("finalize",), 0) + self._trigger(("finalize",)) self.assertEqual(pp.zfile.write.call_count, 3) for call in pp.zfile.write.call_args_list: