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:
parent
ea094692c8
commit
3fc1e12949
@ -161,11 +161,6 @@ class Job():
|
|||||||
if self.ulog:
|
if self.ulog:
|
||||||
self.ulog.info(url)
|
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):
|
class DownloadJob(Job):
|
||||||
"""Download images into appropriate directory/filename locations"""
|
"""Download images into appropriate directory/filename locations"""
|
||||||
@ -479,6 +474,9 @@ class DataJob(Job):
|
|||||||
self.data = []
|
self.data = []
|
||||||
self.ascii = config.get(("output", "ascii"), ensure_ascii)
|
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):
|
def run(self):
|
||||||
# collect data
|
# collect data
|
||||||
try:
|
try:
|
||||||
@ -501,16 +499,16 @@ class DataJob(Job):
|
|||||||
return 0
|
return 0
|
||||||
|
|
||||||
def handle_url(self, url, kwdict):
|
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):
|
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):
|
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):
|
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):
|
def handle_finalize(self):
|
||||||
self.file.close()
|
self.file.close()
|
||||||
|
@ -58,7 +58,7 @@ class MetadataPP(PostProcessor):
|
|||||||
file.write("\n")
|
file.write("\n")
|
||||||
|
|
||||||
def _write_json(self, file, kwdict):
|
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
|
__postprocessor__ = MetadataPP
|
||||||
|
@ -78,6 +78,11 @@ def transform_dict(a, func):
|
|||||||
a[key] = func(value)
|
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)):
|
def number_to_string(value, numbers=(int, float)):
|
||||||
"""Convert numbers (int, float) to string; Return everything else as is."""
|
"""Convert numbers (int, float) to string; Return everything else as is."""
|
||||||
return str(value) if value.__class__ in numbers else value
|
return str(value) if value.__class__ in numbers else value
|
||||||
|
@ -151,6 +151,9 @@ class MetadataTest(BasePostprocessorTest):
|
|||||||
"ascii" : True,
|
"ascii" : True,
|
||||||
"indent" : 2,
|
"indent" : 2,
|
||||||
"extension": "JSON",
|
"extension": "JSON",
|
||||||
|
}, {
|
||||||
|
"public" : "hello",
|
||||||
|
"_private" : "world",
|
||||||
})
|
})
|
||||||
|
|
||||||
self.assertEqual(pp.write , pp._write_json)
|
self.assertEqual(pp.write , pp._write_json)
|
||||||
@ -167,7 +170,8 @@ class MetadataTest(BasePostprocessorTest):
|
|||||||
self.assertEqual(self._output(m), """{
|
self.assertEqual(self._output(m), """{
|
||||||
"category": "test",
|
"category": "test",
|
||||||
"extension": "ext",
|
"extension": "ext",
|
||||||
"filename": "file"
|
"filename": "file",
|
||||||
|
"public": "hello"
|
||||||
}
|
}
|
||||||
""")
|
""")
|
||||||
|
|
||||||
|
@ -207,7 +207,7 @@ class ResultJob(job.DownloadJob):
|
|||||||
def _update_kwdict(self, kwdict, to_list=True):
|
def _update_kwdict(self, kwdict, to_list=True):
|
||||||
if to_list:
|
if to_list:
|
||||||
self.kwdict_list.append(kwdict.copy())
|
self.kwdict_list.append(kwdict.copy())
|
||||||
kwdict = self._filter(kwdict)
|
kwdict = util.filter_dict(kwdict)
|
||||||
self.kwdict_hash.update(
|
self.kwdict_hash.update(
|
||||||
json.dumps(kwdict, sort_keys=True, default=str).encode())
|
json.dumps(kwdict, sort_keys=True, default=str).encode())
|
||||||
|
|
||||||
|
@ -358,6 +358,21 @@ class TestOther(unittest.TestCase):
|
|||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
d, {1: 123, 2: 123, 3: 0, 4: {11: 321, 12: 321, 13: 0}})
|
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):
|
def test_number_to_string(self, f=util.number_to_string):
|
||||||
self.assertEqual(f(1) , "1")
|
self.assertEqual(f(1) , "1")
|
||||||
self.assertEqual(f(1.0) , "1.0")
|
self.assertEqual(f(1.0) , "1.0")
|
||||||
|
Loading…
Reference in New Issue
Block a user