[postprocessor:compare] extend 'action' option (#1592)
allow setting it to "abort", "terminate", or "exit" as with 'skip'
This commit is contained in:
@@ -2691,6 +2691,17 @@ Description
|
|||||||
The action to take when files do not compare as equal.
|
The action to take when files do not compare as equal.
|
||||||
|
|
||||||
* ``"replace"``: Replace/Overwrite the old version with the new one
|
* ``"replace"``: Replace/Overwrite the old version with the new one
|
||||||
|
|
||||||
|
* ``"abort:N"``: Same as ``"replace"`` and stop the current extractor run
|
||||||
|
after ``N`` consecutive files compared as equal.
|
||||||
|
|
||||||
|
* ``"terminate:N"``: Same as ``"replace"``
|
||||||
|
and stop the current extractor run, including parent extractors,
|
||||||
|
after ``N`` consecutive files compared as equal.
|
||||||
|
|
||||||
|
* ``"exit:N"``: Same as ``"replace"`` and exit the program
|
||||||
|
after ``N`` consecutive files compared as equal.
|
||||||
|
|
||||||
* ``"enumerate"``: Add an enumeration index to the filename of the new
|
* ``"enumerate"``: Add an enumeration index to the filename of the new
|
||||||
version like `skip = "enumerate" <extractor.*.skip_>`__
|
version like `skip = "enumerate" <extractor.*.skip_>`__
|
||||||
|
|
||||||
|
|||||||
@@ -9,6 +9,8 @@
|
|||||||
"""Compare versions of the same file and replace/enumerate them on mismatch"""
|
"""Compare versions of the same file and replace/enumerate them on mismatch"""
|
||||||
|
|
||||||
from .common import PostProcessor
|
from .common import PostProcessor
|
||||||
|
from .. import text, util, exception
|
||||||
|
import sys
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
|
||||||
@@ -19,16 +21,33 @@ class ComparePP(PostProcessor):
|
|||||||
if options.get("shallow"):
|
if options.get("shallow"):
|
||||||
self._compare = self._compare_size
|
self._compare = self._compare_size
|
||||||
|
|
||||||
job.register_hooks({"file": (
|
action = options.get("action")
|
||||||
self.enumerate
|
if action == "enumerate":
|
||||||
if options.get("action") == "enumerate" else
|
job.register_hooks({"file": self.enumerate}, options)
|
||||||
self.compare
|
else:
|
||||||
)}, options)
|
job.register_hooks({"file": self.compare}, options)
|
||||||
|
action, _, smax = action.partition(":")
|
||||||
|
self._skipmax = text.parse_int(smax)
|
||||||
|
self._skipexc = self._skipcnt = 0
|
||||||
|
if action == "abort":
|
||||||
|
self._skipexc = exception.StopExtraction
|
||||||
|
elif action == "terminate":
|
||||||
|
self._skipexc = exception.TerminateExtraction
|
||||||
|
elif action == "exit":
|
||||||
|
self._skipexc = sys.exit
|
||||||
|
|
||||||
def compare(self, pathfmt):
|
def compare(self, pathfmt):
|
||||||
try:
|
try:
|
||||||
if self._compare(pathfmt.realpath, pathfmt.temppath):
|
if self._compare(pathfmt.realpath, pathfmt.temppath):
|
||||||
|
if self._skipexc:
|
||||||
|
self._skipcnt += 1
|
||||||
|
if self._skipcnt >= self._skipmax:
|
||||||
|
util.remove_file(pathfmt.temppath)
|
||||||
|
print()
|
||||||
|
raise self._skipexc()
|
||||||
pathfmt.delete = True
|
pathfmt.delete = True
|
||||||
|
else:
|
||||||
|
self._skipcnt = 0
|
||||||
except OSError:
|
except OSError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user