Refactor connector node generation

This commit is contained in:
Daniel Rojas 2021-10-17 16:12:36 +02:00 committed by KV
parent f7359ff9b1
commit 046a1c2ea6
4 changed files with 52 additions and 79 deletions

View File

@ -169,6 +169,9 @@ class Connector:
def is_autogenerated(self): def is_autogenerated(self):
return self.name.startswith(AUTOGENERATED_PREFIX) return self.name.startswith(AUTOGENERATED_PREFIX)
def should_show_pin(self, pin_name):
return not self.hide_disconnected_pins or self.visible_pins.get(pin_name, False)
def __post_init__(self) -> None: def __post_init__(self) -> None:
if isinstance(self.image, dict): if isinstance(self.image, dict):

View File

@ -35,7 +35,6 @@ from wireviz.wv_colors import get_color_hex, translate_color
from wireviz.wv_gv_html import ( from wireviz.wv_gv_html import (
gv_connector_loops, gv_connector_loops,
gv_node_connector, gv_node_connector,
gv_pin,
html_bgcolor, html_bgcolor,
html_bgcolor_attr, html_bgcolor_attr,
html_caption, html_caption,
@ -177,6 +176,7 @@ class Harness:
dot.attr("edge", style="bold", fontname=self.options.fontname) dot.attr("edge", style="bold", fontname=self.options.fontname)
for connector in self.connectors.values(): for connector in self.connectors.values():
# generate connector node
gv_html = gv_node_connector(connector, self.options) gv_html = gv_node_connector(connector, self.options)
_default_fillcolor = translate_color(self.options.bgcolor_connector, "HEX") _default_fillcolor = translate_color(self.options.bgcolor_connector, "HEX")
dot.node( dot.node(
@ -186,7 +186,7 @@ class Harness:
style="filled", style="filled",
fillcolor=_default_fillcolor, fillcolor=_default_fillcolor,
) )
# generate edges for connector loops
if len(connector.loops) > 0: if len(connector.loops) > 0:
dot.attr("edge", color="#000000:#ffffff:#000000") dot.attr("edge", color="#000000:#ffffff:#000000")
loops = gv_connector_loops(connector) loops = gv_connector_loops(connector)

View File

@ -19,10 +19,10 @@ def gv_node_connector(connector: Connector, harness_options: Options) -> str:
if not (connector.ports_left or connector.ports_right): if not (connector.ports_left or connector.ports_right):
connector.ports_left = True # Use left side pins by default connector.ports_left = True # Use left side pins by default
html = [] # generate all rows to be shown in the node
if connector.show_name: if connector.show_name:
row_name = [ row_name = [
f"{html_bgcolor(connector.bgcolor_title)}" f"{remove_links(connector.name)}" f"{html_bgcolor(connector.bgcolor_title)}{remove_links(connector.name)}"
] ]
else: else:
row_name = [] row_name = []
@ -42,17 +42,33 @@ def gv_node_connector(connector: Connector, harness_options: Options) -> str:
html_colorbar(connector.color), html_colorbar(connector.color),
] ]
if connector.style != "simple":
row_connector_table = "<!-- connector table -->"
else:
row_connector_table = None
row_image = [html_image(connector.image)] row_image = [html_image(connector.image)]
row_image_caption = [html_caption(connector.image)] row_image_caption = [html_caption(connector.image)]
row_notes = [html_line_breaks(connector.notes)] row_notes = [html_line_breaks(connector.notes)]
# row_additional_component_table = get_additional_component_table(self, connector) # row_additional_component_table = get_additional_component_table(self, connector)
row_additional_component_table = None row_additional_component_table = None
if connector.style != "simple":
pin_tuples = zip_longest(
connector.pins,
connector.pinlabels,
connector.pincolors,
)
pin_rows = []
for pinindex, (pinname, pinlabel, pincolor) in enumerate(pin_tuples):
if connector.should_show_pin(pinname):
pin_rows.append(
gv_pin_row(pinindex, pinname, pinlabel, pincolor, connector)
)
table_attribs = Attribs(
{"border": 0, "cellspacing": 0, "cellpadding": 3, "cellborder": 1}
)
row_connector_table = str(Table(pin_rows, attribs=table_attribs))
else:
row_connector_table = None
rows = [ rows = [
row_name, row_name,
row_pn, row_pn,
@ -64,75 +80,26 @@ def gv_node_connector(connector: Connector, harness_options: Options) -> str:
row_notes, row_notes,
] ]
html.extend(nested_html_table(rows, html_bgcolor_attr(connector.bgcolor))) html = "\n".join(nested_html_table(rows, html_bgcolor_attr(connector.bgcolor)))
if connector.style != "simple":
pinhtml = []
# fmt: off
# pinhtml.append('<table border="0" cellspacing="0" cellpadding="3" cellborder="1">')
# fmt: on
pin_tuples = zip_longest(
connector.pins,
connector.pinlabels,
connector.pincolors,
)
contents = []
for pinindex, (pinname, pinlabel, pincolor) in enumerate(pin_tuples):
if connector.hide_disconnected_pins and not connector.visible_pins.get(
pinname, False
):
continue
contents.append(gv_pin(pinindex, pinname, pinlabel, pincolor, connector))
table_attribs = {
"border": 0,
"cellspacing": 0,
"cellpadding": 3,
"cellborder": 1,
}
pinhtml.append(str(Table(contents, attribs=Attribs(table_attribs))))
pin_html_joined = "\n".join(pinhtml)
html = [
row.replace("<!-- connector table -->", pin_html_joined) for row in html
]
html = "\n".join(html)
return html return html
def gv_pin(pinindex, pinname, pinlabel, pincolor, connector): def gv_pin_row(pin_index, pin_name, pin_label, pin_color, connector):
pinhtml = [] cell_pin_left = Td(
pinhtml.append(" <tr>") pin_name, attribs=Attribs({"port": f"p{pin_index+1}l"}), flat=True
if connector.ports_left: )
pinhtml.append(f' <td port="p{pinindex+1}l">{pinname}</td>') cell_pin_label = Td(pin_label, flat=True)
if pinlabel: cell_pin_right = Td(
pinhtml.append(f" <td>{pinlabel}</td>") pin_name, attribs=Attribs({"port": f"p{pin_index+1}r"}), flat=True
if connector.pincolors: )
if pincolor in wv_colors._color_hex.keys():
# fmt: off
pinhtml.append(f' <td sides="tbl">{translate_color(pincolor, harness_options.color_mode)}</td>')
pinhtml.append( ' <td sides="tbr">')
pinhtml.append( ' <table border="0" cellborder="1"><tr>')
pinhtml.append(f' <td bgcolor="{wv_colors.translate_color(pincolor, "HEX")}" width="8" height="8" fixedsize="true"></td>')
pinhtml.append( ' </tr></table>')
pinhtml.append( ' </td>')
# fmt: on
else:
pinhtml.append(' <td colspan="2"></td>')
if connector.ports_right: cells = [
pinhtml.append(f' <td port="p{pinindex+1}r">{pinname}</td>') cell_pin_left if connector.ports_left else None,
pinhtml.append(" </tr>") cell_pin_label,
cell_pin_right if connector.ports_right else None,
pinhtml = "\n".join(pinhtml) ]
return Tr(cells)
return pinhtml
def gv_connector_loops(connector: Connector) -> List: def gv_connector_loops(connector: Connector) -> List:

