[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')
This commit is contained in:
Mike Fährmann
2023-08-10 19:46:37 +02:00
parent af4bdb62a7
commit 0ef1fcab20
5 changed files with 22 additions and 16 deletions

View File

@@ -4512,6 +4512,10 @@ Description
and before the first file download and before the first file download
``finalize`` ``finalize``
On extractor shutdown, e.g. after all files were downloaded 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`` ``prepare``
Before a file download Before a file download
``file`` ``file``

View File

@@ -407,10 +407,18 @@ class DownloadJob(Job):
callback(pathfmt) callback(pathfmt)
self.extractor.cookies_store() self.extractor.cookies_store()
if "finalize" in hooks: if "finalize" in hooks:
status = self.status
for callback in hooks["finalize"]: 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): def handle_skip(self):
pathfmt = self.pathfmt pathfmt = self.pathfmt

View File

@@ -46,10 +46,7 @@ class ExecPP(PostProcessor):
self._init_archive(job, options) self._init_archive(job, options)
def exec_list(self, pathfmt, status=None): def exec_list(self, pathfmt):
if status:
return
archive = self.archive archive = self.archive
kwdict = pathfmt.kwdict kwdict = pathfmt.kwdict
@@ -67,15 +64,12 @@ class ExecPP(PostProcessor):
if archive: if archive:
archive.add(kwdict) archive.add(kwdict)
def exec_string(self, pathfmt, status=None): def exec_string(self, pathfmt):
if status:
return
archive = self.archive archive = self.archive
if archive and archive.check(pathfmt.kwdict): if archive and archive.check(pathfmt.kwdict):
return return
if status is None and pathfmt.realpath: if pathfmt.realpath:
args = self.args.replace("{}", quote(pathfmt.realpath)) args = self.args.replace("{}", quote(pathfmt.realpath))
else: else:
args = self.args.replace("{}", quote(pathfmt.realdirectory)) args = self.args.replace("{}", quote(pathfmt.realdirectory))

View File

@@ -88,7 +88,7 @@ class ZipPP(PostProcessor):
if self.delete: if self.delete:
util.remove_file(path) util.remove_file(path)
def finalize(self, pathfmt, status): def finalize(self, pathfmt):
if self.zfile: if self.zfile:
self.zfile.close() self.zfile.close()

View File

@@ -102,10 +102,10 @@ class BasePostprocessorTest(unittest.TestCase):
pp = postprocessor.find(self.__class__.__name__[:-4].lower()) pp = postprocessor.find(self.__class__.__name__[:-4].lower())
return pp(self.job, options) return pp(self.job, options)
def _trigger(self, events=None, *args): def _trigger(self, events=None):
for event in (events or ("prepare", "file")): for event in (events or ("prepare", "file")):
for callback in self.job.hooks[event]: for callback in self.job.hooks[event]:
callback(self.pathfmt, *args) callback(self.pathfmt)
class ClassifyTest(BasePostprocessorTest): class ClassifyTest(BasePostprocessorTest):
@@ -679,7 +679,7 @@ class ZipTest(BasePostprocessorTest):
self.assertEqual(len(pp.zfile.NameToInfo), 4) self.assertEqual(len(pp.zfile.NameToInfo), 4)
# close file # close file
self._trigger(("finalize",), 0) self._trigger(("finalize",))
# reopen to check persistence # reopen to check persistence
with zipfile.ZipFile(pp.zfile.filename) as file: with zipfile.ZipFile(pp.zfile.filename) as file:
@@ -712,7 +712,7 @@ class ZipTest(BasePostprocessorTest):
self._trigger() self._trigger()
# close file # close file
self._trigger(("finalize",), 0) self._trigger(("finalize",))
self.assertEqual(pp.zfile.write.call_count, 3) self.assertEqual(pp.zfile.write.call_count, 3)
for call in pp.zfile.write.call_args_list: for call in pp.zfile.write.call_args_list: