-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprettify.py
More file actions
54 lines (44 loc) · 1.01 KB
/
prettify.py
File metadata and controls
54 lines (44 loc) · 1.01 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
import sys
if len(sys.argv) < 2:
print 'to use: ' + sys.argv[0] + ' <filename>'
sys.exit(1)
else:
file = sys.argv[1]
t = "\t"
def tabulate(n):
if n > 0:
for i in range(0, n):
f.write(t)
def newline(n):
f.write("\n")
tabulate(n)
f = open(file, "r+")
line = f.read()
tabs = 0
contained = 0 #0 not contained, 1 is contained --> [...]
linestart = 1 #1 is start of a new line, 0 is not
count = 0
f.seek(0)
for i in line:
if i == '{':
linestart = 1
tabs += 1
elif count+1 != line.__len__() and line[count+1] == '}':
linestart = 1
tabs -= 1
elif contained == 0 and i == ',':
linestart = 1
else:
linestart = 0
if count+1 != line.__len__() and i == '[' and line[count+1] == '{':
contained = 0
elif i == '[':
contained = 1
elif i == ']':
contained = 0
f.write(i)
if linestart == 1 and contained == 0:
newline(tabs)
count += 1
f.close()
print 'All done. Exiting now...'