-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck-json-serialization.py
executable file
·38 lines (33 loc) · 1.15 KB
/
check-json-serialization.py
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
37
38
#!/usr/bin/env python
"""Identify loader JSON files which have been hand-edited.
We try to store the serialized JSON files used by the loader as the
standard library would print it using 2-space indents. This makes
it easier to get useful output from `git diff` to identify the
real changes. Run this from the directory in which this script lives
(the parent directory of the Loader subdirectory).
"""
import argparse
import json
import glob
FILL = "."
parser = argparse.ArgumentParser()
parser.add_argument("--directory", "-d", default="Loader")
opts = parser.parse_args()
names = []
longest = 0
for name in glob.glob(f"{opts.directory}/*.json"):
longest = max(longest, len(name))
names.append(name)
for name in sorted(names):
with open(name) as fp:
json_string = fp.read().strip()
try:
values = json.loads(json_string)
except Exception:
print(f"{name:{FILL}<{longest+3}}malformed")
continue
else:
if json_string == json.dumps(values, indent=2):
print(f"{name:{FILL}<{longest+3}}ok")
else:
print(f"{name:{FILL}<{longest+3}}hand-edited")