-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmake_sequence.py
executable file
·190 lines (173 loc) · 6.61 KB
/
make_sequence.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import svgwrite
import os.path
import sys
class Colors:
black = '#000000'
gray = '#C0C0C0'
class Phase:
def __init__(self, name, color, action0):
self.name = name
self.color = color
self.action0 = action0
self.action1 = None
class Sequence:
def __init__(self, filename):
self.filename = filename
self.actors = []
self.actors_map = {}
self.actions = []
self.phases = []
# read actions into file
self.parse_input_file(filename)
# create drawing
self.top_left = (100, 50)
self.step_size = (100, 25)
self.text_fudge = (0, 5)
self.width = self.top_left[0] + (len(self.actors) + 1)*self.step_size[0]
self.height = self.top_left[1] + (len(self.actions) + 1)*self.step_size[1]
self.drawing = svgwrite.Drawing(size=(self.width, self.height))
self.markers = {}
def parse_input_file(self, filename):
# read actions into file
open_phases = []
num_lines_processed = 0
with open(filename) as f:
for i, line in enumerate(f):
line = line.strip()
if len(line) == 0:
# skip empty lines
continue
if line[0] == '#':
# skip comments
continue
if line.startswith('@phase'):
tokens = map(lambda s : s.strip(), line[len('@phase'):].split(','))
assert len(tokens) > 0, '@phase must contain at least a name'
phase_name = tokens[0]
assert len(phase_name) > 0, '@phase must contain at least a name'
phase_color = tokens[1] if len(tokens) > 1 else Colors.gray
p = Phase(phase_name, phase_color, len(self.actions))
open_phases.append(p)
elif line.startswith('@endphase'):
assert len(open_phases) > 0, '@endphase found with no corresponding opening @phase'
p = open_phases.pop()
p.action1 = len(self.actions)
self.phases.append(p)
elif line.startswith('@order'):
assert num_lines_processed==0, '@order may only be on the first line!'
tokens = map(lambda s : s.strip(), line[len('@order'):].split(','))
for t in tokens:
key = len(self.actors)
self.actors_map[t] = key
self.actors.append(t)
else:
self.parse_step(line)
num_lines_processed += 1
assert len(open_phases) == 0, '@phase opened without corresponding closing @endphase'
def parse_step(self, line):
tokens = map(lambda s : s.strip(), line.split(','))
assert len(tokens) >= 3, 'line %i has less than 3 tokens' % (i)
if len(tokens) < 3:
return
actor0 = tokens[0]
actor1 = tokens[1]
action = tokens[2]
if actor0 not in self.actors_map:
key = len(self.actors)
self.actors_map[actor0] = key
self.actors.append(actor0)
if actor1 not in self.actors_map:
key = len(self.actors)
self.actors_map[actor1] = key
self.actors.append(actor1)
key0 = self.actors_map[actor0]
key1 = self.actors_map[actor1]
color = Colors.black
if len(tokens) > 3:
color = tokens[3].strip()
self.actions.append((key0, key1, action, color))
def build(self):
self.create_header()
self.create_actors()
self.create_phases()
self.create_actions()
pass
def to_string(self):
return self.drawing.tostring()
def create_header(self):
text = 'filename: %s' % self.filename
self.drawing.add(self.drawing.text(text, insert=(self.top_left[0] - 0.5*self.step_size[0], 0.5*self.top_left[1]), stroke='none', fill=Colors.gray, font_family="Helevetica", font_size="8pt", text_anchor="start"))
def create_phases(self):
for phase in self.phases:
action0 = self.actions[phase.action0]
action1 = self.actions[phase.action1 - 1]
left = len(self.actors) + 1
right = -1
for action in self.actions[phase.action0:phase.action1]:
mn = min(action[0:2])
mx = max(action[0:2])
left = mn if mn < left else left
right = mx if mx > right else right
x = self.top_left[0] + (left - 0.5)*self.step_size[0]
y = self.top_left[1] + (phase.action0 + 0.5)*self.step_size[1]
w = (right - left + 1)*self.step_size[0]
h = (phase.action1 - phase.action0 - 0.125)*self.step_size[1]
filled_rect = self.drawing.add(self.drawing.rect((x,y), (w,h)))
filled_rect.fill(phase.color, None, 0.15)
self.drawing.add(self.drawing.rect((x,y), (w,h), stroke=phase.color, stroke_width=1, fill='none'))
transform = 'rotate(180,%i,%i) translate(6,0)' % (x, y+0.5*h)
self.drawing.add(self.drawing.text(phase.name, insert=(x, y+0.5*h), stroke='none', fill=phase.color, font_family="Helevetica", font_size="6pt", text_anchor="middle", writing_mode="tb", transform=transform))
def create_actors(self):
x = self.top_left[0]
y = self.top_left[1]
for name in self.actors:
self.drawing.add(self.drawing.text(name, insert=(x, y - self.text_fudge[1]), stroke='none', fill=Colors.black, font_family="Helevetica", font_size="8pt", text_anchor="middle"))
line = self.drawing.add(self.drawing.line((x, y), (x, self.height), stroke=Colors.gray, stroke_width=1))
line.dasharray([5, 5])
x += self.step_size[0]
def create_actions(self):
markers = {}
# add actions
y = self.top_left[1] + self.step_size[1]
for (a0, a1, act, color) in self.actions:
x0 = self.top_left[0] + a0*self.step_size[0]
x1 = self.top_left[0] + a1*self.step_size[0]
if a0 == a1:
self.drawing.add(self.drawing.circle((x0, y), r=2, fill=color))
else:
start_marker, end_marker = self.get_markers(color)
assert start_marker is not None
assert end_marker is not None
line = self.drawing.add(self.drawing.line((x0, y), (x1, y), stroke=color, stroke_width=1))
line['marker-start'] = start_marker.get_funciri()
line['marker-end'] = end_marker.get_funciri()
self.drawing.add(self.drawing.text(act, insert=(0.5*(x0 + x1), y - self.text_fudge[1]), stroke='none', fill=color, font_family="Helevetica", font_size="6pt", text_anchor="middle"))
y += self.step_size[1]
def get_markers(self, color):
# create or get marker objects
start_marker, end_marker = None, None
if color in self.markers:
start_marker, end_marker = self.markers[color]
else:
start_marker = self.drawing.marker(insert=(2,2), size=(10,10), orient='auto')
start_marker.add(self.drawing.circle((2,2), r=2, fill=color))
self.drawing.defs.add(start_marker)
end_marker = self.drawing.marker(insert=(6,3), size=(10,10), orient='auto')
end_marker.add(self.drawing.path("M0,0 L0,7 L6,3 L0,0", fill=color))
self.drawing.defs.add(end_marker)
self.markers[color] = (start_marker, end_marker)
return start_marker, end_marker
def usage():
print 'Usage: ./make_sequence.py <in filename> > <out filename>'
sys.exit(-1)
if __name__ == '__main__':
if len(sys.argv) < 2:
print 'missing input filename'
usage()
filename = sys.argv[1]
if not os.path.isfile(filename):
print 'file %s not found' % filename
sys.exit(-1)
seq = Sequence(sys.argv[1])
seq.build()
print seq.to_string()