Skip to content

Commit

Permalink
Apply some manual fixes, reapply black
Browse files Browse the repository at this point in the history
  • Loading branch information
formatc1702 committed Aug 5, 2022
1 parent a7a7b29 commit 8bef71e
Show file tree
Hide file tree
Showing 9 changed files with 147 additions and 354 deletions.
39 changes: 17 additions & 22 deletions src/wireviz/DataClasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,12 @@ def __post_init__(self):
self.fixedsize = (self.width or self.height) and self.scale is None

if self.scale is None:
self.scale = (
"false"
if not self.width and not self.height
else "both"
if self.width and self.height
else "true"
) # When only one dimension is specified.
if not self.width and not self.height:
self.scale = "false"
elif self.width and self.height:
self.scale = "both"
else:
self.scale = "true" # When only one dimension is specified.

if self.fixedsize:
# If only one dimension is specified, compute the other
Expand Down Expand Up @@ -133,9 +132,8 @@ class AdditionalComponent:

@property
def description(self) -> str:
return self.type.rstrip() + (
f", {self.subtype.rstrip()}" if self.subtype else ""
)
s = self.type.rstrip() + f", {self.subtype.rstrip()}" if self.subtype else ""
return s


@dataclass
Expand Down Expand Up @@ -203,9 +201,8 @@ def __post_init__(self) -> None:
self.show_name = self.style != "simple" and self.name[0:2] != "__"

if self.show_pincount is None:
self.show_pincount = (
self.style != "simple"
) # hide pincount for simple (1 pin) connectors by default
# hide pincount for simple (1 pin) connectors by default
self.show_pincount = self.style != "simple"

for loop in self.loops:
# TODO: check that pins to connect actually exist
Expand Down Expand Up @@ -322,9 +319,8 @@ def __post_init__(self) -> None:
if self.wirecount: # number of wires explicitly defined
if self.colors: # use custom color palette (partly or looped if needed)
pass
elif (
self.color_code
): # use standard color palette (partly or looped if needed)
elif self.color_code:
# use standard color palette (partly or looped if needed)
if self.color_code not in COLOR_CODES:
raise Exception("Unknown color code")
self.colors = COLOR_CODES[self.color_code]
Expand Down Expand Up @@ -361,14 +357,12 @@ def __post_init__(self) -> None:
raise Exception("lists of part data are only supported for bundles")

if self.show_name is None:
self.show_name = (
self.name[0:2] != "__"
) # hide designators for auto-generated cables by default
# hide designators for auto-generated cables by default
self.show_name = self.name[0:2] != "__"

if not self.show_wirenumbers:
self.show_wirenumbers = (
self.category != "bundle"
) # by default, show wire numbers for cables, hide for bundles
# by default, show wire numbers for cables, hide for bundles
self.show_wirenumbers = self.category != "bundle"

for i, item in enumerate(self.additional_components):
if isinstance(item, dict):
Expand All @@ -383,6 +377,7 @@ def connect(
to_name: Optional[Designator],
to_pin: NoneOrMorePinIndices,
) -> None:

