-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathjson-key-check.py
48 lines (40 loc) · 1.54 KB
/
json-key-check.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
39
40
41
42
43
44
45
46
47
48
import argparse
import json
import csv
import sys
import re
def consistency_check(json_a, json_b):
a_set = set(json_a.keys())
b_set = set([v['Key'] for v in json_b])
obsolete_keys = a_set - b_set
if len(obsolete_keys) > 0:
print("=================================")
print("ERROR: obsolete keys detected in copy:")
print("\n".join(list(sorted(obsolete_keys))))
missing_keys = b_set - a_set
if len(missing_keys) > 0:
print("=================================")
print("ERROR: Following keys missing from copy: ")
print("\n".join(list(sorted(missing_keys))))
if (len(missing_keys) or len(obsolete_keys)):
print("=================================")
print("Summary")
print("* Obsolete Keys: {}".format(len(obsolete_keys)))
print("* Missing Keys: {}".format(len(missing_keys)))
else:
print("No error detected")
return
def load_json(in_path):
with open(in_path) as in_file:
return json.load(in_file)
def parse_args():
p = argparse.ArgumentParser(description='translations: KEYVALUESJSON consistency check')
p.add_argument('--json-strings', metavar='PATH', help='path json strings generated from the CSV', required=True)
p.add_argument('--json-code', metavar='PATH', help='path to the JSON keys extracted from the code', required=True)
opt = p.parse_args()
return opt
def main():
opt = parse_args()
consistency_check(load_json(opt.json_strings), load_json(opt.json_code))
if __name__ == "__main__":
main()