refactor: move json writing into utility functions

The same options where reused in multiple places
This commit is contained in:
2014-12-12 14:32:35 +01:00
parent 4e3a9ca9b7
commit 2dd2fd9c8d
2 changed files with 32 additions and 28 deletions

View File

@@ -69,3 +69,26 @@ def uuid_from_file(fn, block_size=1 << 20):
# skip the '0x'
return hex(size)[2:] + sha1.hexdigest()
def write_json_to_zip(zip_handle, path, data=None):
import json
zip_handle.writestr(
path,
json.dumps(
data,
check_circular=False,
# optional (pretty)
sort_keys=True, indent=4, separators=(',', ': '),
).encode('utf-8'))
def write_json_to_file(path, data):
import json
with open(path, 'w') as file_handle:
json.dump(
data, file_handle, ensure_ascii=False,
check_circular=False,
# optional (pretty)
sort_keys=True, indent=4, separators=(',', ': '),
)