From 525be74c98fe4087f91587d6533537fb16459538 Mon Sep 17 00:00:00 2001 From: Andrew Katz Date: Wed, 1 Jul 2020 13:58:26 -0400 Subject: [PATCH] Merge multi-color wires into refactored code --- setup.py | 3 +- src/batch.py | 50 -------------- src/{wireviz.py => wireviz.old} | 0 src/wireviz/Harness.py | 31 +++++---- src/wireviz/build_examples.py | 2 +- src/wireviz/wv_colors.py | 112 ++++++++++++++++++++++++-------- 6 files changed, 103 insertions(+), 95 deletions(-) delete mode 100644 src/batch.py rename src/{wireviz.py => wireviz.old} (100%) mode change 100755 => 100644 diff --git a/setup.py b/setup.py index 950e314..ccd09b0 100644 --- a/setup.py +++ b/setup.py @@ -1,4 +1,5 @@ #!/usr/bin/python3 +# -*- coding: utf-8 -*- import os from setuptools import setup, find_packages @@ -42,4 +43,4 @@ setup( 'License :: OSI Approved :: GNU General Public License v3 (GPLv3)', ], -) +) \ No newline at end of file diff --git a/src/batch.py b/src/batch.py deleted file mode 100644 index bf15874..0000000 --- a/src/batch.py +++ /dev/null @@ -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)) diff --git a/src/wireviz.py b/src/wireviz.old old mode 100755 new mode 100644 similarity index 100% rename from src/wireviz.py rename to src/wireviz.old diff --git a/src/wireviz/Harness.py b/src/wireviz/Harness.py index c50384e..46780cf 100644 --- a/src/wireviz/Harness.py +++ b/src/wireviz/Harness.py @@ -39,7 +39,7 @@ class Harness: font = 'arial' dot.attr('graph', rankdir='LR', ranksep='2', - bgcolor='white', + bgcolor=wv_colors.default_bknd_color, nodesep='0.33', fontname=font) dot.attr('node', shape='record', @@ -65,7 +65,6 @@ class Harness: 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'' if connector.color else '' dot.node(key, shape='none', style='filled', @@ -141,7 +140,7 @@ class Harness: html = f'{html}' # attribute row html = f'{html}' # name+attributes table - html = f'{html} ' # spacer between attributes and wires + html = f'{html}' # spacer between attributes and wires html = f'{html}' # conductor table @@ -154,27 +153,29 @@ class Harness: for bla in p: html = f'{html}' html = f'{html}' - bgcolor = wv_colors.translate_color(connection, 'hex') - bgcolor = bgcolor if bgcolor != '' else '#ffffff' - html = f'{html}' - + bgcolors = ('#000000:' + wv_colors.translate_color(connection, 'hex') + ':#000000').split(':') + html = f'{html}' if cable.shield: p = ['', 'Shield', ''] - html = f'{html}' # spacer + html = f'{html}' # spacer html = f'{html}' for bla in p: html = html + f'' html = f'{html}' - html = f'{html}' + html = f'{html}' - html = f'{html}' # spacer at the end + html = f'{html}' # spacer at the end html = f'{html}
{bla}
' + for j, bgcolor in enumerate( + bgcolors[::-1]): # Reverse to match the curved wires when more than 2 colors + html = f'{html}' + html = html + '
 
{bla}
 
' # conductor table html = f'{html}' # main table if cable.notes: html = f'{html}{cable.notes}' # notes table - html = f'{html} ' # spacer at the end + html = f'{html}' # spacer at the end html = f'{html}' # main table @@ -182,12 +183,10 @@ class Harness: for connection in cable.connections: 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] - if search_color in wv_colors.color_hex: - 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') + dot.attr('edge', color='#000000:{wire_color}:#000000'.format( + wire_color=wv_colors.get_color_hex(search_color))) 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 from_ferrule = self.connectors[connection.from_name].category == 'ferrule' diff --git a/src/wireviz/build_examples.py b/src/wireviz/build_examples.py index 078e4e2..64f511d 100755 --- a/src/wireviz/build_examples.py +++ b/src/wireviz/build_examples.py @@ -9,7 +9,7 @@ sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..')) from wireviz import wireviz demos = 2 # 2 -examples = 9 # 9 +examples = 10 # 10 tutorials = 7 # 7 if demos: diff --git a/src/wireviz/wv_colors.py b/src/wireviz/wv_colors.py index 055b467..fe135d7 100644 --- a/src/wireviz/wv_colors.py +++ b/src/wireviz/wv_colors.py @@ -1,13 +1,37 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- +import sys 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'], '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', 'WH': '#ffffff', 'GY': '#999999', @@ -19,10 +43,17 @@ color_hex = { 'TQ': '#00ffff', 'BU': '#0066ff', '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', 'WH': 'white', 'GY': 'grey', @@ -35,9 +66,13 @@ color_full = { 'BU': 'blue', 'VT': 'violet', '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', 'WH': 'ws', 'GY': 'gr', @@ -50,29 +85,52 @@ color_ger = { 'BU': 'bl', 'VT': 'vi', '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 == '': - output = '' - else: - if color_mode == 'full': - output = color_full[inp].lower() - elif color_mode == 'FULL': - output = color_full[inp].upper() - elif color_mode == 'hex': - output = color_hex[inp].lower() - elif color_mode == 'HEX': - output = color_hex[inp].upper() - 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') + +def get_color_hex(input): + if len(input) == 4: # give wires with EXACTLY 2 colors that striped/banded look + input = input + input[:2] + try: + output = ":".join([_color_hex[input[i:i + 2]] for i in range(0, len(input), 2)]) + except KeyError: + print("Unknown Color Specified", file=sys.stderr) + output = default_color + #raise Exception('Unknown Color Name') + if input == '': + output = default_color 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() +