diff --git a/src/wireviz/wireviz.py b/src/wireviz/wireviz.py index b0c1402..7ba7ea6 100755 --- a/src/wireviz/wireviz.py +++ b/src/wireviz/wireviz.py @@ -72,6 +72,7 @@ class Harness: infostring_l = infostring if n.ports_right else '' infostring_r = infostring if n.ports_left else '' + # INFO: Leaving this one as a string.format form because f-strings do not work well with triple quotes dot.node(k, shape='none', style='filled', margin='0', @@ -87,14 +88,12 @@ class Harness: >'''.format(infostring_l=infostring_l, infostring_r=infostring_r, - colorbar=''.format(wv_colors.translate_color(n.color, 'HEX')) if n.color else '')) + colorbar=f'' if n.color else '')) else: # not a ferrule - # a = attributes attributes = [n.type, - n.subtype, - f'{n.pincount}-pin' if n.show_pincount else''] - # p = pinout + n.subtype, + f'{n.pincount}-pin' if n.show_pincount else''] pinouts = [[], [], []] for pinnumber, pinname in zip(n.pinnumbers, n.pinout): if n.hide_disconnected_pins and not n.visible_pins.get(pinnumber, False): @@ -104,7 +103,6 @@ class Harness: pinouts[0].append(f'{pinnumber}') if n.ports_right: pinouts[2].append(f'{pinnumber}') - # l = label label = [n.name if n.show_name else '', attributes, pinouts, n.notes] dot.node(k, label=nested(label)) @@ -119,32 +117,35 @@ class Harness: else: raise Exception('No side for loops') for loop in n.loops: - dot.edge('{name}:p{port_from}{loop_side}:{loop_dir}'.format(name=n.name, port_from=loop[0], port_to=loop[1], loop_side=loop_side, loop_dir=loop_dir), - '{name}:p{port_to}{loop_side}:{loop_dir}'.format(name=n.name, port_from=loop[0], port_to=loop[1], loop_side=loop_side, loop_dir=loop_dir)) + + # FIXME: Original string.format style had some unused arguments (port_to for 1st arg, + # port_from for 2nd arg). De we need them back? + + dot.edge(f'{n.name}:p{loop[0]}{loop_side}:{loop_dir}', + f'{n.name}:p{loop[1]}{loop_side}:{loop_dir}') for k, c in self.cables.items(): - # a = attributes - attributes = ['{}x'.format(len(c.colors)) if c.show_wirecount else '', - '{} {}{}'.format(c.gauge, c.gauge_unit, ' ({} AWG)'.format(awg_equiv(c.gauge)) if c.gauge_unit == - 'mm\u00B2' and c.show_equiv else '') if c.gauge else '', # TODO: show equiv - '+ S' if c.shield else '', - f'{c.length} m' if c.length > 0 else ''] + awg_fmt = f' ({awg_equiv(c.gauge)} AWG)' if c.gauge_unit == 'mm\u00B2' and c.show_equiv else '' + attributes = [f'{len(c.colors)}x' if c.show_wirecount else '', + f'{c.gauge} {c.gauge_unit}{awg_fmt}' if c.gauge else '', # TODO: show equiv + '+ S' if c.shield else '', + f'{c.length} m' if c.length > 0 else ''] attributes = list(filter(None, attributes)) html = '' # name+attributes table + html = f'{html}' # attribute row + html = f'{html}
' # main table - html = html + '' # name+attributes table + html = f'{html}
' # name+attributes table if c.show_name: html = f'{html}' - html = html + '' # attribute row + html = f'{html}' # attribute row for attrib in attributes: html = f'{html}' - html = html + '' # attribute row - html = html + '
{c.name}
{attrib}
' # name+attributes table - html = html + ' ' # spacer between attributes and wires + html = f'{html} ' # spacer between attributes and wires - html = html + '' # conductor table + html = f'{html}' # spacer at the end - html = html + '' # main table + html = f'{html}
' # conductor table for i, x in enumerate(c.colors, 1): p = [] @@ -156,8 +157,8 @@ class Harness: html = f'{html}' html = f'{html}' bgcolor = wv_colors.translate_color(x, 'hex') - html = html + ''.format( - colspan=len(p), bgcolor=bgcolor if bgcolor != '' else '#ffffff', port=f'w{i}') + bgcolor = bgcolor if bgcolor != '' else '#ffffff' + html = f'{html}' if c.shield: p = ['', 'Shield', ''] @@ -165,28 +166,28 @@ class Harness: html = f'{html}' for bla in p: html = html + f'' - html = html + '' - html = html + ''.format( - colspan=len(p), bgcolor=wv_colors.translate_color(x, 'hex'), port='ws') + html = f'{html}' - html = html + '' # spacer at the end + # FIXME, original string.format had a unused bgcolor argument. Do we need it back + html = f'{html}' - html = html + '
{bla}
{bla}
 
' # conductor table + html = f'{html}
 
