This repository has been archived by the owner on Jan 19, 2022. It is now read-only.
/
pianoparser.py
65 lines (58 loc) · 1.8 KB
/
pianoparser.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import re
def parse_file( f ):
lines = f.readlines()
f.close()
lines = cleanup( lines )
code = "".join( lines ).split( ";" )
n = 0
p = []
s = []
for i in code:
m = re.search( "_key_(\w)", i )
if m:
if m.group(1) == 'n':
n = int( re.search( "_key_n = (\d*)", i ).group(1) )
elif m.group(1) == 'p':
a = re.search( "\{(.*)\}", i ).group(1)
p = a.split( "," )
if p[-1] == "": p = p[:-1]
p = [ long( x, 0 ) for x in p ]
elif m.group(1) == 's':
a = re.search( "\{\{(.*)\}\}", i ).group(1)
b = re.split( r"},\s*{", a )
for c in b:
d = c.split( "," )
if d[-1] == "": d = d[:-1]
d = [ long( x, 0 ) for x in d ]
s.append( d )
return ( n, p, s )
#Takes in a collection of lines.
#removes all surrounding whitespace, comments and empty lines.
def cleanup( lines ):
clean = []
comment = False
for line in lines:
( line, comment ) = clean_line( line, comment )
if line == "": continue
if line[0] == '#': continue
clean.append( line )
return clean
def clean_line( line, comment ):
line = line.strip()
if comment:
if "*/" in line:
line = line.split( "*/", 1 )[1]
( line, comment ) = clean_line( line, False )
else:
line = ""
elif "//" in line:
line = line.split( "//" )[0]
( line, comment ) = clean_line( line, False )
elif "/*" in line:
if "*/" in line:
line = line.split( "/*" )[0] + line.split( "*/", 1 )[1]
( line, comment ) = clean_line( line, False )
else:
line = line.split( "/*" )[0]
comment = True
return ( line, comment )