Add a simple visualisation using graphviz

visu_broken
LEdoian 2 years ago
parent 7f783cdbfa
commit 31545b64ef

@ -1,4 +1,8 @@
from birdvisu import maps from birdvisu import maps
from pprint import pprint
import sys
import subprocess
from io import StringIO
with open('current.ospf') as actual: with open('current.ospf') as actual:
act_topo = maps.Topology.from_ospffile(actual) act_topo = maps.Topology.from_ospffile(actual)
@ -8,6 +12,34 @@ with open('reference.ospf') as reference:
diff = maps.TopologyDifference(act_topo, ref_topo) diff = maps.TopologyDifference(act_topo, ref_topo)
diff.compare() diff.compare()
from pprint import pprint def visualize_graphviz(diff):
graph = StringIO()
# Prologue
graph.write('strict graph "OSPF Visualization" {\n')
for r, n, m in set(diff.reference.links + diff.actual.links):
graph.write(f'"{r}" -- "{n}" [label="{m}"];\n')
for r in diff.routers_missing | diff.networks_missing:
graph.write(f'"{r}" [color=red];\n')
for r in diff.routers_extra | diff.networks_extra:
graph.write(f'"{r}" [color=green];\n')
for r, n, _ in diff.links_missing:
graph.write(f'"{r}" -- "{n}" [color=red];\n')
for r, n, _ in diff.links_extra:
graph.write(f'"{r}" -- "{n}" [color=green];\n')
# Epilogue:
graph.write('}\n')
# Show:
gv = subprocess.Popen(['dot', '-Tgtk', '-Kfdp'], text=True, stdin=subprocess.PIPE)
gv.communicate(graph.getvalue())
gv.wait()
if len(sys.argv) == 1:
pprint(vars(diff)) pprint(vars(diff))
elif 'dot' in sys.argv:
visualize_graphviz(diff)

Loading…
Cancel
Save