Skip to content

Commit

Permalink
Refactor connector GraphViz code generation (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
formatc1702 committed Jul 15, 2020
1 parent 12d3002 commit 7e54c7a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 23 deletions.
44 changes: 28 additions & 16 deletions src/wireviz/Harness.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ def create_graph(self) -> Graph:
f'IPN: {connector.internal_part_number}' if connector.internal_part_number else None],
[html_line_breaks(connector.notes)]]
rows = [list(filter(None, row)) for row in rows] # remove missing attributes

html = nested_html_table(rows)

if connector.color: # add color bar next to color info, if present
Expand All @@ -102,24 +101,37 @@ def create_graph(self) -> Graph:
dot.node(key, label=f'<{html}>', shape='none', margin='0', style='filled', fillcolor='white')

else: # not a ferrule
identification = [connector.manufacturer,
f'MPN: {connector.manufacturer_part_number}' if connector.manufacturer_part_number else '',
f'IPN: {connector.internal_part_number}' if connector.internal_part_number else '']

attributes = [graphviz_line_breaks(connector.type),
graphviz_line_breaks(connector.subtype),
f'{connector.pincount}-pin' if connector.show_pincount else'']
pinouts = [[], [], []]

rows = [[connector.name if connector.show_name else None],
[html_line_breaks(connector.type),
html_line_breaks(connector.subtype),
f'{connector.pincount}-pin' if connector.show_pincount else None],
[connector.manufacturer,
f'MPN: {connector.manufacturer_part_number}' if connector.manufacturer_part_number else None,
f'IPN: {connector.internal_part_number}' if connector.internal_part_number else None],
'<!-- connector table -->',
[html_line_breaks(connector.notes)]]
html = nested_html_table(rows)

pinouts = []
for pinnumber, pinname in zip(connector.pinnumbers, connector.pinout):
if connector.hide_disconnected_pins and not connector.visible_pins.get(pinnumber, False):
continue
pinouts[1].append(pinname)
if connector.ports_left:
pinouts[0].append(f'<p{pinnumber}l>{pinnumber}')
if connector.ports_right:
pinouts[2].append(f'<p{pinnumber}r>{pinnumber}')
label = [connector.name if connector.show_name else '', identification, attributes, pinouts, graphviz_line_breaks(connector.notes)]
dot.node(key, label=nested(label))
pinouts.append([f'<td port="p{pinnumber}l">{pinnumber}</td>' if connector.ports_left else None,
f'<td>{pinname}</td>' if pinname else '',
f'<td port="p{pinnumber}r">{pinnumber}</td>' if connector.ports_right else None])

pinhtml = '<table border="0" cellspacing="0" cellpadding="3" cellborder="1">'
for i, pin in enumerate(pinouts):
pinhtml = f'{pinhtml}<tr>'
for column in pin:
if column is not None:
pinhtml = f'{pinhtml}{column}'
pinhtml = f'{pinhtml}</tr>'
pinhtml = f'{pinhtml}</table>'
html = html.replace('<!-- connector table -->', pinhtml)

dot.node(key, label=f'<{html}>', shape='none', margin='0', style='filled', fillcolor='white')

if len(connector.loops) > 0:
dot.attr('edge', color='#000000:#ffffff:#000000')
Expand Down
18 changes: 11 additions & 7 deletions src/wireviz/wv_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,20 @@ def nested(inp):
return '|'.join(l)

def nested_html_table(rows):
# input: list of lists
# output: a parent table with one child table per parent list item
# input: list, each item may be scalar or list
# output: a parent table with one child table per parent item that is list, and one cell per parent item that is scalar
# purpose: create the appearance of one table, where cell widths are independent between rows
html = '<table border="0" cellspacing="0" cellpadding="0">'
for row in rows:
if len(row) > 0:
html = f'{html}<tr><td><table border="0" cellspacing="0" cellpadding="3" cellborder="1"><tr>'
for cell in row:
html = f'{html}<td balign="left">{cell}</td>'
html = f'{html}</tr></table></td></tr>'
if isinstance(row, List):
if len(row) > 0 and any(row):
html = f'{html}<tr><td><table border="0" cellspacing="0" cellpadding="3" cellborder="1"><tr>'
for cell in row:
if cell is not None:
html = f'{html}<td balign="left">{cell}</td>'
html = f'{html}</tr></table></td></tr>'
else:
html = f'{html}<tr><td>{row}</td></tr>'
html = f'{html}</table>'
return html

Expand Down

0 comments on commit 7e54c7a

Please sign in to comment.