Skip to content

Commit

Permalink
Refactor ferrule GraphViz code generation
Browse files Browse the repository at this point in the history
Cleaner code, better graphical output, more flexible (splices look as good as ferrules)
  • Loading branch information
formatc1702 committed Jul 5, 2020
1 parent 4e9933f commit 54f114e
Showing 1 changed file with 20 additions and 33 deletions.
53 changes: 20 additions & 33 deletions src/wireviz/Harness.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,40 +59,27 @@ def create_graph(self):

for key, connector in self.connectors.items():
if connector.category == 'ferrule':
subtype = f', {connector.subtype}' if connector.subtype else ''
color = wv_colors.translate_color(connector.color, self.color_mode) if connector.color else ''
infostring = f'{connector.type}{subtype} {color}'

# id = identification
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 '']
identification = list(filter(None, identification))
if(len(identification) > 0):
infostring = f'{infostring}<br/>'
for attrib in identification:
infostring = f'{infostring}{attrib}, '
infostring = infostring[:-2] # remove trainling comma and space

infostring_l = infostring if connector.ports_right else ''
infostring_r = infostring if connector.ports_left else ''

# INFO: Leaving this one as a string.format form because f-strings do not work well with triple quotes
colorbar = f'<TD BGCOLOR="{wv_colors.translate_color(connector.color, "HEX")}" BORDER="1" SIDES="LR" WIDTH="4"></TD>' if connector.color else ''
dot.node(key, shape='none',
style='filled',
margin='0',
orientation='0' if connector.ports_left else '180',
label='''<
<TABLE BORDER="1" CELLBORDER="0" CELLSPACING="0" CELLPADDING="2"><TR>
<TD PORT="p1l"> {infostring_l} </TD>
{colorbar}
<TD PORT="p1r"> {infostring_r} </TD>
</TR></TABLE>
>'''.format(infostring_l=infostring_l, infostring_r=infostring_r, colorbar=colorbar))
rows = [[connector.type, connector.subtype, connector.color, '<!-- colorbar -->' if connector.color 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]]
rows = [list(filter(None, row)) for row in rows] # remove missing attributes

html = '<table border="0" cellspacing="0" cellpadding="0">'
for row in rows:
if len(row) > 0:
html = f'{html}<tr><td><table border="0" cellspacing="0" cellpadding="3" cellborder="1"><tr>'
for cell in row:
html = f'{html}<td>{cell}</td>'
html = f'{html}</tr></table></td></tr>'
html = f'{html}</table>'

if connector.color: # add color bar next to color info, if present
colorbar = f'<td bgcolor="{wv_colors.translate_color(connector.color, "HEX")}" width="4"></td>'
html = html.replace('<td><!-- colorbar --></td>', colorbar)

dot.node(key, label=f'<{html}>', shape='none', margin='0', style='filled', fillcolor='white')

else: # not a ferrule
identification = [connector.manufacturer,
Expand Down

0 comments on commit 54f114e

Please sign in to comment.