diff --git a/examples/example1.yml b/examples/example1.yml
index 05a5877..7398666 100644
--- a/examples/example1.yml
+++ b/examples/example1.yml
@@ -3,7 +3,6 @@ nodes:
type: D-Sub
gender: female
pinout: [DCD, RX, TX, DTR, GND, DSR, RTS, CTS, RI]
- random: yes
X2:
type: Molex KK 254
gender: female
diff --git a/examples/ferrules.yml b/examples/ferrules.yml
new file mode 100644
index 0000000..9fb8319
--- /dev/null
+++ b/examples/ferrules.yml
@@ -0,0 +1,36 @@
+nodes:
+ X1:
+ type: D-Sub
+ gender: female
+ num_pins: 4
+ X2:
+ type: Molex KK 254
+ gender: female
+ num_pins: 3
+
+wires:
+ W1:
+ mm2: 0.25
+ length: 0.2
+ color_code: IEC
+ num_wires: 10
+ shield: true
+
+ferrules:
+ F_test:
+ type: crimp
+
+connections:
+ -
+ - X1: [1-3]
+ - W1: [1-3]
+ - X2: [1-3]
+ -
+ - X1: 4
+ - W1: s
+ -
+ - F_test
+ - W1: [4-10]
+ -
+ - W1: [10-4]
+ - F_test
diff --git a/src/wireviz.py b/src/wireviz.py
index 55b5d0a..fa47aea 100644
--- a/src/wireviz.py
+++ b/src/wireviz.py
@@ -1,3 +1,5 @@
+from dataclasses import dataclass, field
+from typing import Any, List
from graphviz import Graph
COLOR_CODES = {'DIN': ['WH','BN','GN','YE','GY','PK','BU','RD','BK','VT'], # ,'GYPK','RDBU','WHGN','BNGN','WHYE','YEBN','WHGY','GYBN','WHPK','PKBN'],
@@ -8,14 +10,14 @@ COLOR_CODES = {'DIN': ['WH','BN','GN','YE','GY','PK','BU','RD','BK','VT'], # ,'G
color_hex = {
'BK': '#000000',
'WH': '#ffffff',
- 'GY': '#808080',
- 'PK': '#ff80c0',
+ 'GY': '#999999',
+ 'PK': '#ff66cc',
'RD': '#ff0000',
'OG': '#ff8000',
'YE': '#ffff00',
- 'GN': '#00ff00',
+ 'GN': '#009900',
'TQ': '#00ffff',
- 'BU': '#0000ff',
+ 'BU': '#0066ff',
'VT': '#8000ff',
'BN': '#666600',
}
@@ -57,11 +59,11 @@ class Harness:
self.nodes = {}
self.cables = {}
- def add_node(self, name, type=None, gender=None, show_name=True, num_pins=None, show_num_pins=True, pinout=None, ports_left=False, ports_right=False):
- self.nodes[name] = Node(name, type, gender, show_name, num_pins, show_num_pins, pinout, ports_left, ports_right)
+ def add_node(self, name, *args, **kwargs):
+ self.nodes[name] = Node(name, *args, **kwargs)
- def add_cable(self, name, mm2=None, awg=None, show_equiv=False, length=0, show_name=False, show_pinout=False, num_wires=None, show_num_wires=True, colors=None, color_code=None, shield=False):
- self.cables[name] = Cable(name, mm2, awg, show_equiv, length, show_name, show_pinout, num_wires, show_num_wires, colors, color_code, shield)
+ def add_cable(self, name, *args, **kwargs):
+ self.cables[name] = Cable(name, *args, **kwargs)
def loop(self, node_name, from_pin, to_pin):
self.nodes[node_name].loop(from_pin, to_pin)
@@ -82,38 +84,36 @@ class Harness:
dot.attr('edge', style='bold', fontname=font)
# prepare ports on connectors depending on which side they will connect
- for k in self.cables:
- c = self.cables[k]
+ for k, c in self.cables.items():
for x in c.connections:
if x[1] is not None: # connect to left
self.nodes[x[0]].ports_right = True
if x[4] is not None: # connect to right
self.nodes[x[3]].ports_left = True
- for k in self.nodes:
- n = self.nodes[k]
+ for k, n in self.nodes.items():
# a = attributes
a = [n.type,
n.gender,
- '{}-pin'.format(len(n.pinout)) if n.show_num_pins == True else '']
+ '{}-pin'.format(len(n.pinout)) if n.show_num_pins else '']
# p = pinout
p = [[],[],[]]
p[1] = list(n.pinout)
- for i,x in enumerate(n.pinout, 1):
- if n.ports_left == True:
+ for i, x in enumerate(n.pinout, 1):
+ if n.ports_left:
p[0].append('
{portno}'.format(portno=i))
- if n.ports_right == True:
+ if n.ports_right:
p[2].append('
{portno}'.format(portno=i))
# l = label
- l = [n.name if n.show_name == True else '', a, p]
+ l = [n.name if n.show_name else '', a, p]
dot.node(k, label=nested(l))
if len(n.loops) > 0:
dot.attr('edge',color='#000000')
- if n.ports_left == True:
+ if n.ports_left:
loop_side = 'l'
loop_dir = 'w'
- elif n.ports_right == True:
+ elif n.ports_right:
loop_side = 'r'
loop_dir = 'e'
else:
@@ -122,24 +122,23 @@ class Harness:
dot.edge('{name}:p{port_from}{loop_side}:{loop_dir}'.format(name=n.name, port_from=x[0], port_to=x[1], loop_side=loop_side, loop_dir=loop_dir),
'{name}:p{port_to}{loop_side}:{loop_dir}'.format(name=n.name, port_from=x[0], port_to=x[1], loop_side=loop_side, loop_dir=loop_dir))
- for k in self.cables:
- c = self.cables[k]
+ for k, c in self.cables.items():
# a = attributes
- a = ['{}x'.format(len(c.colors)) if c.show_num_wires == True else '',
- '{} mm\u00B2{}'.format(c.mm2, ' ({} AWG)'.format(awg_equiv(c.mm2)) if c.show_equiv == True else '') if c.mm2 is not None else '',
+ a = ['{}x'.format(len(c.colors)) if c.show_num_wires else '',
+ '{} mm\u00B2{}'.format(c.mm2, ' ({} AWG)'.format(awg_equiv(c.mm2)) if c.show_equiv else '') if c.mm2 is not None else '',
c.awg,
- '+ S' if c.shield == True else '',
+ '+ S' if c.shield else '',
'{} m'.format(c.length) if c.length > 0 else '']
# p = pinout
p = [[],[],[]]
- for i,x in enumerate(c.colors,1):
+ for i, x in enumerate(c.colors,1):
if c.show_pinout:
p[0].append('{wireno}'.format(wireno=i))
p[1].append('{wirecolor}'.format(wirecolor=translate_color(x, self.color_mode)))
p[2].append('{wireno}'.format(wireno=i))
else:
p[1].append('{wirecolor}'.format(wireno=i,wirecolor=translate_color(x, self.color_mode)))
- if c.shield == True:
+ if c.shield:
if c.show_pinout:
p[0].append('')
p[1].append('Shield')
@@ -147,7 +146,7 @@ class Harness:
else:
p[1].append('Shield')
# l = label
- l = [c.name if c.show_name == True else '', a, p]
+ l = [c.name if c.show_name else '', a, p]
dot.node(k, label=nested(l))
# connections
@@ -162,10 +161,10 @@ class Harness:
dot.attr('edge',color='#000000')
if x[1] is not None: # connect to left
dot.edge('{from_name}:p{from_port}r'.format(from_name=x[0],from_port=x[1]),
- '{via_name}:w{via_wire}{via_subport}'.format(via_name=c.name, via_wire=x[2], via_subport='i' if c.show_pinout == True else ''))
+ '{via_name}:w{via_wire}{via_subport}'.format(via_name=c.name, via_wire=x[2], via_subport='i' if c.show_pinout else ''))
# self.nodes[x[0]].ports_right = True
if x[4] is not None: # connect to right
- dot.edge('{via_name}:w{via_wire}{via_subport}'.format(via_name=c.name, via_wire=x[2], via_subport='o' if c.show_pinout == True else ''),
+ dot.edge('{via_name}:w{via_wire}{via_subport}'.format(via_name=c.name, via_wire=x[2], via_subport='o' if c.show_pinout else ''),
'{to_name}:p{to_port}l'.format(to_name=x[3], to_port=x[4]))
# self.nodes[x[3]].ports_left = True
@@ -178,74 +177,73 @@ class Harness:
d.render(filename=filename, directory=directory, view=view, cleanup=cleanup)
d.save(filename='{}.gv'.format(filename), directory=directory)
+@dataclass
class Node:
+ name: str
+ type: str = None
+ gender: str = None
+ num_pins: int = None
+ pinout: List[Any] = field(default_factory=list)
+ show_name: bool = False
+ show_num_pins: bool = False
- def __init__(self, name, type=None, gender=None, show_name=True, num_pins=None, show_num_pins=True, pinout=None, ports_left=False, ports_right=False):
- self.name = name
- self.type = type
- self.gender = gender
- self.show_name = show_name
- self.show_num_pins = show_num_pins
- self.ports_left = ports_left
- self.ports_right = ports_right
+ def __post_init__(self):
+ self.ports_left = False
+ self.ports_right = False
self.loops = []
- if pinout is None:
- if num_pins is None:
- num_pins = 1
- self.pinout = ('',) * num_pins
+ if self.pinout:
+ if self.num_pins is not None:
+ raise Exception('You cannot specify both pinout and num_pins')
else:
- if num_pins is None:
- if pinout is None:
- raise Exception('Must provide num_pins or pinout')
- else:
- self.pinout = pinout
+ if not self.num_pins:
+ self.num_pins = 1
+ self.pinout = ['',] * self.num_pins
def loop(self, from_pin, to_pin):
self.loops.append((from_pin, to_pin))
+@dataclass
class Cable:
+ name: str
+ mm2: float = None
+ awg: int = None
+ show_equiv: bool = False
+ length: float = 0
+ num_wires: int = None
+ shield: bool = False
+ colors: List[Any] = field(default_factory=list)
+ color_code: str = None
+ show_name: bool = False
+ show_pinout: bool = False
+ show_num_wires: bool = True
- def __init__(self, name, mm2=None, awg=None, show_equiv=False, length=0, show_name=False, show_pinout=False, num_wires=None, show_num_wires=True, colors=None, color_code=None, shield=False):
- self.name = name
- if mm2 is not None and awg is not None:
+ def __post_init__(self):
+ if self.mm2 and self.awg:
raise Exception('You cannot define both mm2 and awg!')
- self.mm2 = mm2
- self.awg = awg
- self.show_equiv = show_equiv
- self.length = length
- self.show_name = show_name
- self.show_pinout = show_pinout
- self.show_num_wires = show_num_wires
- self.shield = shield
self.connections = []
- if color_code is None and colors is None:
- self.colors = ('',) * num_wires
- else:
- if colors is None: # no custom color pallet was specified
- if num_wires is None:
- raise Exception('Unknown number of wires')
- else:
- if color_code is None:
- raise Exception('No color code')
- # choose color code
- if color_code not in COLOR_CODES:
- raise Exception('Unknown color code')
- else:
- cc = COLOR_CODES[color_code]
- n = num_wires
- else: # custom color pallet was specified
- cc = colors
- if num_wires is None: # assume number of wires = number of items in custom pallet
- n = len(cc)
- else: # number of wires was specified
- n = num_wires
- cc = tuple(cc)
- if n > len(cc):
- m = num_wires // len(cc) + 1
- cc = cc * int(m)
- self.colors = cc[:n]
+ if self.num_wires: # number of wires explicitly defined
+ if self.colors: # use custom color palette (partly or looped if needed)
+ pass
+ elif self.color_code: # use standard color palette (partly or looped if needed)
+ if self.color_code not in COLOR_CODES:
+ raise Exception('Unknown color code')
+ self.colors = COLOR_CODES[self.color_code]
+ else: # no colors defined, add dummy colors
+ self.colors = [''] * self.num_wires
+
+ # make color code loop around if more wires than colors
+ if self.num_wires > len(self.colors):
+ m = self.num_wires // len(self.colors) + 1
+ self.colors = self.colors * int(m)
+ # cut off excess after looping
+ self.colors = self.colors[:self.num_wires]
+
+ else: # num_wires implicit in length of color list
+ if not self.colors:
+ raise Exception('Unknown number of wires. Must specify num_wires or colors (implicit length)')
+ self.num_wires = len(self.colors)
def connect(self, from_name, from_pin, via_pin, to_name, to_pin):
from_pin = int2tuple(from_pin)
diff --git a/src/yaml2wireviz.py b/src/yaml2wireviz.py
index 6da74f1..b922d4e 100644
--- a/src/yaml2wireviz.py
+++ b/src/yaml2wireviz.py
@@ -1,10 +1,12 @@
import yaml
import wireviz
+filename = '../examples/example1.yml'
filename = '../examples/example2.yml'
+filename = '../examples/ferrules.yml'
def check_designators(what, where):
- for i,x in enumerate(what):
+ for i, x in enumerate(what):
# print('Looking for {} in {}'.format(x,where[i]))
if x not in input[where[i]]:
return False
@@ -45,24 +47,33 @@ with open(filename, 'r') as stream:
print(exc)
h = wireviz.Harness()
-# add nodes
-for k, o in input['nodes'].items():
- h.add_node(k, type=o.get('type'),
- gender=o.get('gender'),
- num_pins=o.get('num_pins'),
- pinout=o.get('pinout'))
-# add wires
-for k, o in input['wires'].items():
- h.add_cable(k, mm2=o.get('mm2'),
- awg=o.get('awg'),
- length=o.get('length'),
- num_wires=o.get('num_wires'),
- colors=o.get('colors'),
- color_code=o.get('color_code'),
- shield=o.get('shield'))
+
+# add items
+sections = ['nodes','wires','ferrules','connections']
+types = [dict, dict, dict, list]
+for sec, ty in zip(sections, types):
+ if sec in input and type(input[sec]) == ty:
+ if len(input[sec]) > 0:
+ if ty == dict:
+ for k, o in input[sec].items():
+ if sec == 'nodes':
+ h.add_node(name=k, **o)
+ elif sec == 'wires':
+ h.add_cable(name=k, **o)
+ elif sec == 'ferrules':
+ pass
+ else:
+ print('{} section empty'.format(sec))
+ else:
+ print('No {} section found'.format(sec))
+ if ty == dict:
+ input[sec] = {}
+ elif ty == list:
+ input[sec] = []
+
# add connections
-conlist = input['connections']
-for con in conlist:
+ferrule_counter = 0
+for con in input['connections']:
if len(con) == 3: # format: connector -- wire -- conector
for c in con:
@@ -89,38 +100,79 @@ for con in conlist:
elif len(con) == 2:
for c in con:
- if len(list(c.keys())) != 1: # check that each entry in con has only one key, which is the designator
- raise Exception('Too many keys')
+ if type(c) is dict:
+ if len(list(c.keys())) != 1: # check that each entry in con has only one key, which is the designator
+ raise Exception('Too many keys')
+
+ # hack to make the format for ferrules compatible with the formats for connectors and wires
+ if type(con[0]) == str:
+ name = con[0]
+ con[0] = {}
+ con[0][name] = name
+ if type(con[1]) == str:
+ name = con[1]
+ con[1] = {}
+ con[1][name] = name
from_name = list(con[0].keys())[0]
- to_name = list(con[1].keys())[0]
+ to_name = list(con[1].keys())[0]
n_w = check_designators([from_name, to_name],('nodes','wires'))
w_n = check_designators([from_name, to_name],('wires','nodes'))
n_n = check_designators([from_name, to_name],('nodes','nodes'))
- if not n_w and not w_n and not n_n:
+
+ f_w = check_designators([from_name, to_name],('ferrules','wires'))
+ w_f = check_designators([from_name, to_name],('wires','ferrules'))
+
+ if not n_w and not w_n and not n_n and not f_w and not w_f:
raise Exception('Wrong designators')
from_pins = expand(con[0][from_name])
to_pins = expand(con[1][to_name])
- if len(from_pins) != len(to_pins):
- raise Exception('List length mismatch')
+ if n_w or w_n or n_n:
+ if len(from_pins) != len(to_pins):
+ raise Exception('List length mismatch')
- if n_w == True or w_n == True:
+ if n_w or w_n:
for (from_pin, to_pin) in zip(from_pins, to_pins):
if n_w:
h.connect(from_name, from_pin, to_name, to_pin, None, None)
else: # w_n
h.connect(None, None, from_name, from_pin, to_name, to_pin)
- elif n_n == True:
+ elif n_n:
con_name = list(con[0].keys())[0]
from_pins = expand(con[0][from_name])
to_pins = expand(con[1][to_name])
for (from_pin, to_pin) in zip(from_pins, to_pins):
h.loop(con_name, from_pin, to_pin)
+ if f_w or w_f:
+ from_pins = expand(con[0][from_name])
+ to_pins = expand(con[1][to_name])
+
+ if f_w:
+ ferrule_name = from_name
+ wire_name = to_name
+ wire_pins = to_pins
+ else:
+ ferrule_name = to_name
+ wire_name = from_name
+ wire_pins = from_pins
+
+ ferrule_params = input['ferrules'][ferrule_name]
+ for wire_pin in wire_pins:
+ ferrule_counter = ferrule_counter + 1
+ ferrule_id = 'F{}'.format(ferrule_counter)
+ h.add_node(ferrule_id, **ferrule_params)
+
+ if f_w:
+ h.connect(ferrule_id, 1, wire_name, wire_pin, None, None)
+ else:
+ h.connect(None, None, wire_name, wire_pin, ferrule_id, 1)
+
+
else:
raise Exception('Wrong number of connection parameters')