-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbeginend.py
47 lines (36 loc) · 829 Bytes
/
beginend.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
41
42
43
44
# To run:
# python beginend.py <filename>
# Outputs to stdout
import sys
if len(sys.argv) != 2:
print("Usage: python beginend.py <filename>")
sys.exit()
f = None
try:
f = open(sys.argv[1], "r")
except OSError:
print("File does not exist or could not be opened.")
sys.exit()
lines = []
order = [0]
for l in f:
ls = l.strip()
if ls == "begin":
order[-1] += 1
label = " // "
for n in order:
label += str(n) + "."
label = label[:-1]
l = l.rstrip() + label + "\n"
order.append(0)
elif ls == "end":
order.pop()
label = " // "
for n in order:
label += str(n) + "."
label = label[:-1]
l = l.rstrip() + label + "\n"
lines.append(l)
f.close()
for l in lines:
print(l, end='')