-
Notifications
You must be signed in to change notification settings - Fork 6
/
router_config_parser.py
62 lines (48 loc) · 1.51 KB
/
router_config_parser.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
from itertools import islice
config_text = """
...
description CUR:ATT:IUEP12345ATI,L,ETH:156MB,MPLS,20KGG12345
...
"""
KEY_INDEX = 0
VALUE_INDEX = 1
class RouterConfig():
def __init__(self, path=""):
self.attributes = {}
if path != "":
self.loadFromPath(path)
def get(self, attribute_key):
return self.attributes[attribute_key]
def loadFromPath(self, path):
with open(path, "r") as f:
self.loadFromIterable(f)
def loadFromString(self, text):
lines = text.split("\n")
self.loadFromIterable(lines)
def loadFromIterable(self, lines):
for line in lines:
parts = self.get_line_parts(line)
if len(parts) == 2:
key = parts[KEY_INDEX]
value = parts[VALUE_INDEX]
self.attributes[key] = value
if key == "description":
self.parse_description(value)
@staticmethod
def get_line_parts(line):
return line.strip().lower().split()
def parse_description(self, description):
ETH_INDEX = 2
parts = description.split(",")
self.attributes["circuit_size"] = parts[ETH_INDEX].split(":")[1].strip("mb")
def __str__(self):
output = []
for key in self.attributes:
value = self.attributes[key]
output.append(f"{key}=\"{value}\"")
return "\n".join(output)
rc = RouterConfig()
rc.loadFromString(config_text)
cs = rc.get("circuit_size")
print(cs)
print(rc)