-
Notifications
You must be signed in to change notification settings - Fork 1
/
columnize.py
64 lines (58 loc) · 1.88 KB
/
columnize.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
#!/usr/bin/python3
import sys
delim = ":"
cross_traffic_pattern = []
if len(sys.argv) > 1:
for cross in sys.argv[1].split(","):
tr, name = cross.split("=")
start,end = [int(x)*1000 for x in tr.split(":")]
cross_traffic_pattern.append((start,end,name))
print_head = eval(sys.argv[2])
else:
print_head = True
def flds(line):
for f in line:
sp = f.split(delim)
try:
yield sp[0], sp[1].split(",")[0]
except:
print('line', line, sp, file=sys.stderr)
raise Exception()
head = None
start_time_col = None
duration_col = None
init_time = None
for line in sys.stdin:
sp = line.strip().split()
if head is None:
fields, vals = zip(*flds(sp))
head = fields
for idx,field in enumerate(head):
if field == "StartTime(ms)":
start_time_col = idx
if field == "Duration(usec)":
duration_col = idx
if print_head:
actual_head = head
if cross_traffic_pattern:
actual_head = head + ("start","finish","during",)
print(" ".join(actual_head))
else:
fields, vals = zip(*flds(sp))
if head == fields:
real_start = int(vals[start_time_col])
duration = int(int(vals[duration_col]) / 1000)
if not init_time:
init_time = real_start
start = real_start - init_time
end = start + duration
during = "none"
for (cross_start, cross_end, cross_name) in cross_traffic_pattern:
if start >= cross_start and end <= cross_end:
during = cross_name
break
vals = vals + (str(start),str(end), during)#(during, )
print(" ".join(vals))
else:
sys.stderr.write("non-standard schema")
sys.exit(1)