[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')
pull/4412/head
Mike Fährmann 1 year ago
parent af4bdb62a7
commit 0ef1fcab20
No known key found for this signature in database
GPG Key ID: 5680CA389D365A88

@ -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``

@ -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

@ -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))

@ -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()

@ -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:

Loading…
Cancel
Save