Make connection definition more readable, Pt. II: Create connection dataclass
This commit is contained in:
parent
9ff48eed3b
commit
c93fb74d17
@ -94,10 +94,10 @@ class Harness:
|
|||||||
# prepare ports on connectors depending on which side they will connect
|
# prepare ports on connectors depending on which side they will connect
|
||||||
for k, c in self.cables.items():
|
for k, c in self.cables.items():
|
||||||
for x in c.connections:
|
for x in c.connections:
|
||||||
if x[1] is not None: # connect to left
|
if x.from_port is not None: # connect to left
|
||||||
self.nodes[x[0]].ports_right = True
|
self.nodes[x.from_name].ports_right = True
|
||||||
if x[4] is not None: # connect to right
|
if x.to_port is not None: # connect to right
|
||||||
self.nodes[x[3]].ports_left = True
|
self.nodes[x.to_name].ports_left = True
|
||||||
|
|
||||||
for k, n in self.nodes.items():
|
for k, n in self.nodes.items():
|
||||||
# a = attributes
|
# a = attributes
|
||||||
@ -126,9 +126,9 @@ class Harness:
|
|||||||
loop_dir = 'e'
|
loop_dir = 'e'
|
||||||
else:
|
else:
|
||||||
raise Exception('No side for loops')
|
raise Exception('No side for loops')
|
||||||
for x in n.loops:
|
for loop in n.loops:
|
||||||
dot.edge('{name}:p{port_from}{loop_side}:{loop_dir}'.format(name=n.name, port_from=x[0], port_to=x[1], loop_side=loop_side, loop_dir=loop_dir),
|
dot.edge('{name}:p{port_from}{loop_side}:{loop_dir}'.format(name=n.name, port_from=loop[0], port_to=loop[1], loop_side=loop_side, loop_dir=loop_dir),
|
||||||
'{name}:p{port_to}{loop_side}:{loop_dir}'.format(name=n.name, port_from=x[0], port_to=x[1], loop_side=loop_side, loop_dir=loop_dir))
|
'{name}:p{port_to}{loop_side}:{loop_dir}'.format(name=n.name, port_from=loop[0], port_to=loop[1], loop_side=loop_side, loop_dir=loop_dir))
|
||||||
|
|
||||||
for k, c in self.cables.items():
|
for k, c in self.cables.items():
|
||||||
# a = attributes
|
# a = attributes
|
||||||
@ -179,13 +179,8 @@ class Harness:
|
|||||||
# connections
|
# connections
|
||||||
existing_connections = [] # for bundles, avoid multiple edges between a bundle's wire's start and end node
|
existing_connections = [] # for bundles, avoid multiple edges between a bundle's wire's start and end node
|
||||||
for x in c.connections:
|
for x in c.connections:
|
||||||
from_name = x[0]
|
if isinstance(x.via_port, int): # check if it's an actual wire and not a shield
|
||||||
from_port = x[1]
|
search_color = c.colors[x.via_port-1]
|
||||||
via_port = x[2]
|
|
||||||
to_name = x[3]
|
|
||||||
to_port = x[4]
|
|
||||||
if isinstance(via_port, int): # check if it's an actual wire and not a shield
|
|
||||||
search_color = c.colors[via_port-1]
|
|
||||||
if search_color in color_hex:
|
if search_color in color_hex:
|
||||||
dot.attr('edge',color='#000000:{wire_color}:#000000'.format(wire_color=color_hex[search_color]))
|
dot.attr('edge',color='#000000:{wire_color}:#000000'.format(wire_color=color_hex[search_color]))
|
||||||
else: # color name not found
|
else: # color name not found
|
||||||
@ -194,35 +189,35 @@ class Harness:
|
|||||||
dot.attr('edge',color='#000000')
|
dot.attr('edge',color='#000000')
|
||||||
|
|
||||||
if c.type == 'bundle':
|
if c.type == 'bundle':
|
||||||
labeltext = '{sp}{color}'.format(color=translate_color(c.colors[via_port-1], self.color_mode), sp=' ' * 35)
|
labeltext = '{sp}{color}'.format(color=translate_color(c.colors[x.via_port-1], self.color_mode), sp=' ' * 35)
|
||||||
if via_port not in existing_connections:
|
if x.via_port not in existing_connections:
|
||||||
dot.edge('{via_name}_w{via_wire}l'.format(via_name=c.name, via_wire=via_port),
|
dot.edge('{via_name}_w{via_wire}l'.format(via_name=c.name, via_wire=x.via_port),
|
||||||
'{via_name}_w{via_wire}r'.format(via_name=c.name, via_wire=via_port),
|
'{via_name}_w{via_wire}r'.format(via_name=c.name, via_wire=x.via_port),
|
||||||
taillabel=labeltext,
|
taillabel=labeltext,
|
||||||
labelangle='60',
|
labelangle='60',
|
||||||
labeldist='0')
|
labeldist='0')
|
||||||
existing_connections.append(via_port)
|
existing_connections.append(x.via_port)
|
||||||
|
|
||||||
if from_port is not None: # connect to left
|
if x.from_port is not None: # connect to left
|
||||||
if c.type == 'bundle':
|
if c.type == 'bundle':
|
||||||
dot.edge('{from_name}:p{from_port}r'.format(from_name=from_name,from_port=from_port),
|
dot.edge('{from_name}:p{from_port}r'.format(from_name=x.from_name, from_port=x.from_port),
|
||||||
'{via_name}_w{via_wire}l:w'.format(via_name=c.name, via_wire=via_port),
|
'{via_name}_w{via_wire}l:w'.format(via_name=c.name, via_wire=x.via_port),
|
||||||
headlabel='{}{}:{}'.format(' ' * 12,from_name,from_port),
|
headlabel='{}{}:{}'.format(' ' * 12, x.from_name, x.from_port),
|
||||||
labelangle='-60',
|
labelangle='-60',
|
||||||
labeldist='0')
|
labeldist='0')
|
||||||
else:
|
else:
|
||||||
dot.edge('{from_name}:p{from_port}r'.format(from_name=from_name,from_port=from_port),
|
dot.edge('{from_name}:p{from_port}r'.format(from_name=x.from_name, from_port=x.from_port),
|
||||||
'{via_name}:w{via_wire}{via_subport}'.format(via_name=c.name, via_wire=via_port, via_subport='i' if c.show_pinout else ''))
|
'{via_name}:w{via_wire}{via_subport}'.format(via_name=c.name, via_wire=x.via_port, via_subport='i' if c.show_pinout else ''))
|
||||||
if to_port is not None: # connect to right
|
if x.to_port is not None: # connect to right
|
||||||
if c.type == 'bundle':
|
if c.type == 'bundle':
|
||||||
dot.edge('{via_name}_w{via_wire}r:e'.format(via_name=c.name, via_wire=via_port),
|
dot.edge('{via_name}_w{via_wire}r:e'.format(via_name=c.name, via_wire=x.via_port),
|
||||||
'{to_name}:p{to_port}l'.format(to_name=to_name, to_port=to_port),
|
'{to_name}:p{to_port}l'.format(to_name=x.to_name, to_port=x.to_port),
|
||||||
taillabel='{}:{}{}'.format(to_name,to_port,' ' * 12),
|
taillabel='{}:{}{}'.format(x.to_name, x.to_port,' ' * 12),
|
||||||
labelangle='60',
|
labelangle='60',
|
||||||
labeldist='0')
|
labeldist='0')
|
||||||
else:
|
else:
|
||||||
dot.edge('{via_name}:w{via_wire}{via_subport}'.format(via_name=c.name, via_wire=via_port, via_subport='o' if c.show_pinout else ''),
|
dot.edge('{via_name}:w{via_wire}{via_subport}'.format(via_name=c.name, via_wire=x.via_port, via_subport='o' if c.show_pinout else ''),
|
||||||
'{to_name}:p{to_port}l'.format(to_name=to_name, to_port=to_port))
|
'{to_name}:p{to_port}l'.format(to_name=x.to_name, to_port=x.to_port))
|
||||||
|
|
||||||
return dot
|
return dot
|
||||||
|
|
||||||
@ -309,11 +304,20 @@ class Cable:
|
|||||||
if len(from_pin) != len(to_pin):
|
if len(from_pin) != len(to_pin):
|
||||||
raise Exception('from_pin must have the same number of elements as to_pin')
|
raise Exception('from_pin must have the same number of elements as to_pin')
|
||||||
for i, x in enumerate(from_pin):
|
for i, x in enumerate(from_pin):
|
||||||
self.connections.append((from_name, from_pin[i], via_pin[i], to_name, to_pin[i]))
|
# self.connections.append((from_name, from_pin[i], via_pin[i], to_name, to_pin[i]))
|
||||||
|
self.connections.append(Connection(from_name, from_pin[i], via_pin[i], to_name, to_pin[i]))
|
||||||
|
|
||||||
def connect_all_straight(self, from_name, to_name):
|
def connect_all_straight(self, from_name, to_name):
|
||||||
self.connect(from_name, 'auto', 'auto', to_name, 'auto')
|
self.connect(from_name, 'auto', 'auto', to_name, 'auto')
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class Connection:
|
||||||
|
from_name: Any
|
||||||
|
from_port: Any
|
||||||
|
via_port: Any
|
||||||
|
to_name: Any
|
||||||
|
to_port: Any
|
||||||
|
|
||||||
def nested(input):
|
def nested(input):
|
||||||
l = []
|
l = []
|
||||||
for x in input:
|
for x in input:
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user