-
Notifications
You must be signed in to change notification settings - Fork 0
/
fixmatlab.py
executable file
·50 lines (45 loc) · 1.58 KB
/
fixmatlab.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
45
46
47
48
49
50
#!/usr/bin/env python
import re, sys
def startswith(line=""):
# these need some word-boundary condition, but \b isn't working
ctrlstart = '\s*(function|if|while|for|switch)'
ctrlcont = '\s*(elseif|else|case|catch|otherwise)'
ctrlend = '\s*(end|endfunction|endif|endwhile|endfor|endswitch)'
match = re.match(ctrlstart, line)
if ( match != None ) :
return ['start', match.group(0)]
match=re.match(ctrlcont, line)
if ( match!=None ) :
return ['cont', match.group(0)]
match=re.match(ctrlend, line)
if ( match!=None ) :
return ['end', match.group(0)]
else :
return [False, None]
def main( filelist = list() ) :
for filename in filelist:
nextindent = 0
indentmult = 2
file = open(filename, 'r')
filelines = file.readlines()
for ind in range(0, len(filelines)) :
indentlevel = nextindent
match = startswith(filelines[ind])
if match[0] == 'start' :
nextindent += 1
elif match[0] == 'cont' :
indentlevel -= 1
elif match[0] == 'end' :
indentlevel -= 1
nextindent -= 1
elif match[0] == False :
nextindent = indentlevel
filelines[ind] = ' '*indentlevel*indentmult + filelines[ind].lstrip().rstrip() +'\n'
file.close()
outfile = open(filename, 'w')
outfile.writelines(filelines)
outfile.close()
args = []
for arg in sys.argv[1:] :
args += [str(arg)]
main(args)