# -*- coding: utf-8 -*- import re from typing import Any, List, Optional, Union from wireviz import APP_NAME, APP_URL, __version__ from wireviz.wv_bom import partnumbers2list from wireviz.wv_colors import MultiColor, SingleColor from wireviz.wv_dataclasses import ( ArrowDirection, ArrowWeight, Cable, Component, Connector, MateComponent, MatePin, ShieldClass, WireClass, ) from wireviz.wv_html import Img, Table, Td, Tr from wireviz.wv_utils import html_line_breaks, remove_links from wireviz.wv_templates import get_template def gv_pin_table(component) -> Table: pin_rows = [] for pin in component.pin_objects.values(): if component.should_show_pin(pin.id): pin_rows.append(gv_pin_row(pin, component)) if len(pin_rows) == 0: # TODO: write test for empty pin tables, and for unconnected connectors that hide disconnected pins pass tbl = Table(pin_rows, border=0, cellborder=1, cellpadding=3, cellspacing=0) return tbl def gv_pin_row(pin, connector) -> Tr: # ports in GraphViz are 1-indexed for more natural maping to pin/wire numbers has_pincolors = any([_pin.color for _pin in connector.pin_objects.values()]) cells = [ Td(pin.id, port=f"p{pin.index+1}l") if connector.ports_left else None, Td(pin.label, delete_if_empty=True), Td(str(pin.color) if pin.color else "", sides="TBL") if has_pincolors else None, Td(color_minitable(pin.color), sides="TBR") if has_pincolors else None, Td(pin.id, port=f"p{pin.index+1}r") if connector.ports_right else None, ] return Tr(cells) def gv_node_connector(connector: Connector) -> Table: pins = [] use_left = bool(connector.ports_left) use_right = bool(connector.ports_right) has_pincolors = any([_pin.color for _pin in connector.pin_objects.values()]) for pin in connector.pin_objects.values(): if not connector.should_show_pin(pin.id): continue color = str(pin.color) if pin.color else "" if has_pincolors else None pins.append({ 'id': pin.id, 'index': pin.index, 'label': pin.label, 'color': color, 'color_len': len(pin.color), 'has_pincolors': has_pincolors, }) columns = 2 + (1 if use_left else 0) + (1 if use_right else 0) params = { 'designator': f"{remove_links(connector.designator)}", 'use_left': use_left, 'use_right': use_right, 'pins': pins, 'columns': columns, 'bom_id': connector.bom_entry.id, # TODO: support asdict(connector) 'type': html_line_breaks(connector.type), 'subtype': html_line_breaks(connector.subtype), 'pincount': connector.pincount, 'show_pincount': connector.show_pincount, 'color': connector.color, 'color_len': len(connector.color), } # TODO: extend connector style support is_simple_connector = connector.style == 'simple' template_name = "connector.html" if is_simple_connector: template_name = "simple-connector.html" rendered = get_template(template_name).render(params) cleaned_render = '\n'.join([l.rstrip() for l in rendered.split('\n') if l.strip()]) return cleaned_render ## If no wires connected (except maybe loop wires)? #if isinstance(connector, Connector): # if not (connector.ports_left or connector.ports_right): # connector.ports_left = True # Use left side pins by default ## generate all rows to be shown in the node #if connector.show_name: # str_name = f"{remove_links(connector.designator)}" # line_name = Td(str_name, bgcolor=connector.bgcolor_title.html) #else: # line_name = None #line_pn = partnumbers2list(connector.partnumbers) #is_simple_connector = connector.style == 'simple' #line_info = [ # html_line_breaks(connector.type), # html_line_breaks(connector.subtype), # f"{connector.pincount}-pin" if connector.show_pincount else None, # str(connector.color) if connector.color else None, #] #if connector.color: # line_info.extend(colorbar_cells(connector.color)) #line_image, line_image_caption = image_and_caption_cells(connector) ##line_additional_connector_table = gv_additional_component_table(connector) #line_notes = [html_line_breaks(connector.notes)] #if connector.style != "simple": # line_ports = gv_pin_table(connector) #else: # line_ports = None #lines = [ # line_name, # line_pn, # line_info, # line_ports, # line_image, # line_image_caption, # line_additional_connector_table, # line_notes, #] #tbl = nested_table(lines) #if is_simple_connector: # # Simple connectors have no pin table, and therefore, no ports to attach wires to. # # Manually assign left and right ports here if required. # # Use table itself for right port, and the first cell for left port. # # Even if the table only has one cell, two separate ports can still be assigned. # tbl.update_attribs(port="p1r") # first_cell_in_tbl = tbl.contents[0].contents # first_cell_in_tbl.update_attribs(port="p1l") return tbl def gv_node_component(component: Component) -> Table: # If no wires connected (except maybe loop wires)? if isinstance(component, Connector): if not (component.ports_left or component.ports_right): component.ports_left = True # Use left side pins by default # generate all rows to be shown in the node if component.show_name: str_name = f"{remove_links(component.designator)}" line_name = Td(str_name, bgcolor=component.bgcolor_title.html) else: line_name = None line_pn = partnumbers2list(component.partnumbers) is_simple_connector = ( isinstance(component, Connector) and component.style == "simple" ) if isinstance(component, Connector): line_info = [ html_line_breaks(component.type), html_line_breaks(component.subtype), f"{component.pincount}-pin" if component.show_pincount else None, str(component.color) if component.color else None, ] elif isinstance(component, Cable): line_info = [ html_line_breaks(component.type), f"{component.wirecount}x" if component.show_wirecount else None, component.gauge_str_with_equiv, "+ S" if component.shield else None, component.length_str, str(component.color) if component.color else None, ] if component.color: line_info.extend(colorbar_cells(component.color)) line_image, line_image_caption = image_and_caption_cells(component) line_additional_component_table = gv_additional_component_table(component) line_notes = [html_line_breaks(component.notes)] if isinstance(component, Connector): if component.style != "simple": line_ports = gv_pin_table(component) else: line_ports = None elif isinstance(component, Cable): line_ports = gv_conductor_table(component) lines = [ line_name, line_pn, line_info, line_ports, line_image, line_image_caption, line_additional_component_table, line_notes, ] tbl = nested_table(lines) if is_simple_connector: # Simple connectors have no pin table, and therefore, no ports to attach wires to. # Manually assign left and right ports here if required. # Use table itself for right port, and the first cell for left port. # Even if the table only has one cell, two separate ports can still be assigned. tbl.update_attribs(port="p1r") first_cell_in_tbl = tbl.contents[0].contents first_cell_in_tbl.update_attribs(port="p1l") return tbl def gv_additional_component_table(component): if not component.additional_components: return None rows = [] # TODO: support recursive management of additional components for subitem in component.additional_components: bom_entry = subitem.bom_entry rows.append( Tr( [ Td(f"{bom_entry.qty.number}", align="right"), Td( f"{bom_entry.qty.unit if bom_entry.qty.unit else 'x'}", align="left", ), Td(f"{bom_entry.description}", align="left"), ] ) ) return Table(rows, border=0) def calculate_node_bgcolor(component, harness_options): # assign component node bgcolor at the GraphViz node level # instead of at the HTML table level for better rendering of node outline if component.bgcolor: return component.bgcolor.html elif isinstance(component, Connector) and harness_options.bgcolor_connector: return harness_options.bgcolor_connector.html elif ( isinstance(component, Cable) and component.category == "bundle" and harness_options.bgcolor_bundle ): return harness_options.bgcolor_bundle.html elif isinstance(component, Cable) and harness_options.bgcolor_cable: return harness_options.bgcolor_cable.html def make_list_of_cells(inp) -> List[Td]: # inp may be List, if isinstance(inp, List): # ensure all list items are Td list_out = [item if isinstance(item, Td) else Td(item) for item in inp] return list_out else: if inp is None: return [] if isinstance(inp, Td): return [inp] else: return [Td(inp)] def nested_table(lines: List[Td]) -> Table: cell_lists = [make_list_of_cells(line) for line in lines] rows = [] for lst in cell_lists: if len(lst) == 0: continue # no cells in list cells = [item for item in lst if item.contents is not None] if len(cells) == 0: continue # no cells in list that are not None if ( len(cells) == 1 and isinstance(cells[0].contents, Table) and not "!" in cells[0].contents.attribs.get("id", "") ): # cell content is already a table, no need to re-wrap it; # unless explicitly asked to by a "!" in the ID field # as used by image_and_caption_cells() inner_table = cells[0].contents else: # nest cell content inside a table inner_table = Table( Tr(cells), border=0, cellborder=1, cellpadding=3, cellspacing=0 ) rows.append(Tr(Td(inner_table))) if len(rows) == 0: # create dummy row to avoid GraphViz errors due to empty