1
0
mirror of https://github.com/mikf/gallery-dl.git synced 2024-11-22 10:42:34 +01:00

[postprocessor:metadata] filter private entries

i.e. keys starting with an underscore
This commit is contained in:
Mike Fährmann 2019-11-21 16:57:39 +01:00
parent ea094692c8
commit 3fc1e12949
No known key found for this signature in database
GPG Key ID: 5680CA389D365A88
6 changed files with 34 additions and 12 deletions

View File

@ -161,11 +161,6 @@ class Job():
if self.ulog:
self.ulog.info(url)
@staticmethod
def _filter(kwdict):
"""Return a copy of 'kwdict' without "private" entries"""
return {k: v for k, v in kwdict.items() if k[0] != "_"}
class DownloadJob(Job):
"""Download images into appropriate directory/filename locations"""
@ -479,6 +474,9 @@ class DataJob(Job):
self.data = []
self.ascii = config.get(("output", "ascii"), ensure_ascii)
private = config.get(("output", "private"))
self.filter = (lambda x: x) if private else util.filter_dict
def run(self):
# collect data
try:
@ -501,16 +499,16 @@ class DataJob(Job):
return 0
def handle_url(self, url, kwdict):
self.data.append((Message.Url, url, self._filter(kwdict)))
self.data.append((Message.Url, url, self.filter(kwdict)))
def handle_urllist(self, urls, kwdict):
self.data.append((Message.Urllist, list(urls), self._filter(kwdict)))
self.data.append((Message.Urllist, list(urls), self.filter(kwdict)))
def handle_directory(self, kwdict):
self.data.append((Message.Directory, self._filter(kwdict)))
self.data.append((Message.Directory, self.filter(kwdict)))
def handle_queue(self, url, kwdict):
self.data.append((Message.Queue, url, self._filter(kwdict)))
self.data.append((Message.Queue, url, self.filter(kwdict)))
def handle_finalize(self):
self.file.close()

View File

@ -58,7 +58,7 @@ class MetadataPP(PostProcessor):
file.write("\n")
def _write_json(self, file, kwdict):
util.dump_json(kwdict, file, self.ascii, self.indent)
util.dump_json(util.filter_dict(kwdict), file, self.ascii, self.indent)
__postprocessor__ = MetadataPP

View File

@ -78,6 +78,11 @@ def transform_dict(a, func):
a[key] = func(value)
def filter_dict(a):
"""Return a copy of 'a' without "private" entries"""
return {k: v for k, v in a.items() if k[0] != "_"}
def number_to_string(value, numbers=(int, float)):
"""Convert numbers (int, float) to string; Return everything else as is."""
return str(value) if value.__class__ in numbers else value

View File

@ -151,6 +151,9 @@ class MetadataTest(BasePostprocessorTest):
"ascii" : True,
"indent" : 2,
"extension": "JSON",
}, {
"public" : "hello",
"_private" : "world",
})
self.assertEqual(pp.write , pp._write_json)
@ -167,7 +170,8 @@ class MetadataTest(BasePostprocessorTest):
self.assertEqual(self._output(m), """{
"category": "test",
"extension": "ext",
"filename": "file"
"filename": "file",
"public": "hello"
}
""")

View File

@ -207,7 +207,7 @@ class ResultJob(job.DownloadJob):
def _update_kwdict(self, kwdict, to_list=True):
if to_list:
self.kwdict_list.append(kwdict.copy())
kwdict = self._filter(kwdict)
kwdict = util.filter_dict(kwdict)
self.kwdict_hash.update(
json.dumps(kwdict, sort_keys=True, default=str).encode())

View File

@ -358,6 +358,21 @@ class TestOther(unittest.TestCase):
self.assertEqual(
d, {1: 123, 2: 123, 3: 0, 4: {11: 321, 12: 321, 13: 0}})
def test_filter_dict(self):
d = {}
r = util.filter_dict(d)
self.assertEqual(r, d)
self.assertIsNot(r, d)
d = {"foo": 123, "bar": [], "baz": None}
r = util.filter_dict(d)
self.assertEqual(r, d)
self.assertIsNot(r, d)
d = {"foo": 123, "_bar": [], "__baz__": None}
r = util.filter_dict(d)
self.assertEqual(r, {"foo": 123})
def test_number_to_string(self, f=util.number_to_string):
self.assertEqual(f(1) , "1")
self.assertEqual(f(1.0) , "1.0")