blob: 4a5fc5cf91205b3be50634fa240ca0e28d2a78e1 [file] [log] [blame]
James Feist3cb5fec2018-01-23 14:41:51 -08001#!/usr/bin/python3
2# all arguments to this script are considered as json files
3# and attempted to be formatted alphabetically
4
5import json
James Feist5ffd8b42019-10-25 11:22:10 -07006import os
James Feist3cb5fec2018-01-23 14:41:51 -08007from sys import argv
8
James Feist5ffd8b42019-10-25 11:22:10 -07009files = argv[1:]
10
11for file in files[:]:
12 if os.path.isdir(file):
13 files.remove(file)
14 for f in os.listdir(file):
15 files.append(os.path.join(file, f))
16
17for file in files:
Brad Bishopca000e52019-12-19 15:43:06 -050018 if not file.endswith('.json'):
19 continue
James Feistc4e56942019-04-19 12:15:19 -070020 print("formatting file {}".format(file))
21 with open(file) as f:
22 j = json.load(f)
James Feist3cb5fec2018-01-23 14:41:51 -080023
James Feistc4e56942019-04-19 12:15:19 -070024 if isinstance(j, list):
25 for item in j:
26 item["Exposes"] = sorted(item["Exposes"], key=lambda k: k["Type"])
27 else:
28 j["Exposes"] = sorted(j["Exposes"], key=lambda k: k["Type"])
29
John Wang086f19d2021-02-28 14:54:08 +080030 nl = [s['Name'] for s in j['Exposes']]
31 ns = set(nl)
32 for n in ns:
33 t = nl.count(n)
34 if t != 1:
35 print('\033[1;35mthe %s appears %d times\033[0m!'%(n, t))
36 os._exit(-1)
37
James Feistc4e56942019-04-19 12:15:19 -070038 with open(file, 'w') as f:
39 f.write(json.dumps(j, indent=4, sort_keys=True, separators=(',', ': ')))