From 548adcdfb7dae277b175d64f1a44a8b0e66543ab Mon Sep 17 00:00:00 2001 From: Daniel Rojas Date: Fri, 22 May 2020 18:25:24 +0200 Subject: [PATCH] Add Harness class, simplify harness creation --- src/example1.py | 20 ++++++------- src/example2.py | 51 ++++++++++++++++----------------- src/example3.py | 25 ++++++++-------- src/wireviz.py | 76 ++++++++++++++++++++++++++----------------------- 4 files changed, 88 insertions(+), 84 deletions(-) diff --git a/src/example1.py b/src/example1.py index 51e8472..59463d3 100644 --- a/src/example1.py +++ b/src/example1.py @@ -1,14 +1,14 @@ -import wireviz +from wireviz import Harness, Node, Cable -X1 = wireviz.Node('X1', type='D-Sub', gender='female', pinout=('DCD','RX','TX','DTR','GND','DSR','RTS','CTS','RI'), ports_right=True) -X2 = wireviz.Node('X2', type='Molex KK 254', gender='female', pinout=('GND','RX','TX','NC','OUT','IN'), ports_left=True) -W1 = wireviz.Cable('W1', mm2=0.25, length=0.2, show_name=True, show_pinout=True, num_wires=3, color_code='DIN', shield=True) +Harness = Harness() + +Harness.add(Cable('W1', mm2=0.25, length=0.2, show_name=True, show_pinout=True, num_wires=3, color_code='DIN', shield=True)) +Harness.add(Node('X1', type='D-Sub', gender='female', pinout=('DCD','RX','TX','DTR','GND','DSR','RTS','CTS','RI'), ports_right=True)) +Harness.add(Node('X2', type='Molex KK 254', gender='female', pinout=('GND','RX','TX','NC','OUT','IN'), ports_left=True)) # Option 1: define wires and shield in one line -# W1.connect(X1,(5,2,3,5),(1,2,3,'s'),X2,(1,3,2,None)) +Harness.objects['W1'].connect('X1',(5,2,3,5),(1,2,3,'s'),'X2',(1,3,2,None)) # Option 2: define wires and shield separately -W1.connect(X1,(5,2,3),'auto',X2,(1,3,2)) # wires -W1.connect(X1,(5,),('s',),X2,(None,)) # shield -X2.loop(5,6) -objects = [X1, X2, W1] +# Harness.objects['W1'].connect('X1',(5,2,3),'auto','X2',(1,3,2)) # wires +# Harness.objects['W1'].connect('X1',(5,),('s',),'X2',(None,)) # shield -wireviz.output(objects) +Harness.graphviz() diff --git a/src/example2.py b/src/example2.py index 25de389..cdb9603 100644 --- a/src/example2.py +++ b/src/example2.py @@ -1,32 +1,31 @@ -import wireviz +from wireviz import Harness, Node, Cable +# shortcuts for use during harness creation PINOUT_I2C = ('GND','+5V','SCL','SDA') COLORS_I2C = ('BK', 'RD', 'YE', 'GN') - PINOUT_SPI_DATAONLY = ('MISO','MOSI','SCK') -X1 = wireviz.Node('X1',type='Molex KK 254', gender='female', pinout=( -'GND', -'+5V', -'SCL', -'SDA', -'MISO', -'MOSI', -'SCK', -'N/C' -), ports_right=True) -X2 = wireviz.Node('X2', type='Molex KK 254', gender='female', pinout=PINOUT_I2C, ports_left=True) -X3 = wireviz.Node('X3', type='Molex KK 254', gender='female', pinout=PINOUT_I2C, ports_left=True) -X4 = wireviz.Node('X4', type='Molex KK 254', gender='female', pinout=('GND','+12V')+PINOUT_SPI_DATAONLY, ports_left=True) -X5 = wireviz.Node('X5', type='Molex Micro-Fit', gender='male', pinout=('GND','+12V'), ports_right=True) -W1 = wireviz.Cable('W1', mm2=0.14, length=0.2, colors=COLORS_I2C) -W2 = wireviz.Cable('W2', mm2=0.14, length=0.2, colors=COLORS_I2C) -W3 = wireviz.Cable('W3', mm2=0.14, length=0.2, colors=('BK','BU','OG','VT')) -W4 = wireviz.Cable('W4', mm2=0.5, length=0.35, colors=('BK','RD')) -W1.connect(X1,(1,2,3,4),'auto',X2,'auto') -W2.connect(X1,(1,2,3,4),'auto',X3,'auto') -W3.connect(X1,(1,5,6,7),'auto',X4,(1,3,4,5)) -W4.connect(X5,'auto','auto',X4,'auto') -objects = [X1, X2, X3, X4, X5, W1, W2, W3, W4] +Harness = Harness() -wireviz.output(objects) +Harness.add(Node('X1',type='Molex KK 254', gender='female', pinout=('GND', + '+5V', + 'SCL', + 'SDA', + 'MISO', + 'MOSI', + 'SCK', + 'N/C'), ports_right=True)) +Harness.add(Node('X2', type='Molex KK 254', gender='female', pinout=PINOUT_I2C, ports_left=True)) +Harness.add(Node('X3', type='Molex KK 254', gender='female', pinout=PINOUT_I2C, ports_left=True)) +Harness.add(Node('X4', type='Molex KK 254', gender='female', pinout=('GND','+12V')+PINOUT_SPI_DATAONLY, ports_left=True)) +Harness.add(Node('X5', type='Molex Micro-Fit', gender='male', pinout=('GND','+12V'), ports_right=True)) +Harness.add(Cable('W1', mm2=0.14, length=0.2, colors=COLORS_I2C)) +Harness.add(Cable('W2', mm2=0.14, length=0.2, colors=COLORS_I2C)) +Harness.add(Cable('W3', mm2=0.14, length=0.2, colors=('BK','BU','OG','VT'))) +Harness.add(Cable('W4', mm2=0.5, length=0.35, colors=('BK','RD'))) +Harness.objects['W1'].connect('X1',(1,2,3,4),'auto','X2','auto') +Harness.objects['W2'].connect('X1',(1,2,3,4),'auto','X3','auto') +Harness.objects['W3'].connect('X1',(1,5,6,7),'auto','X4',(1,3,4,5)) +Harness.objects['W4'].connect_all_straight('X5','X4') + +Harness.graphviz() diff --git a/src/example3.py b/src/example3.py index 06a09dd..43080ca 100644 --- a/src/example3.py +++ b/src/example3.py @@ -1,15 +1,16 @@ -import wireviz +from wireviz import Harness, Node, Cable -wireviz.color_mode = 'full' # short/SHORT/full/FULL/hex/HEX +Harness = Harness() +Harness.color_mode = 'full' -X1 = wireviz.Node('X1', num_pins=10, ports_right=True) -X2 = wireviz.Node('X2', num_pins=10, ports_left=True) -W1 = wireviz.Cable('W1', num_wires=10, color_code='IEC') -W1.connect_all_straight(X1,X2) -X3 = wireviz.Node('X3', num_pins=10, ports_right=True) -X4 = wireviz.Node('X4', num_pins=10, ports_left=True) -W2 = wireviz.Cable('W2', num_wires=10, color_code='DIN') -W2.connect_all_straight(X3,X4) -objects = [X1, X2, W1, X3, X4, W2] +Harness.add(Node('X1', num_pins=10, ports_right=True)) +Harness.add(Node('X2', num_pins=10, ports_left=True)) +Harness.add(Cable('W1', num_wires=10, color_code='IEC')) +Harness.objects['W1'].connect_all_straight('X1','X2') -wireviz.output(objects) +Harness.add(Node('X3', num_pins=10, ports_right=True)) +Harness.add(Node('X4', num_pins=10, ports_left=True)) +Harness.add(Cable('W2', num_wires=10, color_code='DIN')) +Harness.objects['W2'].connect_all_straight('X3','X4') + +Harness.graphviz() diff --git a/src/wireviz.py b/src/wireviz.py index 254bdad..9c12fd0 100644 --- a/src/wireviz.py +++ b/src/wireviz.py @@ -32,7 +32,38 @@ color_full = { 'BN': 'brown', } -color_mode = 'SHORT' +class Harness: + + def __init__(self): + self.color_mode = 'SHORT' + self.objects = {} + + def add(self, object): + self.objects[object.name] = object + self.objects[object.name].color_mode = self.color_mode + + def debug(self): + print(self.objects) + + def graphviz(self, print_to_screen=False): + with open('output/output.dot','w') as f: + with open('input/header.dot','r') as infile: + for line in infile: + f.write(line) + f.write('\n\n') + + for o in self.objects: + f.write(self.objects[o].graphviz() + '\n') + + f.write('\n\n') + with open('input/footer.dot','r') as infile: + for line in infile: + f.write(line) + + if print_to_screen == True: + with open('output/output.dot','r') as f: + for line in f: + print(line) class Node: @@ -44,6 +75,7 @@ class Node: self.ports_left = ports_left self.ports_right = ports_right self.loops = [] + self.color_mode = 'SHORT' if pinout is None: self.pinout = ('',) * num_pins @@ -66,12 +98,6 @@ class Node: loop_side = side self.loops.append((from_pin, to_pin, loop_side)) - def __repr__(self): - return '{} = {} {}'.format(self.name, len(self.pinout), self.pinout) - - def __str__(self): - return '{}'.format(self.name) - def graphviz(self): s = '' # print header @@ -137,6 +163,7 @@ class Cable: self.show_pinout = show_pinout self.shield = shield self.connections = [] + self.color_mode = 'SHORT' if color_code is None and colors is None: self.colors = ('',) * num_wires else: @@ -172,9 +199,6 @@ class Cable: def connect_all_straight(self, from_name, to_name): self.connect(from_name, 'auto', 'auto', to_name, 'auto') - def __repr__(self): - return '{} = {} {}\n {}'.format(self.name, len(self.colors), self.colors, self.connections) - def debug(self): print(self.name) print(self.colors) @@ -231,17 +255,17 @@ class Cable: else: l = [] for i,x in enumerate(self.colors,1): - if color_mode == 'full': + if self.color_mode == 'full': x = color_full[x].lower() - elif color_mode == 'FULL': + elif self.color_mode == 'FULL': x = color_hex[x].upper() - elif color_mode == 'hex': + elif self.color_mode == 'hex': x = color_hex[x].lower() - elif color_mode == 'HEX': + elif self.color_mode == 'HEX': x = color_hex[x].upper() - elif color_mode == 'short': + elif self.color_mode == 'short': x = x.lower() - elif color_mode == 'SHORT': + elif self.color_mode == 'SHORT': x = x.upper() else: raise Exception('Unknown color mode') @@ -281,23 +305,3 @@ class Cable: s = s + '}' return s - -def output(objects, print_to_screen=False): - with open('output/output.dot','w') as f: - with open('input/header.dot','r') as infile: - for line in infile: - f.write(line) - f.write('\n\n') - - for o in objects: - f.write(o.graphviz() + '\n') - - f.write('\n\n') - with open('input/footer.dot','r') as infile: - for line in infile: - f.write(line) - - if print_to_screen == True: - with open('output/output.dot','r') as f: - for line in f: - print(line)