Skip to content

Commit b2c8151

Browse files
committed
RF: avoid code duplication in writing json and add final newline if pretty
1 parent 8a97403 commit b2c8151

1 file changed

Lines changed: 6 additions & 8 deletions

File tree

tributors/utils/file.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,13 @@ def write_json(json_obj, filename, pretty=True):
2020
- json_obj (dict) : the dict to print to json
2121
- filename (str) : the output file to write to
2222
"""
23+
kw = dict(indent=4, separators=(",", ": ")) if pretty else {}
2324
with open(filename, "w", encoding="utf8") as filey:
24-
if pretty:
25-
filey.writelines(
26-
json.dumps(
27-
json_obj, indent=4, ensure_ascii=False, separators=(",", ": ")
28-
)
29-
)
30-
else:
31-
filey.writelines(json.dumps(json_obj), ensure_ascii=False)
25+
dump = json.dumps(json_obj, ensure_ascii=False, **kw)
26+
if pretty and not dump.endswith(os.linesep):
27+
# Add newline as it is typically desired
28+
dump += os.linesep
29+
filey.write(dump)
3230
return filename
3331

3432

0 commit comments

Comments
 (0)