Skip to content

Commit

Permalink
add 'output.num-to-str' option
Browse files Browse the repository at this point in the history
... to convert any numeric values to string when outputting them as JSON
(during '--dump-json' or otherwise)
  • Loading branch information
mikf committed Oct 8, 2018
1 parent af3f81c commit 48a8717
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
11 changes: 9 additions & 2 deletions gallery_dl/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,6 @@ def __init__(self, url, parent=None, file=sys.stdout):
Job.__init__(self, url, parent)
self.file = file
self.data = []
self.ensure_ascii = config.get(("output", "ascii"), True)

def run(self):
# collect data
Expand All @@ -510,10 +509,15 @@ def run(self):
except BaseException:
pass

if config.get(("output", "num-to-str"), False):
for msg in self.data:
util.transform_dict(msg[-1], util.number_to_string)

# dump to 'file'
json.dump(
self.data, self.file,
sort_keys=True, indent=2, ensure_ascii=self.ensure_ascii
sort_keys=True, indent=2,
ensure_ascii=config.get(("output", "ascii"), True),
)
self.file.write("\n")

Expand All @@ -528,3 +532,6 @@ def handle_directory(self, keywords):

def handle_queue(self, url, keywords):
self.data.append((Message.Queue, url, keywords.copy()))

def handle_finalize(self):
self.file.close()
16 changes: 15 additions & 1 deletion gallery_dl/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def wrap():


def combine_dict(a, b):
"""Recursively combine the contents of b into a"""
"""Recursively combine the contents of 'b' into 'a'"""
for key, value in b.items():
if key in a and isinstance(value, dict) and isinstance(a[key], dict):
combine_dict(a[key], value)
Expand All @@ -66,6 +66,20 @@ def combine_dict(a, b):
return a


def transform_dict(a, func):
"""Recursively apply 'func' to all values in 'a'"""
for key, value in a.items():
if isinstance(value, dict):
transform_dict(value, func)
else:
a[key] = func(value)


def number_to_string(value):
"""Convert numbers (int, float) to string; Return everything else as is."""
return str(value) if isinstance(value, (int, float)) else value


def expand_path(path):
"""Expand environment variables and tildes (~)"""
if not path:
Expand Down

0 comments on commit 48a8717

Please sign in to comment.