' # conductor table + + html = f'{html}' # main table if c.notes: - html = html + f'{c.notes}' # notes table - html = html + ' ' # spacer at the end + html = f'{html}{c.notes}' # notes table + html = f'{html} ' # spacer at the end - html = html + '' # main table + html = f'{html}' # main table # connections for x in c.connections: if isinstance(x.via_port, int): # check if it's an actual wire and not a shield search_color = c.colors[x.via_port - 1] if search_color in wv_colors.color_hex: - dot.attr('edge', color='#000000:{wire_color}:#000000'.format( - wire_color=wv_colors.color_hex[search_color])) + dot.attr('edge', color=f'#000000:{wv_colors.color_hex[search_color]}:#000000') else: # color name not found dot.attr('edge', color='#000000:#ffffff:#000000') else: # it's a shield connection @@ -194,19 +195,22 @@ class Harness: if x.from_port is not None: # connect to left from_ferrule = self.connectors[x.from_name].category == 'ferrule' - code_left_1 = '{from_name}{from_port}:e'.format( - from_name=x.from_name, from_port=f':p{x.from_port}r' if not from_ferrule else '') - code_left_2 = '{via_name}:w{via_wire}:w'.format( - via_name=c.name, via_wire=x.via_port, via_subport='i' if c.show_pinout else '') + port = f':p{x.from_port}r' if not from_ferrule else '' + code_left_1 = f'{x.from_name}{port}:e' + # FIXME: Uncomment, then add to end of f-string if needed + # via_subport = 'i' if c.show_pinout else '' + code_left_2 = f'{c.name}:w{x.via_port}:w' dot.edge(code_left_1, code_left_2) from_string = f'{x.from_name}:{x.from_port}' if not from_ferrule else '' html = html.replace(f'', from_string) if x.to_port is not None: # connect to right to_ferrule = self.connectors[x.to_name].category == 'ferrule' - code_right_1 = '{via_name}:w{via_wire}:e'.format( - via_name=c.name, via_wire=x.via_port, via_subport='o' if c.show_pinout else '') - code_right_2 = '{to_name}{to_port}:w'.format( - to_name=x.to_name, to_port=f':p{x.to_port}l' if not to_ferrule else '') + + # FIXME: Add in if it was supposed to be here. the add to fstring two lines down + #via_subport = 'o' if c.show_pinout else '' + code_right_1 = f'{c.name}:w{x.via_port}:e' + to_port = f":p{x.to_port}l" if not to_ferrule else "" + code_right_2 = f'{x.to_name}{to_port}:w' dot.edge(code_right_1, code_right_2) to_string = f'{x.to_name}:{x.to_port}' if not to_ferrule else '' html = html.replace(f'', to_string) @@ -246,8 +250,8 @@ class Harness: for row in listy[1:]: file.write('') for i, item in enumerate(row): - file.write('{content}'.format( - content=item, align='align="right"' if listy[0][i] == 'Qty' else '')) + align = 'align="right"' if listy[0][i] == 'Qty' else '' + file.write(f'<{item}') file.write('') file.write('') @@ -264,12 +268,11 @@ class Harness: shared = next(iter(items.values())) designators = list(items.keys()) designators.sort() - name = 'Connector{type}{subtype}{pincount}{color}'.format(type=f', {shared.type}' if shared.type else '', - subtype=', {}'.format( - shared.subtype) if shared.subtype else '', - pincount=', {} pins'.format( - shared.pincount) if shared.category != 'ferrule' else '', - color=f', {shared.color}' if shared.color else '') + conn_type = f', {shared.type}' if shared.type else '' + conn_subtype = f', {shared.subtype}' if shared.subtype else '' + conn_pincount = f', {shared.pincount} pins' if shared.category != 'ferrule' else '' + conn_color = f', {shared.color}' if shared.color else '' + name = f'Connector{conn_type}{conn_subtype}{conn_pincount}{conn_color}' item = {'item': name, 'qty': len(designators), 'unit': '', 'designators': designators if shared.category != 'ferrule' else ''} bom_connectors.append(item) @@ -285,10 +288,9 @@ class Harness: designators = list(items.keys()) designators.sort() total_length = sum(i.length for i in items.values()) - name = 'Cable, {wirecount}{gauge}{shield}'.format(wirecount=shared.wirecount, - gauge=' x {} {}'.format( - shared.gauge, shared.gauge_unit) if shared.gauge else ' wires', - shield=' shielded' if shared.shield else '') + gauge_name = f' x {shared.gauge} {shared.gauge_unit}'if shared.gauge else ' wires' + shield_name = ' shielded' if shared.shield else '' + name = f'Cable, {shared.wirecount}{gauge_name}{shield_name}' item = {'item': name, 'qty': round(total_length, 3), 'unit': 'm', 'designators': designators} bom_cables.append(item) # bundles (ignores wirecount) @@ -315,8 +317,9 @@ class Harness: designators = list(dict.fromkeys(designators)) designators.sort() total_length = sum(i['length'] for i in items) - name = 'Wire{gauge}{color}'.format(gauge=', {} {}'.format(shared['gauge'], shared['gauge_unit']) if shared['gauge'] else '', - color=', {}'.format(shared['color']) if shared['color'] != '' else '') + gauge_name = f', {shared["gauge"]} {shared["gauge_unit"]}' if shared['gauge'] else '' + gauge_color = f', {shared["color"]}' if shared['color'] != '' else '' + name = f'Wire{gauge_name}{gauge_color}' item = {'item': name, 'qty': round(total_length, 3), 'unit': 'm', 'designators': designators} bom_cables.append(item) bom_cables = sorted(bom_cables, key=lambda k: k['item']) # https://stackoverflow.com/a/73050