-
Notifications
You must be signed in to change notification settings - Fork 0
/
toporenderpdfgraph.py
executable file
·70 lines (65 loc) · 2.23 KB
/
toporenderpdfgraph.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
import sys
import json
from pathlib import Path
from topoviewer import nx_graph_from_topo
from networkx.drawing.layout import spring_layout
from networkx.drawing.nx_pylab import draw_networkx
from networkx.drawing.nx_pylab import draw_networkx_labels
from networkx.drawing.nx_pylab import draw_networkx_nodes
from networkx.drawing.nx_pylab import draw_networkx_edges
from networkx.drawing.nx_pylab import draw_networkx_edge_labels
from matplotlib import pyplot as plt
from matplotlib.figure import Figure
def main(fn: str):
infile = Path(f'{fn}')
outfile = infile.parent.joinpath(infile.stem+'.pdf')
outfilebw = infile.parent.joinpath(infile.stem+'.pdf.bw.txt')
topo = json.loads(infile.read_text())
nxg = nx_graph_from_topo(topo)
pos = spring_layout(nxg, iterations=4000)
fig = Figure()
fig.set_dpi(300)
ax = fig.add_subplot(111)
ax.axis('off')
labels = dict()
for edge in nxg.edges:
bw = nxg.get_edge_data(edge[0], edge[1], dict()).get('bw', None)
bw = '' if bw is None else '%.1f mbps' % (bw,)
labels[edge] = bw
if len(set(labels.values()))>1:
draw_networkx_edge_labels(
nxg, pos, ax=ax,
edge_labels=labels,
bbox=dict(
facecolor='#FFFFFF88',
edgecolor='none'
),
font_size='x-small'
)
else:
sbw = next(iter(set(labels.values())))
outfilebw.write_text(sbw)
draw_networkx_edges(
nxg, pos, ax=ax,
edgelist=nxg.edges, edge_color='black'
)
draw_networkx_nodes(
nxg, pos, ax=ax,
nodelist=topo[0], node_color='lightgreen', edgecolors="green"
)
draw_networkx_nodes(
nxg, pos, ax=ax,
nodelist=topo[1], node_color='cyan', edgecolors="darkcyan"
)
draw_networkx_labels(nxg, pos, ax=ax, font_size='small')
fig.savefig(str(outfile))
if __name__ == "__main__":
if len(sys.argv) == 2 and Path(sys.argv[1]).exists():
fn = ' '.join(sys.argv[1:])
main(fn)
else:
print("Usage:", file=sys.stderr)
print(f" {sys.argv[0]} <toponame.json>", file=sys.stderr)
print(f" <toponame.json> --> <toponame>.pdf", file=sys.stderr)