-
Notifications
You must be signed in to change notification settings - Fork 9
/
serial.py
104 lines (75 loc) · 2.45 KB
/
serial.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
from diagram import Schematic, Element
import json
from bidict import bidict
from version import VERSION
from core.descriptors import ExposedPin, Gate, Not, Composite
_DESC_TO_NAME = bidict({
Not: 'not',
Gate: 'gate',
Composite: 'schematic',
ExposedPin: 'exposed_pin'
})
_OP_TO_NAME = bidict({
Gate.AND: 'and',
Gate.OR: 'or',
Gate.XOR: 'xor'
})
def _desc_to_dict(desc):
d = dict()
d['type'] = _DESC_TO_NAME[type(desc)]
if isinstance(desc, Not):
d['width'] = desc.width
elif isinstance(desc, Gate):
d['num_inputs'] = desc.num_inputs
d['width'] = desc.width
d['negated'] = desc.negated
d['op'] = _OP_TO_NAME[desc.op]
elif isinstance(desc, Composite):
d['children'] = dict(map(lambda t: (
t[0], _desc_to_dict(t[1])), desc.children.items()))
d['connections'] = list(map(list, desc.connections))
elif isinstance(desc, ExposedPin):
d['width'] = desc.width
d['direction'] = 'in' if desc.direction == ExposedPin.IN else 'out'
return d
def _dict_to_desc(d):
type = _DESC_TO_NAME.inverse[d['type']]
if type is Not:
return Not(d['width'])
elif type is Gate:
return Gate(_OP_TO_NAME.inverse[d['op']], d['width'], d['num_inputs'], d['negated'])
elif type is Composite:
s = Composite()
for name, child in d['children'].items():
s.add_child(name, _dict_to_desc(child))
for conn in d['connections']:
s.connect(*conn)
return s
elif type is ExposedPin:
return ExposedPin(ExposedPin.IN if d['direction'] == 'in' else ExposedPin.OUT, d['width'])
def _dict_to_element(d):
name = d['name']
descriptor = _dict_to_desc(d['descriptor'])
position = tuple(d['position'])
facing = d['facing']
return Element(name, descriptor, qAddPostRoutine, facing)
def _element_to_dict(element: Element):
d = dict()
d['name'] = element.name
d['descriptor'] = _desc_to_dict(element.descriptor)
d['position'] = list(element.position)
d['facing'] = element.facing
return d
def load_project(obj):
d = json.load(obj)
# version = d['version']
root = _dict_to_desc(d['root'])
return root
def save_project(obj, diagrams):
d = dict()
d['version'] = VERSION
dlist = d['diagrams'] = list()
for diagram in diagrams:
d['schematic'] = _desc_to_dict()
d['connections'] = pass
json.dump(d, obj, indent=4)