From 0839115ff51897a18b59dbb2ad0640e5b71a1ab1 Mon Sep 17 00:00:00 2001 From: Daniel Rojas Date: Sun, 5 Jul 2020 21:36:13 +0200 Subject: [PATCH] Refactor connector GraphViz code generation (#66) --- src/wireviz/Harness.py | 44 +++++++++++++++++++++++++--------------- src/wireviz/wv_helper.py | 18 +++++++++------- 2 files changed, 39 insertions(+), 23 deletions(-) diff --git a/src/wireviz/Harness.py b/src/wireviz/Harness.py index 394640ac..fc103b5f 100644 --- a/src/wireviz/Harness.py +++ b/src/wireviz/Harness.py @@ -66,7 +66,6 @@ def create_graph(self): 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 @@ -76,24 +75,37 @@ def create_graph(self): 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 = [[cable.name if cable.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], + '', + [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'{pinnumber}') - if connector.ports_right: - pinouts[2].append(f'{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'{pinnumber}' if connector.ports_left else None, + f'{pinname}' if pinname else '', + f'{pinnumber}' if connector.ports_right else None]) + + pinhtml = '' + for i, pin in enumerate(pinouts): + pinhtml = f'{pinhtml}' + for column in pin: + if column is not None: + pinhtml = f'{pinhtml}{column}' + pinhtml = f'{pinhtml}' + pinhtml = f'{pinhtml}
' + html = html.replace('', 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') diff --git a/src/wireviz/wv_helper.py b/src/wireviz/wv_helper.py index 83ee46ee..e46ade26 100644 --- a/src/wireviz/wv_helper.py +++ b/src/wireviz/wv_helper.py @@ -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 = '' for row in rows: - if len(row) > 0: - html = f'{html}' + if isinstance(row, List): + if len(row) > 0 and any(row): + html = f'{html}' + else: + html = f'{html}' html = f'{html}
' - for cell in row: - html = f'{html}' - html = f'{html}
{cell}
' + for cell in row: + if cell is not None: + html = f'{html}' + html = f'{html}
{cell}
{row}
' return html