-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformat-pernosco-variable.py
More file actions
executable file
·59 lines (51 loc) · 1.44 KB
/
format-pernosco-variable.py
File metadata and controls
executable file
·59 lines (51 loc) · 1.44 KB
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
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env python3
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
# Author: Manuel Bucher <dev@manuelbucher.com>
# Date: 2025-02-26
import sys
def main():
file = open(sys.argv[1]).read()
out = ""
whitespace = False
is_escape_char = False
is_str_literal = False
indent = 0
for c in file:
# skip over string literals
if is_str_literal:
out += c
if is_escape_char:
is_escape_char = False
elif c == '\\':
is_escape_char = True
elif c == '"':
is_str_literal = False
continue
# normalize spaces
if c.strip() == '':
if whitespace == False:
out += " "
whitespace = True
continue
whitespace = False
if c == '{':
indent += 1
whitespace = True
out += '{\n' + ' ' * indent
continue
if c == '}':
indent -= 1
out += '\n' + ' ' * indent + '}'
continue
if c == ',':
whitespace = True
out += ',\n' + ' ' * indent
continue
if c == '"':
is_str_literal = True
out += c
print(out)
if __name__ == '__main__':
main()