-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformat-json
More file actions
executable file
·36 lines (28 loc) · 884 Bytes
/
format-json
File metadata and controls
executable file
·36 lines (28 loc) · 884 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/usr/bin/env python3
r"""Command-line tool to format software release JSON for slapos.
Inspired by json.tool from python, but enforcing 2 spaces and non-sorted keys.
The files are modified in-place.
Usage::
format-json file1.json [file2.json]
"""
from __future__ import print_function
import sys
import json
import collections
def main():
exit_code = 0
for f in sys.argv[1:]:
print('Processing', f,)
with open(f) as infile:
try:
obj = json.load(infile, object_pairs_hook=collections.OrderedDict)
except ValueError as e:
exit_code = 1
print(f'{f}:{e.lineno}', e, file=sys.stderr)
else:
with open(f, 'w') as outfile:
json.dump(obj, outfile, ensure_ascii=False, sort_keys=False, indent=2, separators=(',', ': '))
outfile.write('\n')
sys.exit(exit_code)
if __name__ == '__main__':
main()