From ba8b83f792eb3eb791e67d09e51cc6b54e6f1586 Mon Sep 17 00:00:00 2001 From: KV Date: Wed, 15 Jul 2020 11:16:11 +0200 Subject: [PATCH 1/6] Make multi-line connector attributes centered for consistency --- src/wireviz/wv_helper.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wireviz/wv_helper.py b/src/wireviz/wv_helper.py index 9c19a5e..95bf408 100644 --- a/src/wireviz/wv_helper.py +++ b/src/wireviz/wv_helper.py @@ -117,7 +117,7 @@ def html_line_breaks(inp): return inp.replace('\n', '
') if isinstance(inp, str) else inp def graphviz_line_breaks(inp): - return inp.replace('\n', '\\l') if isinstance(inp, str) else inp # \l generates left-aligned new lines. http://www.graphviz.org/doc/info/attrs.html#k:escString + return inp.replace('\n', '\\n') if isinstance(inp, str) else inp # \n generates centered new lines. http://www.graphviz.org/doc/info/attrs.html#k:escString def remove_line_breaks(inp): return inp.replace('\n', ' ').rstrip() if isinstance(inp, str) else inp From 12d3002af865361c1b232aa180192edfc46e8f69 Mon Sep 17 00:00:00 2001 From: Jason Date: Wed, 15 Jul 2020 13:30:23 -0400 Subject: [PATCH 2/6] Add type hinting (#92) --- src/wireviz/Harness.py | 12 ++++++------ src/wireviz/wireviz.py | 7 ++++--- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/wireviz/Harness.py b/src/wireviz/Harness.py index ca9f8e6..95e7a2a 100644 --- a/src/wireviz/Harness.py +++ b/src/wireviz/Harness.py @@ -20,16 +20,16 @@ class Harness: self.cables = {} self.additional_bom_items = [] - def add_connector(self, name, *args, **kwargs): + def add_connector(self, name: str, *args, **kwargs) -> None: self.connectors[name] = Connector(name, *args, **kwargs) - def add_cable(self, name, *args, **kwargs): + def add_cable(self, name: str, *args, **kwargs) -> None: self.cables[name] = Cable(name, *args, **kwargs) - def add_bom_item(self, item): + def add_bom_item(self, item: dict) -> None: self.additional_bom_items.append(item) - def connect(self, from_name, from_pin, via_name, via_pin, to_name, to_pin): + def connect(self, from_name: str, from_pin: (int, str), via_name: str, via_pin: (int, str), to_name: str, to_pin: (int, str)) -> None: for (name, pin) in zip([from_name, to_name], [from_pin, to_pin]): # check from and to connectors if name is not None and name in self.connectors: connector = self.connectors[name] @@ -58,7 +58,7 @@ class Harness: if to_name in self.connectors: self.connectors[to_name].activate_pin(to_pin) - def create_graph(self): + def create_graph(self) -> Graph: dot = Graph() dot.body.append('// Graph generated by WireViz') dot.body.append('// https://github.com/formatc1702/WireViz') @@ -280,7 +280,7 @@ class Harness: data.seek(0) return data.read() - def output(self, filename: (str, Path), view=False, cleanup=True, fmt=('pdf', )): + def output(self, filename: (str, Path), view: bool = False, cleanup: bool = True, fmt: tuple = ('pdf', )) -> None: # graphical output graph = self.create_graph() for f in fmt: diff --git a/src/wireviz/wireviz.py b/src/wireviz/wireviz.py index d52d3b1..6c9a186 100755 --- a/src/wireviz/wireviz.py +++ b/src/wireviz/wireviz.py @@ -3,8 +3,9 @@ import argparse import os +from pathlib import Path import sys -from typing import Tuple +from typing import Any, Tuple import yaml @@ -16,7 +17,7 @@ from wireviz.Harness import Harness from wireviz.wv_helper import expand -def parse(yaml_input, file_out=None, return_types: (None, str, Tuple[str]) = None): +def parse(yaml_input: str, file_out: (str, Path) = None, return_types: (None, str, Tuple[str]) = None) -> Any: """ Parses yaml input string and does the high-level harness conversion @@ -196,7 +197,7 @@ def parse(yaml_input, file_out=None, return_types: (None, str, Tuple[str]) = Non return tuple(returns) if len(returns) != 1 else returns[0] -def parse_file(yaml_file, file_out=None): +def parse_file(yaml_file: str, file_out: (str, Path) = None) -> None: with open(yaml_file, 'r') as file: yaml_input = file.read() From 7e54c7aaef6d16e1cdce07bfa124854db0f12a36 Mon Sep 17 00:00:00 2001 From: Daniel Rojas Date: Sun, 5 Jul 2020 21:36:13 +0200 Subject: [PATCH 3/6] Refactor connector GraphViz code generation (#66) --- src/wireviz/Harness.py | 42 ++++++++++++++++++++++++++-------------- src/wireviz/wv_helper.py | 18 ++++++++++------- 2 files changed, 38 insertions(+), 22 deletions(-) diff --git a/src/wireviz/Harness.py b/src/wireviz/Harness.py index 95e7a2a..b7be17f 100644 --- a/src/wireviz/Harness.py +++ b/src/wireviz/Harness.py @@ -92,7 +92,6 @@ class Harness: 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 @@ -102,24 +101,37 @@ class Harness: 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], + '', + [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 95bf408..5759a3c 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 From 6c7d700a1f76ecbf4b9324d2aa40ebcedb7f5838 Mon Sep 17 00:00:00 2001 From: Daniel Rojas Date: Sun, 5 Jul 2020 21:45:39 +0200 Subject: [PATCH 4/6] Remove obsolete nesting function, fine-tune table generating behavior --- src/wireviz/Harness.py | 3 +-- src/wireviz/wv_helper.py | 16 +--------------- 2 files changed, 2 insertions(+), 17 deletions(-) diff --git a/src/wireviz/Harness.py b/src/wireviz/Harness.py index b7be17f..0ccbcaa 100644 --- a/src/wireviz/Harness.py +++ b/src/wireviz/Harness.py @@ -4,7 +4,7 @@ from wireviz.DataClasses import Connector, Cable from graphviz import Graph from wireviz import wv_colors -from wireviz.wv_helper import awg_equiv, mm2_equiv, tuplelist2tsv, nested, \ +from wireviz.wv_helper import awg_equiv, mm2_equiv, tuplelist2tsv, \ nested_html_table, flatten2d, index_if_list, html_line_breaks, \ graphviz_line_breaks, remove_line_breaks from collections import Counter @@ -91,7 +91,6 @@ class Harness: 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)]] - 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 diff --git a/src/wireviz/wv_helper.py b/src/wireviz/wv_helper.py index 5759a3c..016fe96 100644 --- a/src/wireviz/wv_helper.py +++ b/src/wireviz/wv_helper.py @@ -30,20 +30,6 @@ def awg_equiv(mm2): def mm2_equiv(awg): return mm2_equiv_table.get(str(awg), 'Unknown') -def nested(inp): - l = [] - for x in inp: - if isinstance(x, list): - if len(x) > 0: - n = nested(x) - if n != '': - l.append('{' + n + '}') - else: - if x is not None: - if x != '': - l.append(str(x)) - return '|'.join(l) - def nested_html_table(rows): # 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 @@ -57,7 +43,7 @@ def nested_html_table(rows): if cell is not None: html = f'{html}{cell}' html = f'{html}' - else: + elif row is not None: html = f'{html}{row}' html = f'{html}' return html From 11baf0f507117aa77bf1a6ab38cdc5282aadd772 Mon Sep 17 00:00:00 2001 From: Daniel Rojas Date: Sun, 5 Jul 2020 22:11:39 +0200 Subject: [PATCH 5/6] Move part number info directly below designator (to be consistent with the changes proposed in #11) --- src/wireviz/Harness.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/wireviz/Harness.py b/src/wireviz/Harness.py index 0ccbcaa..3461cc9 100644 --- a/src/wireviz/Harness.py +++ b/src/wireviz/Harness.py @@ -86,10 +86,10 @@ class Harness: for key, connector in self.connectors.items(): if connector.category == 'ferrule': - rows = [[html_line_breaks(connector.type), html_line_breaks(connector.subtype), connector.color, '' if connector.color else None], - [connector.manufacturer, + rows = [[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.type), html_line_breaks(connector.subtype), connector.color, '' if connector.color else None], [html_line_breaks(connector.notes)]] html = nested_html_table(rows) @@ -102,14 +102,14 @@ class Harness: else: # not a ferrule 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], - '', - [html_line_breaks(connector.notes)]] + [html_line_breaks(connector.type), + html_line_breaks(connector.subtype), + f'{connector.pincount}-pin' if connector.show_pincount else None], + '', + [html_line_breaks(connector.notes)]] html = nested_html_table(rows) pinouts = [] From d1b48861ec82a53b6a279924821eb6f304600b54 Mon Sep 17 00:00:00 2001 From: Jason Date: Wed, 15 Jul 2020 14:21:09 -0400 Subject: [PATCH 6/6] Add initial contribution guidelines --- CONTRIBUTING.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 CONTRIBUTING.md diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..7a94ac7 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,18 @@ +# Contribution Guidelines + +When contributing to this repository, please first discuss the change you +wish to make via issue, email, or any other method with the owners of this +repository before making a change. + +## Pull Requests + +1. Fork this repository to your repository +1. Clone the repository to your local machine +1. Checkout the `dev` branch +1. Make any changes to the code on the `dev` branch +1. Push your changes to your fork +1. Create new pull request + +## Documentation Strings + +Documentation strings are to follow the Google Style ([examples](https://sphinxcontrib-napoleon.readthedocs.io/en/latest/example_google.html)).