-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpost_processors.py
More file actions
82 lines (59 loc) · 2.23 KB
/
post_processors.py
File metadata and controls
82 lines (59 loc) · 2.23 KB
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
from slearner_helpers import get_sl_tokens
class PostProcessorInterface:
pass
class AsIsPP(PostProcessorInterface):
"""
Returns object as-is, instead of converting to string
"""
def go(self, entry):
return entry
class InterleavedSystemPP(PostProcessorInterface):
def get_block_name(self, block):
for x in block.outputs:
x = x.strip()
tokens = get_sl_tokens(x)
if tokens[0] == 'Name':
return tokens[1]
raise Exception('Unexpected - no name found for block')
def get_block_names_for_line(self, line):
src_blk = None
dst_blks = []
for x in line.outputs:
x = x.strip()
tokens = get_sl_tokens(x)
if tokens[0] == 'SrcBlock':
src_blk = tokens[1]
elif tokens[0] == 'DstBlock':
dst_blks.append(tokens[1])
return src_blk, dst_blks
def get_plain_output(self, entries):
return ''.join(entries)
def go(self, entry):
entries = entry.outputs
final_output = [entries[0]]
blocks = {}
lines = []
for i in entries[1:len(entries) - 1]:
if type(i) == str:
final_output.append(i)
elif i.kw == 'Block':
blocks[self.get_block_name(i)] = i
elif i.kw == 'Line':
lines.append(i)
added_blocks = set()
for line in lines:
src_blk, dst_blks = self.get_block_names_for_line(line)
# if src_blk not in blocks:
# assert False
if src_blk in blocks and src_blk not in added_blocks:
final_output.append(self.get_plain_output(blocks[src_blk].outputs))
added_blocks.add(src_blk)
for dst_blk in dst_blks:
assert dst_blk in blocks
if dst_blk not in blocks or dst_blk in added_blocks:
continue
final_output.append(self.get_plain_output(blocks[dst_blk].outputs))
added_blocks.add(dst_blk)
final_output.append(self.get_plain_output(line.outputs))
final_output.append(entries[-1])
return self.get_plain_output(final_output)