Skip to content

Commit

Permalink
[postprocessor:metadata] add 'skip' option (#3786)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikf committed Mar 17, 2023
1 parent 2bb9370 commit 00f0233
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
10 changes: 10 additions & 0 deletions docs/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4411,6 +4411,16 @@ Description
i.e. fields whose name starts with an underscore.


metadata.skip
-------------
Type
``bool``
Default
``false``
Description
Do not overwrite already existing files.


metadata.archive
----------------
Type
Expand Down
4 changes: 4 additions & 0 deletions gallery_dl/postprocessor/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ def __init__(self, job, options):
self.omode = options.get("open", omode)
self.encoding = options.get("encoding", "utf-8")
self.private = options.get("private", False)
self.skip = options.get("skip", False)

def run(self, pathfmt):
archive = self.archive
Expand All @@ -96,6 +97,9 @@ def run(self, pathfmt):
directory = self._directory(pathfmt)
path = directory + self._filename(pathfmt)

if self.skip and os.path.exists(path):
return

try:
with open(path, self.omode, encoding=self.encoding) as fp:
self.write(fp, pathfmt.kwdict)
Expand Down
35 changes: 35 additions & 0 deletions test/test_postprocessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -428,11 +428,46 @@ def test_metadata_delete(self):
self.assertNotIn("baz", pdict["bar"])
self.assertEqual(kwdict["bar"], pdict["bar"])

# no errors for deleted/undefined fields
self._trigger()
self.assertNotIn("foo", pdict)
self.assertNotIn("baz", pdict["bar"])
self.assertEqual(kwdict["bar"], pdict["bar"])

def test_metadata_option_skip(self):
self._create({"skip": True})

with patch("builtins.open", mock_open()) as m, \
patch("os.path.exists") as e:
e.return_value = True
self._trigger()

self.assertTrue(e.called)
self.assertTrue(not m.called)
self.assertTrue(not len(self._output(m)))

with patch("builtins.open", mock_open()) as m, \
patch("os.path.exists") as e:
e.return_value = False
self._trigger()

self.assertTrue(e.called)
self.assertTrue(m.called)
self.assertGreater(len(self._output(m)), 0)

path = self.pathfmt.realdirectory + "file.ext.json"
m.assert_called_once_with(path, "w", encoding="utf-8")

def test_metadata_option_skip_false(self):
self._create({"skip": False})

with patch("builtins.open", mock_open()) as m, \
patch("os.path.exists") as e:
self._trigger()

self.assertTrue(not e.called)
self.assertTrue(m.called)

@staticmethod
def _output(mock):
return "".join(
Expand Down

0 comments on commit 00f0233

Please sign in to comment.