-
Notifications
You must be signed in to change notification settings - Fork 0
/
np.py
executable file
·105 lines (70 loc) · 2.1 KB
/
np.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/python
import re
class Config:
config = None
children = []
def __init__(self):
pass
def __str__(self):
res = "I'm a config object\n"
for child in self.children:
res += str(child)
return res
def parse(self, config):
self.config = config
self._guess_platform()
# get interfaces
self.children.extend(Interface._parse_xr_interfaces(self.config))
def _guess_platform(self):
""" Guess platform from config
Possible NILS integration?
"""
# XXX: just return XR for now
return 'xr'
class Interface:
""" Representing a physical or logical interface
"""
name = None
description = None
def __init__(self):
pass
def __str__(self):
return "IF %-25s - %s\n" % (self.name, self.description)
@classmethod
def _parse_xr_interfaces(cls, config):
""" Parse interfaces from an XR config!
"""
res = []
for line in config.splitlines():
# get interface name
m = re.match('interface (?!preconfigure)(.*)$', line)
if m is not None:
res.append(Interface._parse_xr_interface(config, m.group(1)))
return res
@classmethod
def _parse_xr_interface(cls, config, name):
data = { 'name': name,
'description': None }
in_if = False
for line in config.splitlines():
if re.match('interface ' + name, line):
in_if = True
m = re.match(' description (.*)$', line)
if in_if and m is not None:
data['description'] = m.group(1)
if re.match('!', line):
in_if = False
return Interface.from_dict(data)
@classmethod
def from_dict(cls, data):
i = Interface()
i.name = data['name']
i.description = data['description']
return i
if __name__ == '__main__':
f = open('kst5-core-1')
raw_config = f.read()
f.close()
c = Config()
c.parse(raw_config)
print c