Merge multi-color wires into refactored code

This commit is contained in:
Andrew Katz 2020-07-01 13:58:26 -04:00
parent f0d10bfef8
commit 525be74c98
6 changed files with 103 additions and 95 deletions

View File

@ -1,4 +1,5 @@
#!/usr/bin/python3 #!/usr/bin/python3
# -*- coding: utf-8 -*-
import os import os
from setuptools import setup, find_packages from setuptools import setup, find_packages
@ -42,4 +43,4 @@ setup(
'License :: OSI Approved :: GNU General Public License v3 (GPLv3)', 'License :: OSI Approved :: GNU General Public License v3 (GPLv3)',
], ],
) )

View File

@ -1,50 +0,0 @@
import wireviz
import os
demos = 2 # 2
examples = 7 # 7
tutorials = 7 # 7
if demos:
for i in range(1,demos+1):
fn = '../examples/demo{:02d}.yml'.format(i)
print(fn)
wireviz.parse(fn, gen_bom=True)
if examples:
with open(os.path.abspath('../examples/readme.md'), 'w') as file:
file.write('# Example gallery\n')
for i in range(1,examples+1):
fn = '../examples/ex{:02d}.yml'.format(i)
print(fn)
wireviz.parse(fn, gen_bom=True)
file.write('## Example {:02d}\n'.format(i))
file.write('![](ex{:02d}.png)\n\n'.format(i))
file.write('[Source](ex{:02d}.yml) - [Bill of Materials](ex{:02d}.bom.tsv)\n\n\n'.format(i,i))
if tutorials:
with open(os.path.abspath('../tutorial/readme.md'), 'w') as file:
file.write('# WireViz Tutorial\n')
for i in range(1,tutorials+1):
fn = '../tutorial/tutorial{:02d}.yml'.format(i)
print(fn)
wireviz.parse(fn, gen_bom=True)
with open(os.path.abspath('../tutorial/tutorial{:02d}.md'.format(i)), 'r') as info:
for line in info:
file.write(line.replace('## ', '## {} - '.format(i)))
file.write('\n[Source](tutorial{:02d}.yml):\n\n'.format(i))
with open(os.path.abspath('../tutorial/tutorial{:02d}.yml'.format(i)), 'r') as src:
file.write('```yaml\n')
for line in src:
file.write(line)
file.write('```\n')
file.write('\n')
file.write('\nOutput:\n\n'.format(i))
file.write('![](tutorial{:02d}.png)\n\n'.format(i))
file.write('[Bill of Materials](tutorial{:02d}.bom.tsv)\n\n\n'.format(i))

0
src/wireviz.py → src/wireviz.old Executable file → Normal file
View File

View File

