[postprocessor:compare] add 'equal' option (#1592)

Move functionality from cdd72e14 to its own option,
where it can be used with any 'action'
pull/1936/head
Mike Fährmann 3 years ago
parent f8410203ef
commit df8050b81d
No known key found for this signature in database
GPG Key ID: 5680CA389D365A88

@ -1,5 +1,7 @@
# Changelog
## Unreleased
## 1.19.0 - 2021-10-01
### Additions
- [aryion] add `tag` extractor ([#1849](https://github.com/mikf/gallery-dl/issues/1849))

@ -2688,22 +2688,32 @@ Type
Default
``"replace"``
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
* ``"abort:N"``: Same as ``"replace"`` and stop the current extractor run
after ``N`` consecutive files compared as equal.
* ``"enumerate"``: Add an enumeration index to the filename of the new
version like `skip = "enumerate" <extractor.*.skip_>`__
* ``"terminate:N"``: Same as ``"replace"``
and stop the current extractor run, including parent extractors,
compare.equal
-------------
Type
``string``
Default
``"null"``
Description
The action to take when files do compare as equal.
* ``"abort:N"``: Stop the current extractor run
after ``N`` consecutive files compared as equal.
* ``"exit:N"``: Same as ``"replace"`` and exit the program
* ``"terminate:N"``: Stop the current extractor run,
including parent extractors,
after ``N`` consecutive files compared as equal.
* ``"enumerate"``: Add an enumeration index to the filename of the new
version like `skip = "enumerate" <extractor.*.skip_>`__
* ``"exit:N"``: Exit the program
after ``N`` consecutive files compared as equal.
compare.shallow

@ -20,36 +20,32 @@ class ComparePP(PostProcessor):
PostProcessor.__init__(self, job)
if options.get("shallow"):
self._compare = self._compare_size
self._equal_exc = self._equal_cnt = 0
action = options.get("action")
if action == "enumerate":
job.register_hooks({"file": self.enumerate}, options)
else:
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):
equal = options.get("equal")
if equal:
equal, _, emax = equal.partition(":")
self._equal_max = text.parse_int(emax)
if equal == "abort":
self._equal_exc = exception.StopExtraction
elif equal == "terminate":
self._equal_exc = exception.TerminateExtraction
elif equal == "exit":
self._equal_exc = sys.exit
job.register_hooks({"file": (
self.enumerate
if options.get("action") == "enumerate" else
self.replace
)}, options)
def replace(self, pathfmt):
try:
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
else:
self._skipcnt = 0
return self._equal(pathfmt)
except OSError:
pass
self._equal_cnt = 0
def enumerate(self, pathfmt):
num = 1
@ -58,9 +54,10 @@ class ComparePP(PostProcessor):
pathfmt.prefix = str(num) + "."
pathfmt.set_extension(pathfmt.extension, False)
num += 1
pathfmt.delete = True
return self._equal(pathfmt)
except OSError:
pass
self._equal_cnt = 0
def _compare(self, f1, f2):
return self._compare_size(f1, f2) and self._compare_content(f1, f2)
@ -81,5 +78,14 @@ class ComparePP(PostProcessor):
if not buf1:
return True
def _equal(self, pathfmt):
if self._equal_exc:
self._equal_cnt += 1
if self._equal_cnt >= self._equal_max:
util.remove_file(pathfmt.temppath)
print()
raise self._equal_exc()
pathfmt.delete = True
__postprocessor__ = ComparePP

@ -6,4 +6,4 @@
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
__version__ = "1.19.0"
__version__ = "1.19.1-dev"

Loading…
Cancel
Save