View File

@ -1,8 +1,8 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from collections.abc import Iterable
from dataclasses import dataclass, field from dataclasses import dataclass, field
from typing import Dict, List, Optional from typing import Dict, List, Optional
from collections.abc import Iterable
class Attribs(Dict): class Attribs(Dict):
@ -23,21 +23,24 @@ class Attribs(Dict):
class Tag: class Tag:
contents: str contents: str
attribs: Attribs = field(default_factory=Attribs) attribs: Attribs = field(default_factory=Attribs)
one_line: bool = False flat: bool = False
@property @property
def tagname(self): def tagname(self):
return type(self).__name__.lower() return type(self).__name__.lower()
def get_contents(self): def get_contents(self):
if isinstance(self.contents, Iterable): # import pudb; pudb.set_trace()
return "\n".join([str(c) for c in self.contents]) separator = "" if self.flat else "\n"
if isinstance(self.contents, Iterable) and not isinstance(self.contents, str):
return separator.join([str(c) for c in self.contents if c is not None])
elif self.contents is None:
return ""
else: else:
return str(self.contents) return str(self.contents)
def __repr__(self): def __repr__(self):
separator = "" if self.one_line else "\n" separator = "" if self.flat else "\n"
html = [ html = [
f"<{self.tagname}{str(self.attribs)}>", f"<{self.tagname}{str(self.attribs)}>",
self.get_contents(), self.get_contents(),