@ -39,7 +39,7 @@ class Harness:
font = 'arial' font = 'arial'
dot.attr('graph', rankdir='LR', dot.attr('graph', rankdir='LR',
ranksep='2', ranksep='2',
bgcolor='white', bgcolor=wv_colors.default_bknd_color,
nodesep='0.33', nodesep='0.33',
fontname=font) fontname=font)
dot.attr('node', shape='record', dot.attr('node', shape='record',
@ -65,7 +65,6 @@ class Harness:
infostring_l = infostring if connector.ports_right else '' infostring_l = infostring if connector.ports_right else ''
infostring_r = infostring if connector.ports_left 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 '' 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', dot.node(key, shape='none',
style='filled', style='filled',
@ -141,7 +140,7 @@ class Harness:
html = f'{html}</tr>' # attribute row html = f'{html}</tr>' # attribute row
html = f'{html}</table></td></tr>' # name+attributes table html = f'{html}</table></td></tr>' # name+attributes table
html = f'{html}<tr><td>&nbsp;</td></tr>' # spacer between attributes and wires html = f'{html}<tr><td></td></tr>' # spacer between attributes and wires
html = f'{html}<tr><td><table border="0" cellspacing="0" cellborder="0">' # conductor table html = f'{html}<tr><td><table border="0" cellspacing="0" cellborder="0">' # conductor table
@ -154,27 +153,29 @@ class Harness:
for bla in p: for bla in p:
html = f'{html}<td>{bla}</td>' html = f'{html}<td>{bla}</td>'
html = f'{html}</tr>' html = f'{html}</tr>'
bgcolor = wv_colors.translate_color(connection, 'hex') bgcolors = ('#000000:' + wv_colors.translate_color(connection, 'hex') + ':#000000').split(':')
bgcolor = bgcolor if bgcolor != '' else '#ffffff' html = f'{html}<tr><td colspan="{len(p)}" border="0" cellspacing="0" cellpadding="0" port="w{i}" height="{(2 * len(bgcolors))}"><table cellspacing="0" cellborder="0" border = "0">'
html = f'{html}<tr><td colspan="{len(p)}" cellpadding="0" height="6" bgcolor="{bgcolor}" border="2" sides="tb" port="w{i}"></td></tr>' for j, bgcolor in enumerate(
bgcolors[::-1]): # Reverse to match the curved wires when more than 2 colors
html = f'{html}<tr><td colspan="{len(p)}" cellpadding="0" height="2" bgcolor="{bgcolor if bgcolor != "" else wv_colors.default_color}" border="0"></td></tr>'
html = html + '</table></td></tr>'
if cable.shield: if cable.shield:
p = ['<!-- s_in -->', 'Shield', '<!-- s_out -->'] p = ['<!-- s_in -->', 'Shield', '<!-- s_out -->']
html = f'{html}<tr><td>&nbsp;</td></tr>' # spacer html = f'{html}<tr><td></td></tr>' # spacer
html = f'{html}<tr>' html = f'{html}<tr>'
for bla in p: for bla in p:
html = html + f'<td>{bla}</td>' html = html + f'<td>{bla}</td>'
html = f'{html}</tr>' html = f'{html}</tr>'
html = f'{html}<tr><td colspan="{len(p)}" cellpadding="0" height="6" border="2" sides="b" port="ws"></td></tr>' html = f'{html}<tr><td colspan="{len(p)}" cellpadding="0" bgcolor="{wv_colors.shield_color}" height="6" border="2" sides="b" port="ws"></td></tr>'
html = f'{html}<tr><td>&nbsp;</td></tr>' # spacer at the end html = f'{html}<tr><td></td></tr>' # spacer at the end
html = f'{html}</table>' # conductor table html = f'{html}</table>' # conductor table
html = f'{html}</td></tr>' # main table html = f'{html}</td></tr>' # main table
if cable.notes: if cable.notes:
html = f'{html}<tr><td cellpadding="3">{cable.notes}</td></tr>' # notes table html = f'{html}<tr><td cellpadding="3">{cable.notes}</td></tr>' # notes table
html = f'{html}<tr><td>&nbsp;</td></tr>' # spacer at the end html = f'{html}<tr><td></td></tr>' # spacer at the end
html = f'{html}</table>' # main table html = f'{html}</table>' # main table
@ -182,12 +183,10 @@ class Harness:
for connection in cable.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
search_color = cable.colors[connection.via_port - 1] search_color = cable.colors[connection.via_port - 1]
if search_color in wv_colors.color_hex: dot.attr('edge', color='#000000:{wire_color}:#000000'.format(
dot.attr('edge', color=f'#000000:{wv_colors.color_hex[search_color]}:#000000') wire_color=wv_colors.get_color_hex(search_color)))
else: # color name not found
dot.attr('edge', color='#000000:#ffffff:#000000')
else: # it's a shield connection else: # it's a shield connection
dot.attr('edge', color='#000000') dot.attr('edge', color=wv_colors.shield_color)
if connection.from_port is not None: # connect to left if connection.from_port is not None: # connect to left
from_ferrule = self.connectors[connection.from_name].category == 'ferrule' from_ferrule = self.connectors[connection.from_name].category == 'ferrule'

View File

@ -9,7 +9,7 @@ sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
from wireviz import wireviz from wireviz import wireviz
demos = 2 # 2 demos = 2 # 2
examples = 9 # 9 examples = 10 # 10
tutorials = 7 # 7 tutorials = 7 # 7
if demos: if demos:

View File

@ -1,13 +1,37 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import sys
COLOR_CODES = { COLOR_CODES = {
'DIN': ['WH', 'BN', 'GN', 'YE', 'GY', 'PK', 'BU', 'RD', 'BK', 'VT'], # ,'GYPK','RDBU','WHGN','BNGN','WHYE','YEBN','WHGY','GYBN','WHPK','PKBN'], 'DIN': ['WH', 'BN', 'GN', 'YE', 'GY', 'PK', 'BU', 'RD', 'BK', 'VT', 'GYPK', 'RDBU', 'WHGN', 'BNGN', 'WHYE', 'YEBN',
'WHGY', 'GYBN', 'WHPK', 'PKBN', 'WHBU', 'BNBU', 'WHRD', 'BNRD', 'WHBK', 'BNBK', 'GYGN', 'YEGY', 'PKGN',
'YEPK', 'GNBU', 'YEBU', 'GNRD', 'YERD', 'GNBK', 'YEBK', 'GYBU', 'PKBU', 'GYRD', 'PKRD', 'GYBK', 'PKBK',
'BUBK', 'RDBK', 'WHBNBK', 'YEGNBK', 'GYPKBK', 'RDBUBK', 'WHGNBK', 'BNGNBK', 'WHYEBK', 'YEBNBK', 'WHGYBK',
'GYBNBK', 'WHPKBK', 'PKBNBK', 'WHBUBK', 'BNBUBK', 'WHRDBK', 'BNRDBK'],
'IEC': ['BN', 'RD', 'OG', 'YE', 'GN', 'BU', 'VT', 'GY', 'WH', 'BK'], 'IEC': ['BN', 'RD', 'OG', 'YE', 'GN', 'BU', 'VT', 'GY', 'WH', 'BK'],
'BW': ['BK', 'WH'], 'BW': ['BK', 'WH'],
'TEL': ['BUWH', 'WHBU', 'OGWH', 'WHOG', 'GNWH', 'WHGN', 'BNWH', 'WHBN', 'SLWH', 'WHSL', 'BURD', 'RDBU', 'OGRD',
'RDOG', 'GNRD', 'RDGN', 'BNRD', 'RDBN', 'SLRD', 'RDSL', 'BUBK', 'BKBU', 'OGBK', 'BKOG', 'GNBK', 'BKGN',
'BNBK', 'BKBN', 'SLBK', 'BKSL', 'BUYW', 'YWBU', 'OGYW', 'YWOG', 'GNYW', 'YWGN', 'BNYW', 'YWBN', 'SLYW',
'YWSL', 'BUVT', 'VTBU', 'OGVT', 'VTOG', 'GNVT', 'VTGN', 'BNVT', 'VTBN', 'SLVT', 'VTSL'],
'TELALT': ['WHBU', 'BU', 'WHOG', 'OG', 'WHGN', 'GN', 'WHBN', 'BN', 'WHSL', 'SL', 'RDBU', 'BURD', 'RDOG', 'OGRD',
'RDGN', 'GNRD', 'RDBN', 'BNRD', 'RDSL', 'SLRD', 'BKBU', 'BUBK', 'BKOG', 'OGBK', 'BKGN', 'GNBK', 'BKBN',
'BNBK', 'BKSL', 'SLBK', 'YWBU', 'BUYW', 'YWOG', 'OGYW', 'YWGN', 'GNYW', 'YWBN', 'BNYW', 'YWSL', 'SLYW',
'VTBU', 'BUVT', 'VTOG', 'OGVT', 'VTGN', 'GNVT', 'VTBN', 'BNVT', 'VTSL', 'SLVT'],
'T568A': ['WHGN', 'GN', 'WHOG', 'BU', 'WHBU', 'OG', 'WHBN', 'BN'],
'T568B': ['WHOG', 'OG', 'WHGN', 'BU', 'WHBU', 'GN', 'WHBN', 'BN'],
} }
color_hex = { default_color = '#ffffff'
# default_bkgnd_color = '#ffffff' # white
default_bknd_color = '#fffbf8' # off-white beige-ish
# Convention: Color names should be 2 letters long, to allow for multicolored wires
shield_color = '#aaaaaa:#84878c' # SN
_color_hex = {
'BK': '#000000', 'BK': '#000000',
'WH': '#ffffff', 'WH': '#ffffff',
'GY': '#999999', 'GY': '#999999',
@ -19,10 +43,17 @@ color_hex = {
'TQ': '#00ffff', 'TQ': '#00ffff',
'BU': '#0066ff', 'BU': '#0066ff',
'VT': '#8000ff', 'VT': '#8000ff',
'BN': '#666600', 'BN': '#a52a2a',
'SL': '#708090',
# Faux-copper look, for bare CU wire
'CU': '#d6775e:#895956',
# Silvery look for tinned bare wire
'SN': '#aaaaaa:#84878c',
# Yellow-green PE wire
'PE': '#54aa85:#f7f854:#54aa85',
} }
color_full = { _color_full = {
'BK': 'black', 'BK': 'black',
'WH': 'white', 'WH': 'white',
'GY': 'grey', 'GY': 'grey',
@ -35,9 +66,13 @@ color_full = {
'BU': 'blue', 'BU': 'blue',
'VT': 'violet', 'VT': 'violet',
'BN': 'brown', 'BN': 'brown',
'SL': 'slate',
'CU': 'bare copper',
'SN': 'tinned copper',
} }
color_ger = { # TODO Help wanted: can someone check the german translation?
_color_ger = {
'BK': 'sw', 'BK': 'sw',
'WH': 'ws', 'WH': 'ws',
'GY': 'gr', 'GY': 'gr',
@ -50,29 +85,52 @@ color_ger = {
'BU': 'bl', 'BU': 'bl',
'VT': 'vi', 'VT': 'vi',
'BN': 'br', 'BN': 'br',
# To the best of my ability, likely incorrect:
# Slate --> Schieferfarbe --> SI ??
'SL': 'si',
# Copper
'CU': 'ku',
# Tinned
'SN': 'si'
} }
def translate_color(inp, color_mode):
if inp == '': def get_color_hex(input):
output = '' if len(input) == 4: # give wires with EXACTLY 2 colors that striped/banded look
else: input = input + input[:2]
if color_mode == 'full': try:
output = color_full[inp].lower() output = ":".join([_color_hex[input[i:i + 2]] for i in range(0, len(input), 2)])
elif color_mode == 'FULL': except KeyError:
output = color_full[inp].upper() print("Unknown Color Specified", file=sys.stderr)
elif color_mode == 'hex': output = default_color
output = color_hex[inp].lower() #raise Exception('Unknown Color Name')
elif color_mode == 'HEX': if input == '':
output = color_hex[inp].upper() output = default_color
elif color_mode == 'ger':
output = color_ger[inp].lower()
elif color_mode == 'GER':
output = color_ger[inp].upper()
elif color_mode == 'short':
output = inp.lower()
elif color_mode == 'SHORT':
output = inp.upper()
else:
raise Exception('Unknown color mode')
return output return output
def translate_color(input, color_mode):
if input == '':
return ''
upper = color_mode.isupper()
if not (color_mode.isupper() or color_mode.islower()):
raise Exception('Unknown color mode capitalization')
color_mode = color_mode.lower()
if color_mode == 'full':
output = "/".join([_color_full[input[i:i+2]] for i in range(0,len(input),2)])
elif color_mode == 'hex':
output = get_color_hex(input)
elif color_mode == 'ger':
output = "".join([_color_ger[input[i:i+2]] for i in range(o,len(input),2)])
elif color_mode == 'short':
output = input
else:
raise Exception('Unknown color mode')
if upper:
return output.upper()
else:
return output.lower()