-
Notifications
You must be signed in to change notification settings - Fork 0
/
VisualizationParser.py
80 lines (64 loc) · 2.65 KB
/
VisualizationParser.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/bin/python
class VisualizationParser(object):
def __init__(self, file):
self.file = file
self.lines = {}
self.parse()
def addCommand(self, line_number, command):
if line_number not in self.lines:
self.lines[line_number] = [command]
else:
self.lines[line_number].append(command)
def parse(self):
vis_content = ""
with open(self.file, "r") as file:
vis_content = file.read()
line_counter = 0
for line in vis_content.split("\n"):
line_counter = line_counter + 1
#skip empty lines
if len(line) == 0:
continue
#skip comments
if line[0] == "#":
continue
#<linenumber>:<command>:<value>
items = line.split(":")
if len(items) < 3:
sys.stderr.write("line %s: '%s' not recognized, wrong format" % (line_counter, line))
continue
command = items[1]
if command == 'fold':
if len(items) < 5:
sys.stderr.write("line %s: '%s' not recognized, wrong format" % (line_counter, line))
continue
self.addCommand(int(items[0]), {'command': items[1], 'endline': int(items[2]), 'folded': int(items[3]), 'value': ":".join(items[4:])})
#self.lines[ int(items[0]) ] = {'command': items[1], 'endline': int(items[2]), 'folded': int(items[3]), 'value': ":".join(items[4:])}
elif command == 'highlight':
if len(items) < 3:
sys.stderr.write("line %s: '%s' not recognized, wrong format" % (line_counter, line))
continue
self.addCommand(int(items[0]), {'command': items[1], 'keyword': items[2], 'value': items[3:][0]})
#self.lines[ int(items[0]) ] = {'command': items[1], 'keyword': items[2], 'value': items[3:][0]}
elif command.startswith('needinfo', 0, 8):
if len(items) < 3:
sys.stderr.write("line %s: '%s' not recognized, wrong format" % (line_counter, line))
continue
# does the command contains attribues?
attrs = command.split('[')
attrs_db = {}
# comma separated values
if len(attrs) > 1:
attrs = attrs[1][0:-1].split(",")
for attr in attrs:
pair = attr.split("=")
if len(pair) != 2:
continue
attrs_db[ pair[0] ] = pair[1]
self.addCommand( int(items[0]), {'command': 'needinfo', 'keyword': items[2], 'value': items[3:][0], 'attrs': attrs_db } )
#self.lines[ int(items[0]) ] = {'command': 'needinfo', 'keyword': items[2], 'value': items[3:][0], 'attrs': attrs_db }
else:
self.addCommand( int(items[0]), {'command': items[1], 'value': "// " + ":".join(items[2:])} )
#self.lines[ int(items[0]) ] = {'command': items[1], 'value': "// " + ":".join(items[2:])}
def getCommands(self):
return self.lines