diff --git a/snippets/test_diff.py b/snippets/test_diff.py index 1cdf0d0..c70160f 100644 --- a/snippets/test_diff.py +++ b/snippets/test_diff.py @@ -1,4 +1,8 @@ from birdvisu import maps +from pprint import pprint +import sys +import subprocess +from io import StringIO with open('current.ospf') as 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.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() -pprint(vars(diff)) +if len(sys.argv) == 1: + pprint(vars(diff)) +elif 'dot' in sys.argv: + visualize_graphviz(diff)