|
1 | 1 | #!/usr/bin/env python3 |
2 | 2 |
|
3 | | -"""Clean JSON file. |
| 3 | +""" |
| 4 | +Clean JSON files. |
4 | 5 |
|
5 | | -Cleans JSON file so as to fix indentation, order fields, and remove trailing |
6 | | -whitespace. |
| 6 | +Clean one or more JSON files so as to fix indentation, order fields, and remove |
| 7 | +trailing whitespace. |
7 | 8 | """ |
8 | 9 |
|
9 | 10 | import json |
|
13 | 14 |
|
14 | 15 |
|
15 | 16 | @click.command() |
16 | | -@click.argument("filename", type=click.Path(exists=True)) |
| 17 | +@click.argument( |
| 18 | + "filenames", metavar="[FILENAME] ...", type=click.Path(exists=True), nargs=-1 |
| 19 | +) |
17 | 20 | @click.option( |
18 | 21 | "--check", |
19 | 22 | is_flag=True, |
20 | 23 | default=False, |
21 | 24 | help="Don't write the file back, only check whether the file is well formatted.", |
22 | 25 | ) |
23 | | -def clean_json_file(filename, check): # noqa: D301 |
24 | | - """Clean JSON file. |
25 | | -
|
26 | | - Clean JSON file so as to fix indentation, order fields, and remove trailing |
27 | | - whitespace. |
| 26 | +def clean_json_file(filenames, check): # noqa: D301 |
| 27 | + """ |
| 28 | + Clean JSON files. |
28 | 29 |
|
29 | | - \b |
30 | | - :param filename: The file name to be reformatted. |
31 | | - :param check: Do not modify file, only check whether it is well formatted. |
32 | | - :type filename: str |
33 | | - :type check: bool |
| 30 | + Clean one or more JSON files so as to fix indentation, order fields, and |
| 31 | + remove trailing whitespace. |
34 | 32 | """ |
35 | | - with open(filename, "r") as fdesc: |
36 | | - old_content = fdesc.read() |
37 | | - records = json.loads(old_content) |
38 | | - new_content = ( |
39 | | - json.dumps( |
40 | | - records, |
41 | | - indent=2, |
42 | | - sort_keys=True, |
43 | | - ensure_ascii=False, |
44 | | - separators=(",", ": "), |
| 33 | + for filename in filenames: |
| 34 | + with open(filename, "r") as fdesc: |
| 35 | + old_content = fdesc.read() |
| 36 | + records = json.loads(old_content) |
| 37 | + new_content = ( |
| 38 | + json.dumps( |
| 39 | + records, |
| 40 | + indent=2, |
| 41 | + sort_keys=True, |
| 42 | + ensure_ascii=False, |
| 43 | + separators=(",", ": "), |
| 44 | + ) |
| 45 | + + "\n" |
45 | 46 | ) |
46 | | - + "\n" |
47 | | - ) |
48 | | - |
49 | | - if old_content != new_content: |
50 | | - if check: |
51 | | - print(f"[ERROR] File {filename} is badly formatted.") |
52 | | - sys.exit(1) |
53 | | - else: |
54 | | - with open(filename, "w") as fdesc: |
55 | | - fdesc.write(new_content) |
| 47 | + |
| 48 | + if old_content != new_content: |
| 49 | + if check: |
| 50 | + print(f"[ERROR] File {filename} is badly formatted.") |
| 51 | + sys.exit(1) |
| 52 | + else: |
| 53 | + with open(filename, "w") as fdesc: |
| 54 | + fdesc.write(new_content) |
56 | 55 |
|
57 | 56 |
|
58 | 57 | if __name__ == "__main__": |
|
0 commit comments