diff --git a/src/input/footer.dot b/src/input/footer.dot new file mode 100644 index 0000000..5c34318 --- /dev/null +++ b/src/input/footer.dot @@ -0,0 +1 @@ +} diff --git a/src/input/header.dot b/src/input/header.dot new file mode 100644 index 0000000..e3f1a79 --- /dev/null +++ b/src/input/header.dot @@ -0,0 +1,4 @@ +digraph G { + graph [rankdir = LR, ranksep=2, fontname = "arial"]; + edge [arrowhead=none, fontname = "arial"]; + node [shape=record, style=rounded, fontname = "arial"]; diff --git a/src/output/output.dot b/src/output/output.dot new file mode 100644 index 0000000..0014b97 --- /dev/null +++ b/src/output/output.dot @@ -0,0 +1,17 @@ +digraph G { + graph [rankdir = LR, ranksep=2, fontname = "arial"]; + edge [arrowhead=none, fontname = "arial"]; + node [shape=record, style=rounded, fontname = "arial"]; + + +X1[label="X1 | {{DCD|RX|TX|DTR|GND|DSR|RTS|CTS|RI} | {1|2|3|4|5|6|7|8|9}}}"] +X2[label="X2 | {{1|2|3|4|5|6} | {|||||}}}"] +W1[label="W1 | {{1|2|3|4} | {WH|BN|GN|Shield} | {1|2|3|4}}}"] + +{edge[style=bold]{X1:p2 -> W1:w1i; W1:w1o -> X2:p1} +{X1:p3 -> W1:w2i; W1:w2o -> X2:p3} +{X1:p5 -> W1:w3i; W1:w3o -> X2:p2} +} + + +} diff --git a/src/test.py b/src/test.py index 358be4c..26e3d4a 100644 --- a/src/test.py +++ b/src/test.py @@ -1,20 +1,32 @@ import wireviz -PINOUT_I2C = ("GND","VCC","SCL","SDA") +PINOUT_SERIAL = ('DCD','RX','TX','DTR','GND','DSR','RTS','CTS','RI') COLORS_WEIRD = ("infrared","ultraviolet","transparent","invisible") -X1 = wireviz.Node("X1", num_pins=4) -X2 = wireviz.Node("X2", pinout=PINOUT_I2C) -X3 = wireviz.Node("X3", pinout=PINOUT_I2C) +X1 = wireviz.Node("X1", pinout=PINOUT_SERIAL, ports_right=True) +X2 = wireviz.Node("X2", num_pins=6, ports_left=True) -W1 = wireviz.Cable("W1", num_wires=4) -W2 = wireviz.Cable("W2", num_wires=4, color_code="DIN") -W3 = wireviz.Cable("W3", num_wires=3, colors=COLORS_WEIRD) +W1 = wireviz.Cable("W1", num_wires=3, color_code="DIN", shield=True) -print(X1) -print(X2) -print(X3) +W1.connect(X1,(2,3,5),(1,2,3),X2,(1,3,2)) -print(W1) -print(W2) -print(W3) +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') + + f.write(X1.graphviz() + '\n') + f.write(X2.graphviz() + '\n') + f.write(W1.graphviz() + '\n') + + + f.write('\n\n') + with open('input/footer.dot','r') as infile: + for line in infile: + f.write(line) + +# print output file +# with open('output/output.dot','r') as f: +# for line in f: +# print(line) diff --git a/src/wireviz.py b/src/wireviz.py index 9bae181..b5d213f 100644 --- a/src/wireviz.py +++ b/src/wireviz.py @@ -4,8 +4,10 @@ COLOR_CODE_IEC = ['BN','RD','OG','YE','GN','BU','VT','GY','WH','BK'] class Node: - def __init__(self, name, num_pins=None, pinout=None): + def __init__(self, name, num_pins=None, pinout=None, ports_left=False, ports_right=False): self.name = name + self.ports_left = ports_left + self.ports_right = ports_right if pinout is None: self.pinout = ("",) * num_pins @@ -19,10 +21,43 @@ class Node: 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 + s = s + '{name}[label="{name} | {{'.format(name=self.name) + # print pinout + if self.ports_left == True: + s = s + '{' + l = [] + for i,x in enumerate(self.pinout,1): + l.append('{portno}'.format(portno=i)) + s = s + '|'.join(l) + s = s + '} | ' + + s = s + '{' + s = s + '|'.join(self.pinout) + s = s + '}' + + if self.ports_right == True: + s = s + ' | {' + l = [] + for i,x in enumerate(self.pinout,1): + l.append('{portno}'.format(portno=i)) + s = s + '|'.join(l) + s = s + '}' + + s = s + '}}"]' + + return s + class Cable: - def __init__(self, name, num_wires=None, colors=None, color_code=None): + def __init__(self, name, num_wires=None, colors=None, color_code=None, shield=False): self.name = name + self.connections = [] if color_code is None and colors is None: self.colors = ("",) * num_wires else: @@ -42,15 +77,61 @@ class Cable: self.colors = colors else: self.colors = colors[:num_wires] + if shield == True: + self.colors = self.colors + ('Shield',) + + def connect(self, from_name, from_pin, via, to_name, to_pin): + if len(from_pin) != len(to_pin): + raise Exception("from_pin must have the same number of elements as to_pin") + for i, x in enumerate(from_pin): + self.connections.append((from_name, from_pin[i], via[i], to_name, to_pin[i])) def __repr__(self): - return "{} = {} {}".format(self.name, len(self.colors), self.colors) + return "{} = {} {}\n {}".format(self.name, len(self.colors), self.colors, self.connections) + def debug(self): + print(self.name) + print(self.colors) + if len(self.connections) > 0: + for i, x in enumerate(self.connections): + if i < len(self.colors): + s = self.colors[int(x[2]-1)] + else: + s = '--' + # print(self.colors(x[2]) if i < len(self.colors) else '-') + print("{}:{} -- {}({}) -> {}:{}".format(x[0],x[1],x[2],s,x[3],x[4])) + def graphviz(self): + s = '' + # print header + s = s + '{name}[label="{name} | {{'.format(name=self.name) + # print pinout + s = s + '{' + l = [] + for i,x in enumerate(self.colors,1): + l.append('{wireno}'.format(wireno=i)) + s = s + '|'.join(l) + s = s + '} | ' -# class ClassName(object): -# """docstring for .""" -# -# def __init__(self, arg): -# super(, self).__init__() -# self.arg = arg + s = s + '{' + s = s + '|'.join(self.colors) + s = s + '}' + + s = s + ' | {' + l = [] + for i,x in enumerate(self.colors,1): + l.append('{wireno}'.format(wireno=i)) + s = s + '|'.join(l) + s = s + '}' + + s = s + '}}"]' + + s = s + '\n\n{edge[style=bold]' + for x in self.connections: + s = s + '{' + t = '{from_name}:p{from_port} -> {via_name}:w{via_wire}i; {via_name}:w{via_wire}o -> {to_name}:p{to_port}'.format(from_name=x[0],from_port=x[1],via_name=self.name, via_wire=x[2],to_name=x[3],to_port=x[4]) + s = s + t + s = s + '}\n' + s = s + '}' + + return s