Merge pull request #261 from YunoHost/add-sort_keys-and-indent-to-write_to_json

add options to write_to_json
This commit is contained in:
Alexandre Aubin 2020-12-11 16:02:09 +01:00 committed by GitHub
commit 4bcaf99204
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -193,7 +193,7 @@ def append_to_file(file_path, data):
write_to_file(file_path, data, file_mode="a")
def write_to_json(file_path, data):
def write_to_json(file_path, data, sort_keys=False, indent=None):
"""
Write a dictionnary or a list to a json file
@ -222,7 +222,7 @@ def write_to_json(file_path, data):
# Write dict to file
try:
with open(file_path, "w") as f:
json.dump(data, f)
json.dump(data, f, sort_keys=sort_keys, indent=indent)
except IOError as e:
raise MoulinetteError("cannot_write_file", file=file_path, error=str(e))
except Exception as e: