Skip to content

Commit

Permalink
[postprocessor:compare] add 'equal' option (#1592)
Browse files Browse the repository at this point in the history
Move functionality from cdd72e1 to its own option,
where it can be used with any 'action'
  • Loading branch information
mikf committed Oct 5, 2021
1 parent f841020 commit df8050b
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 35 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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))
Expand Down
26 changes: 18 additions & 8 deletions docs/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
58 changes: 32 additions & 26 deletions gallery_dl/postprocessor/compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,36 +20,32 @@ def __init__(self, job, options):
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
Expand All @@ -58,9 +54,10 @@ def enumerate(self, pathfmt):
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)
Expand All @@ -81,5 +78,14 @@ def _compare_content(f1, f2):
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
2 changes: 1 addition & 1 deletion gallery_dl/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

0 comments on commit df8050b

Please sign in to comment.