-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
27 lines (20 loc) · 778 Bytes
/
main.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
import json
import re
from io import TextIOWrapper
import click
@click.command()
@click.option('-o', '--output_file', type=click.File('w'), help="Output file path, if different from input file")
@click.argument('input_file', type=click.File('r'))
def format_file(input_file: TextIOWrapper, output_file: TextIOWrapper):
with input_file:
s = input_file.read()
s = re.sub("[\r\n\ ]+", " ", s) # collapse to one line
s = re.sub(",[\ ]*}", " }", s) # remove trailing commas
settings_json_dict = json.loads(s)
if output_file is None:
output_file = open(input_file.name, 'w')
with output_file:
output_file.write(json.dumps(
settings_json_dict, indent=4, sort_keys=True))
if __name__ == '__main__':
format_file()