Add GraphViz output functionality
This commit is contained in:
parent
072958d1a3
commit
48e744a0ce
1
src/input/footer.dot
Normal file
1
src/input/footer.dot
Normal file
@ -0,0 +1 @@
|
||||
}
|
||||
4
src/input/header.dot
Normal file
4
src/input/header.dot
Normal file
@ -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"];
|
||||
17
src/output/output.dot
Normal file
17
src/output/output.dot
Normal file
@ -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} | {<p1>1|<p2>2|<p3>3|<p4>4|<p5>5|<p6>6|<p7>7|<p8>8|<p9>9}}}"]
|
||||
X2[label="X2 | {{<p1>1|<p2>2|<p3>3|<p4>4|<p5>5|<p6>6} | {|||||}}}"]
|
||||
W1[label="W1 | {{<w1i>1|<w2i>2|<w3i>3|<w4i>4} | {WH|BN|GN|Shield} | {<w1o>1|<w2o>2|<w3o>3|<w4o>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}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
38
src/test.py
38
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)
|
||||
|
||||
@ -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('<p{portno}>{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('<p{portno}>{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('<w{wireno}i>{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('<w{wireno}o>{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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user