James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1 | #!/usr/bin/python3 |
| 2 | # all arguments to this script are considered as json files |
| 3 | # and attempted to be formatted alphabetically |
| 4 | |
| 5 | import json |
James Feist | 5ffd8b4 | 2019-10-25 11:22:10 -0700 | [diff] [blame] | 6 | import os |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 7 | from sys import argv |
| 8 | |
James Feist | 5ffd8b4 | 2019-10-25 11:22:10 -0700 | [diff] [blame] | 9 | files = argv[1:] |
| 10 | |
| 11 | for 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 | |
| 17 | for file in files: |
Brad Bishop | ca000e5 | 2019-12-19 15:43:06 -0500 | [diff] [blame] | 18 | if not file.endswith('.json'): |
| 19 | continue |
James Feist | c4e5694 | 2019-04-19 12:15:19 -0700 | [diff] [blame] | 20 | print("formatting file {}".format(file)) |
| 21 | with open(file) as f: |
| 22 | j = json.load(f) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 23 | |
James Feist | c4e5694 | 2019-04-19 12:15:19 -0700 | [diff] [blame] | 24 | 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 | |
| 30 | with open(file, 'w') as f: |
| 31 | f.write(json.dumps(j, indent=4, sort_keys=True, separators=(',', ': '))) |
Paul Fertser | 478cfd2 | 2021-04-15 21:24:43 +0300 | [diff] [blame] | 32 | f.write('\n') |