-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcs.py
executable file
·40 lines (31 loc) · 1017 Bytes
/
cs.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
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/usr/bin/python3
from sys import (
argv
)
from re import (
compile,
MULTILINE
)
non_unix_newlines = compile(b"(\r\n)|(\r)")
trailing_whitespaces = compile(b"[ \t]+\n")
unindented_right_comments = compile(b";(?=\w)")
unindented_left_comments = compile(b"(?<=[\w]);")
def fixup_coding_style(file_name):
with open(file_name, "rb") as f:
original = f.read()
fixed = non_unix_newlines.sub(b"\n", original)
# add new line at and of file
if fixed[-1:] != b"\n":
fixed += b"\n"
fixed = trailing_whitespaces.sub(b"\n", fixed)
# Add space at beginning of word-like commetns.
# Special symbol starting comments like ;------ are ignored.
fixed = unindented_right_comments.sub(b"; ", fixed)
# Add space after word.
fixed = unindented_left_comments.sub(b" ;", fixed)
with open(file_name, "wb") as f:
f.write(fixed)
if __name__ == "__main__":
for fn in argv[1:]:
print("formatting " + fn + " ...")
fixup_coding_style(fn)