[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 # Changelog
## Unreleased
## 1.19.0 - 2021-10-01 ## 1.19.0 - 2021-10-01
### Additions ### Additions
- [aryion] add `tag` extractor ([#1849](https://github.com/mikf/gallery-dl/issues/1849)) - [aryion] add `tag` extractor ([#1849](https://github.com/mikf/gallery-dl/issues/1849))

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

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

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

Loading…
Cancel
Save