-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtopomn2json.py
More file actions
executable file
·33 lines (28 loc) · 886 Bytes
/
topomn2json.py
File metadata and controls
executable file
·33 lines (28 loc) · 886 Bytes
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
#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
import sys
import json
from pathlib import Path
from toporender import main as renderer
def main(fn: str):
mn = json.loads(Path(f'{fn}.mn').read_text())
hosts = [i['opts']['hostname'] for i in mn['hosts']]
switches = [i['opts']['hostname'] for i in mn['switches']]
links = [
(
i['src'],
i['dest'],
i.get('opts', dict()).get('bw')
)
for i in mn['links']
]
Path(f'{fn}.json').write_text(json.dumps([hosts, switches, links]))
renderer(fn)
if __name__ == "__main__":
if len(sys.argv) > 1:
fn = ' '.join(sys.argv[1:])
main(fn)
else:
print("Usage:", file=sys.stderr)
print(f" {sys.argv[0]} <toponame>", file=sys.stderr)
print(f" <toponame>.mn --> <toponame>.json --> <toponame>.py", file=sys.stderr)