from_pin = int2tuple(from_pin)
via_wire = int2tuple(via_wire)
to_pin = int2tuple(to_pin)
Expand Down
195 changes: 72 additions & 123 deletions src/wireviz/Harness.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,8 @@ def connect(
raise Exception(
f"{via_name}:{via_wire} is used for more than one wire."
)
via_wire = (
cable.colors.index(via_wire) + 1
) # list index starts at 0, wire IDs start at 1
# list index starts at 0, wire IDs start at 1
via_wire = cable.colors.index(via_wire) + 1
elif via_wire in cable.wirelabels:
if cable.wirelabels.count(via_wire) > 1:
raise Exception(
Expand Down Expand Up @@ -180,37 +179,22 @@ def create_graph(self) -> Graph:
connector.ports_left = True # Use left side pins.

html = []
# fmt: off
rows = [[f'{html_bgcolor(connector.bgcolor_title)}{remove_links(connector.name)}'
if connector.show_name else None],
[pn_info_string(HEADER_PN, None, remove_links(connector.pn)),
html_line_breaks(pn_info_string(HEADER_MPN, connector.manufacturer, connector.mpn)),
html_line_breaks(pn_info_string(HEADER_SPN, connector.supplier, connector.spn))],
[html_line_breaks(connector.type),
html_line_breaks(connector.subtype),
f'{connector.pincount}-pin' if connector.show_pincount else None,
translate_color(connector.color, self.options.color_mode) if connector.color else None,
html_colorbar(connector.color)],
'<!-- connector table -->' if connector.style != 'simple' else None,
[html_image(connector.image)],
[html_caption(connector.image)]]
# fmt: on

rows = [
[
f"{html_bgcolor(connector.bgcolor_title)}{remove_links(connector.name)}"
if connector.show_name
else None
],
[
pn_info_string(HEADER_PN, None, remove_links(connector.pn)),
html_line_breaks(
pn_info_string(
HEADER_MPN, connector.manufacturer, connector.mpn
)
),
html_line_breaks(
pn_info_string(HEADER_SPN, connector.supplier, connector.spn)
),
],
[
html_line_breaks(connector.type),
html_line_breaks(connector.subtype),
f"{connector.pincount}-pin" if connector.show_pincount else None,
translate_color(connector.color, self.options.color_mode)
if connector.color
else None,
html_colorbar(connector.color),
],
"<!-- connector table -->" if connector.style != "simple" else None,
[html_image(connector.image)],
[html_caption(connector.image)],
]
rows.extend(get_additional_component_table(self, connector))
rows.append([html_line_breaks(connector.notes)])
html.extend(nested_html_table(rows, html_bgcolor_attr(connector.bgcolor)))
Expand All @@ -231,23 +215,22 @@ def create_graph(self) -> Graph:
and not connector.visible_pins.get(pinname, False)
):
continue

pinhtml.append(" <tr>")
if connector.ports_left:
pinhtml.append(f' <td port="p{pinindex+1}l">{pinname}</td>')
if pinlabel:
pinhtml.append(f" <td>{pinlabel}</td>")
if connector.pincolors:
if pincolor in wv_colors._color_hex.keys():
pinhtml.append(
f' <td sides="tbl">{translate_color(pincolor, self.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: off
pinhtml.append(f' <td sides="tbl">{translate_color(pincolor, self.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>')

Expand Down Expand Up @@ -309,61 +292,36 @@ def create_graph(self) -> Graph:
elif cable.gauge_unit.upper() == "AWG":
awg_fmt = f" ({mm2_equiv(cable.gauge)} mm\u00B2)"

rows = [
[
f"{html_bgcolor(cable.bgcolor_title)}{remove_links(cable.name)}"
if cable.show_name
else None
],
[
pn_info_string(HEADER_PN, None, remove_links(cable.pn))
if not isinstance(cable.pn, list)
else None,
html_line_breaks(
pn_info_string(
HEADER_MPN,
cable.manufacturer
if not isinstance(cable.manufacturer, list)
else None,
cable.mpn if not isinstance(cable.mpn, list) else None,
)
),
html_line_breaks(
pn_info_string(
HEADER_SPN,
cable.supplier
if not isinstance(cable.supplier, list)
else None,
cable.spn if not isinstance(cable.spn, list) else None,
)
),
],
[
html_line_breaks(cable.type),
f"{cable.wirecount}x" if cable.show_wirecount else None,
f"{cable.gauge} {cable.gauge_unit}{awg_fmt}"
if cable.gauge
else None,
"+ S" if cable.shield else None,
f"{cable.length} {cable.length_unit}" if cable.length > 0 else None,
translate_color(cable.color, self.options.color_mode)
if cable.color
else None,
html_colorbar(cable.color),
],
"<!-- wire table -->",
[html_image(cable.image)],
[html_caption(cable.image)],
]
# fmt: off
rows = [[f'{html_bgcolor(cable.bgcolor_title)}{remove_links(cable.name)}'
if cable.show_name else None],
[pn_info_string(HEADER_PN, None,
remove_links(cable.pn)) if not isinstance(cable.pn, list) else None,
html_line_breaks(pn_info_string(HEADER_MPN,
cable.manufacturer if not isinstance(cable.manufacturer, list) else None,
cable.mpn if not isinstance(cable.mpn, list) else None)),
html_line_breaks(pn_info_string(HEADER_SPN,
cable.supplier if not isinstance(cable.supplier, list) else None,
cable.spn if not isinstance(cable.spn, list) else None))],
[html_line_breaks(cable.type),
f'{cable.wirecount}x' if cable.show_wirecount else None,
f'{cable.gauge} {cable.gauge_unit}{awg_fmt}' if cable.gauge else None,
'+ S' if cable.shield else None,
f'{cable.length} {cable.length_unit}' if cable.length > 0 else None,
translate_color(cable.color, self.options.color_mode) if cable.color else None,
html_colorbar(cable.color)],
'<!-- wire table -->',
[html_image(cable.image)],
[html_caption(cable.image)]]
# fmt: on

rows.extend(get_additional_component_table(self, cable))
rows.append([html_line_breaks(cable.notes)])
html.extend(nested_html_table(rows, html_bgcolor_attr(cable.bgcolor)))

wirehtml = []
wirehtml.append(
'<table border="0" cellspacing="0" cellborder="0">'
) # conductor table
# conductor table
wirehtml.append('<table border="0" cellspacing="0" cellborder="0">')
wirehtml.append(" <tr><td>&nbsp;</td></tr>")

for i, (connection_color, wirelabel) in enumerate(
Expand All @@ -389,28 +347,20 @@ def create_graph(self) -> Graph:
wirehtml.append(f" <td><!-- {i}_out --></td>")
wirehtml.append(" </tr>")

bgcolors = (
["#000000"] + get_color_hex(connection_color, pad=pad) + ["#000000"]
)
# fmt: off
bgcolors = ['#000000'] + get_color_hex(connection_color, pad=pad) + ['#000000']
wirehtml.append(f" <tr>")
wirehtml.append(
f' <td colspan="3" border="0" cellspacing="0" cellpadding="0" port="w{i}" height="{(2 * len(bgcolors))}">'
)
wirehtml.append(
' <table cellspacing="0" cellborder="0" border="0">'
)
for j, bgcolor in enumerate(
bgcolors[::-1]
): # Reverse to match the curved wires when more than 2 colors
wirehtml.append(
f' <tr><td colspan="3" cellpadding="0" height="2" bgcolor="{bgcolor if bgcolor != "" else wv_colors.default_color}" border="0"></td></tr>'
)
wirehtml.append(f' <td colspan="3" border="0" cellspacing="0" cellpadding="0" port="w{i}" height="{(2 * len(bgcolors))}">')
wirehtml.append(' <table cellspacing="0" cellborder="0" border="0">')
for j, bgcolor in enumerate(bgcolors[::-1]): # Reverse to match the curved wires when more than 2 colors
wirehtml.append(f' <tr><td colspan="3" cellpadding="0" height="2" bgcolor="{bgcolor if bgcolor != "" else wv_colors.default_color}" border="0"></td></tr>')
wirehtml.append(" </table>")
wirehtml.append(" </td>")
wirehtml.append(" </tr>")
if (
cable.category == "bundle"
): # for bundles individual wires can have part information
# fmt: on

# for bundles, individual wires can have part information
if cable.category == "bundle":
# create a list of wire parameters
wireidentification = []
if isinstance(cable.pn, list):
Expand Down Expand Up @@ -439,14 +389,14 @@ def create_graph(self) -> Graph:
wireidentification.append(html_line_breaks(supplier_info))
# print parameters into a table row under the wire
if len(wireidentification) > 0:
# fmt: off
wirehtml.append(' <tr><td colspan="3">')
wirehtml.append(
' <table border="0" cellspacing="0" cellborder="0"><tr>'
)
wirehtml.append(' <table border="0" cellspacing="0" cellborder="0"><tr>')
for attrib in wireidentification:
wirehtml.append(f" <td>{attrib}</td>")
wirehtml.append(" </tr></table>")
wirehtml.append(" </td></tr>")
# fmt: on

if cable.shield:
wirehtml.append(" <tr><td>&nbsp;</td></tr>") # spacer
Expand All @@ -464,9 +414,9 @@ def create_graph(self) -> Graph:
else:
# shield is shown as a thin black wire
attributes = f'height="2" bgcolor="#000000" border="0"'
wirehtml.append(
f' <tr><td colspan="3" cellpadding="0" {attributes} port="ws"></td></tr>'
)
# fmt: off
wirehtml.append(f' <tr><td colspan="3" cellpadding="0" {attributes} port="ws"></td></tr>')
# fmt: on

wirehtml.append(" <tr><td>&nbsp;</td></tr>")
wirehtml.append(" </table>")
Expand All @@ -477,9 +427,8 @@ def create_graph(self) -> Graph:

# connections
for connection in cable.connections:
if isinstance(
connection.via_port, int
): # check if it's an actual wire and not a shield
if isinstance(connection.via_port, int):
# check if it's an actual wire and not a shield
dot.attr(
"edge",
color=":".join(
Expand Down Expand Up @@ -734,15 +683,15 @@ def output(
with open_file_write(f"{filename}.bom.tsv") as file:
file.write(tuplelist2tsv(bomlist))
if "csv" in fmt:
print(
"CSV output is not yet supported"
) # TODO: implement CSV output (preferrably using CSV library)
# TODO: implement CSV output (preferrably using CSV library)
print("CSV output is not yet supported")
# HTML output
if "html" in fmt:
generate_html_output(filename, bomlist, self.metadata, self.options)
# PDF output
if "pdf" in fmt:
print("PDF output is not yet supported") # TODO: implement PDF output
# TODO: implement PDF output
print("PDF output is not yet supported")
# delete SVG if not needed
if "html" in fmt and not "svg" in fmt and not svg_already_exists:
Path(f"{filename}.svg").unlink()
Expand Down
Loading

0 comments on commit 8bef71e

Please sign in to comment.