rework post processor callbacks

This commit is contained in:
Mike Fährmann
2020-11-18 17:11:55 +01:00
parent d6986be8b0
commit 9fffa9c343
10 changed files with 109 additions and 121 deletions

View File

@@ -32,13 +32,16 @@ class ClassifyPP(PostProcessor):
for ext in exts
}
job.hooks["prepare"].append(self.prepare)
job.hooks["file"].append(self.move)
def prepare(self, pathfmt):
ext = pathfmt.extension
if ext in self.mapping:
# set initial paths to enable download skips
self._build_paths(pathfmt, self.mapping[ext])
def run(self, pathfmt):
def move(self, pathfmt):
ext = pathfmt.extension
if ext in self.mapping:
# rebuild paths in case the filename extension changed

View File

@@ -16,25 +16,5 @@ class PostProcessor():
name = self.__class__.__name__[:-2].lower()
self.log = job.get_logger("postprocessor." + name)
@staticmethod
def prepare(pathfmt):
"""Update file paths, etc."""
@staticmethod
def run(pathfmt):
"""Execute the postprocessor for a file"""
@staticmethod
def run_metadata(pathfmt):
"""Execute the postprocessor for a file"""
@staticmethod
def run_after(pathfmt):
"""Execute postprocessor after moving a file to its target location"""
@staticmethod
def run_final(pathfmt, status):
"""Postprocessor finalization after all files have been downloaded"""
def __repr__(self):
return self.__class__.__name__

View File

@@ -16,22 +16,25 @@ class ComparePP(PostProcessor):
def __init__(self, job, options):
PostProcessor.__init__(self, job)
if options.get("action") == "enumerate":
self.run = self._run_enumerate
if options.get("shallow"):
self.compare = self._compare_size
self._compare = self._compare_size
job.hooks["file"].append(
self.enumerate
if options.get("action") == "enumerate" else
self.compare
)
def run(self, pathfmt):
def compare(self, pathfmt):
try:
if self.compare(pathfmt.realpath, pathfmt.temppath):
if self._compare(pathfmt.realpath, pathfmt.temppath):
pathfmt.delete = True
except OSError:
pass
def _run_enumerate(self, pathfmt):
def enumerate(self, pathfmt):
num = 1
try:
while not self.compare(pathfmt.realpath, pathfmt.temppath):
while not self._compare(pathfmt.realpath, pathfmt.temppath):
pathfmt.prefix = str(num) + "."
pathfmt.set_extension(pathfmt.extension, False)
num += 1
@@ -39,7 +42,7 @@ class ComparePP(PostProcessor):
except OSError:
pass
def compare(self, f1, f2):
def _compare(self, f1, f2):
return self._compare_size(f1, f2) and self._compare_content(f1, f2)
@staticmethod

View File

@@ -41,18 +41,13 @@ class ExecPP(PostProcessor):
self.args = [util.Formatter(arg) for arg in args]
self.shell = False
if final:
self.run_after = PostProcessor.run_after
else:
self.run_final = PostProcessor.run_final
if options.get("async", False):
self._exec = self._exec_async
def run_after(self, pathfmt):
self._exec(self._format(pathfmt))
event = "finalize" if final else "after"
job.hooks[event].append(self.run)
def run_final(self, pathfmt, status):
def run(self, pathfmt, status=0):
if status == 0:
self._exec(self._format(pathfmt))

View File

@@ -48,8 +48,8 @@ class MetadataPP(PostProcessor):
else:
self.extension = options.get("extension", ext)
if options.get("bypost"):
self.run_metadata, self.run = self.run, self.run_metadata
event = "metadata" if options.get("bypost") else "file"
job.hooks[event].append(self.run)
def run(self, pathfmt):
path = self._directory(pathfmt) + self._filename(pathfmt)

View File

@@ -17,6 +17,7 @@ class MtimePP(PostProcessor):
def __init__(self, job, options):
PostProcessor.__init__(self, job)
self.key = options.get("key", "date")
job.hooks["file"].append(self.run)
def run(self, pathfmt):
mtime = pathfmt.kwdict.get(self.key)

View File

@@ -49,6 +49,9 @@ class UgoiraPP(PostProcessor):
else:
self.prevent_odd = False
job.hooks["prepare"].append(self.prepare)
job.hooks["file"].append(self.convert)
def prepare(self, pathfmt):
self._frames = None
@@ -65,7 +68,7 @@ class UgoiraPP(PostProcessor):
if self.delete:
pathfmt.set_extension(self.extension)
def run(self, pathfmt):
def convert(self, pathfmt):
if not self._frames:
return

View File

@@ -38,12 +38,11 @@ class ZipPP(PostProcessor):
self.args = (self.path[:-1] + ext, "a",
self.COMPRESSION_ALGORITHMS[algorithm], True)
if options.get("mode") == "safe":
self.run = self._write_safe
else:
self.run = self._write
job.hooks["file"].append(
self.write_safe if options.get("mode") == "safe" else self.write)
job.hooks["finalize"].append(self.finalize)
def _write(self, pathfmt, zfile=None):
def write(self, pathfmt, zfile=None):
# 'NameToInfo' is not officially documented, but it's available
# for all supported Python versions and using it directly is a lot
# faster than calling getinfo()
@@ -55,11 +54,11 @@ class ZipPP(PostProcessor):
zfile.write(pathfmt.temppath, pathfmt.filename)
pathfmt.delete = self.delete
def _write_safe(self, pathfmt):
def write_safe(self, pathfmt):
with zipfile.ZipFile(*self.args) as zfile:
self._write(pathfmt, zfile)
def run_final(self, pathfmt, status):
def finalize(self, pathfmt, status):
if self.zfile:
self.zfile.close()