forked from BerkeleyHCI/PolymorphicBlocks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LibraryDump.py
44 lines (36 loc) · 1.56 KB
/
LibraryDump.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
# Simple tool that scans for libraries and dumps the whole thing to a proto file
from typing import cast
import edgir
from edg_core.HdlInterfaceServer import LibraryElementResolver
import edg_core
import edg
from edg_core.Builder import builder
OUTPUT_FILE = "library.edg"
if __name__ == '__main__':
library = LibraryElementResolver()
library.load_module(edg)
pb = edgir.Library()
count = 0
for (name, cls) in library.lib_class_map.items():
obj = cls()
if isinstance(obj, edg_core.Block):
print(f"Elaborating block {name}")
block_proto = builder.elaborate_toplevel(obj, f"in elaborating library block {cls}")
pb.root.members[name].hierarchy_block.CopyFrom(block_proto)
elif isinstance(obj, edg_core.Link):
print(f"Elaborating link {name}")
link_proto = builder.elaborate_toplevel(obj, f"in elaborating library link {cls}")
assert isinstance(link_proto, edgir.Link) # TODO this needs to be cleaned up
pb.root.members[name].link.CopyFrom(link_proto)
elif isinstance(obj, edg_core.Bundle): # TODO: note Bundle extends Port, so this must come first
print(f"Elaborating bundle {name}")
pb.root.members[name].bundle.CopyFrom(obj._def_to_proto())
elif isinstance(obj, edg_core.Port):
print(f"Elaborating port {name}")
pb.root.members[name].port.CopyFrom(cast(edgir.Port, obj._def_to_proto()))
else:
print(f"Unknown category for class {cls}")
count += 1
with open(OUTPUT_FILE, 'wb') as file:
file.write(pb.SerializeToString())
print(f"Wrote {count} classes to {OUTPUT_FILE}")