mirror of
https://github.com/mikf/gallery-dl.git
synced 2024-11-22 02:32:33 +01:00
[postprocessor:metadata] fix traversing more than 1 level deep
for mode 'modify' and 'delete'
This commit is contained in:
parent
5297ee0cd9
commit
7459e4abce
@ -124,10 +124,8 @@ class MetadataPP(PostProcessor):
|
||||
for key, func in self.fields.items():
|
||||
obj = kwdict
|
||||
try:
|
||||
while "[" in key:
|
||||
name, _, key = key.partition("[")
|
||||
obj = obj[name]
|
||||
key = key.rstrip("]")
|
||||
if "[" in key:
|
||||
obj, key = _traverse(obj, key)
|
||||
obj[key] = func(kwdict)
|
||||
except Exception:
|
||||
pass
|
||||
@ -137,10 +135,8 @@ class MetadataPP(PostProcessor):
|
||||
for key in self.fields:
|
||||
obj = kwdict
|
||||
try:
|
||||
while "[" in key:
|
||||
name, _, key = key.partition("[")
|
||||
obj = obj[name]
|
||||
key = key.rstrip("]")
|
||||
if "[" in key:
|
||||
obj, key = _traverse(obj, key)
|
||||
del obj[key]
|
||||
except Exception:
|
||||
pass
|
||||
@ -214,4 +210,15 @@ class MetadataPP(PostProcessor):
|
||||
)
|
||||
|
||||
|
||||
def _traverse(obj, key):
|
||||
name, _, key = key.partition("[")
|
||||
obj = obj[name]
|
||||
|
||||
while "[" in key:
|
||||
name, _, key = key.partition("[")
|
||||
obj = obj[name.rstrip("]")]
|
||||
|
||||
return obj, key.strip("]")
|
||||
|
||||
|
||||
__postprocessor__ = MetadataPP
|
||||
|
@ -392,47 +392,57 @@ class MetadataTest(BasePostprocessorTest):
|
||||
self._create({
|
||||
"mode": "modify",
|
||||
"fields": {
|
||||
"foo" : "{filename}-{foo!s}",
|
||||
"foo2" : "\fE bar['bax'] + 122",
|
||||
"bar[baz]": "{_now}",
|
||||
"bar[ba2]": "test",
|
||||
"foo" : "{filename}-{foo!s}",
|
||||
"foo2" : "\fE bar['bax'] + 122",
|
||||
"bar[baz]" : "{_now}",
|
||||
"bar[ba2]" : "\fE {}",
|
||||
"bar[ba2][a]": "test",
|
||||
},
|
||||
}, kwdict)
|
||||
pdict = self.pathfmt.kwdict
|
||||
|
||||
pdict = self.pathfmt.kwdict
|
||||
self.assertIsNot(kwdict, pdict)
|
||||
self.assertEqual(pdict["foo"], kwdict["foo"])
|
||||
self.assertEqual(pdict["bar"], kwdict["bar"])
|
||||
|
||||
self._trigger()
|
||||
|
||||
self.assertEqual(pdict["foo"] , "file-0")
|
||||
self.assertEqual(pdict["foo2"] , 123)
|
||||
self.assertEqual(pdict["bar"]["ba2"], "test")
|
||||
self.assertEqual(pdict["foo"] , "file-0")
|
||||
self.assertEqual(pdict["foo2"], 123)
|
||||
self.assertEqual(pdict["bar"]["ba2"]["a"], "test")
|
||||
self.assertIsInstance(pdict["bar"]["baz"], datetime)
|
||||
|
||||
def test_metadata_delete(self):
|
||||
kwdict = {"foo": 0, "bar": {"bax": 1, "bay": 2, "baz": 3}}
|
||||
self._create({"mode": "delete", "fields": ["foo", "bar[baz]"]}, kwdict)
|
||||
pdict = self.pathfmt.kwdict
|
||||
kwdict = {
|
||||
"foo": 0,
|
||||
"bar": {
|
||||
"bax": 1,
|
||||
"bay": 2,
|
||||
"baz": {"a": 3, "b": 4},
|
||||
},
|
||||
}
|
||||
self._create({
|
||||
"mode": "delete",
|
||||
"fields": ["foo", "bar[bax]", "bar[baz][a]"],
|
||||
}, kwdict)
|
||||
|
||||
pdict = self.pathfmt.kwdict
|
||||
self.assertIsNot(kwdict, pdict)
|
||||
|
||||
self.assertEqual(pdict["foo"], kwdict["foo"])
|
||||
self.assertEqual(pdict["bar"], kwdict["bar"])
|
||||
|
||||
del kwdict["foo"]
|
||||
del kwdict["bar"]["baz"]
|
||||
|
||||
self._trigger()
|
||||
|
||||
self.assertNotIn("foo", pdict)
|
||||
self.assertNotIn("baz", pdict["bar"])
|
||||
self.assertEqual(kwdict["bar"], pdict["bar"])
|
||||
self.assertNotIn("bax", pdict["bar"])
|
||||
self.assertNotIn("a", pdict["bar"]["baz"])
|
||||
|
||||
# no errors for deleted/undefined fields
|
||||
self._trigger()
|
||||
self.assertNotIn("foo", pdict)
|
||||
self.assertNotIn("baz", pdict["bar"])
|
||||
self.assertEqual(kwdict["bar"], pdict["bar"])
|
||||
self.assertNotIn("bax", pdict["bar"])
|
||||
self.assertNotIn("a", pdict["bar"]["baz"])
|
||||
|
||||
def test_metadata_option_skip(self):
|
||||
self._create({"skip": True})
|
||||
|
Loading…
Reference in New Issue
Block a user