5
5
class BatmanParser (BaseParser ):
6
6
"""batman-adv parser"""
7
7
8
- protocol = ' batman-adv'
9
- version = ' 2015.0'
10
- metric = 'TQ'
8
+ protocol = " batman-adv"
9
+ version = " 2015.0"
10
+ metric = "TQ"
11
11
12
12
# the default expected format
13
- _format = ' alfred_vis'
13
+ _format = " alfred_vis"
14
14
15
15
def to_python (self , data ):
16
16
"""
@@ -25,20 +25,20 @@ def _txtinfo_to_python(self, data):
25
25
"""
26
26
Converts txtinfo format to python
27
27
"""
28
- self ._format = ' txtinfo'
28
+ self ._format = " txtinfo"
29
29
# find interesting section
30
- lines = data .split (' \n ' )
30
+ lines = data .split (" \n " )
31
31
try :
32
- start = lines .index (' Table: Topology' ) + 2
32
+ start = lines .index (" Table: Topology" ) + 2
33
33
except ValueError :
34
- raise ParserError (' Unrecognized format' )
34
+ raise ParserError (" Unrecognized format" )
35
35
topology_lines = [line for line in lines [start :] if line ]
36
36
# convert to python list
37
37
parsed_lines = []
38
38
for line in topology_lines :
39
- values = line .split (' ' )
39
+ values = line .split (" " )
40
40
parsed_lines .append (
41
- {' source' : values [0 ], ' target' : values [1 ], ' cost' : float (values [4 ])}
41
+ {" source" : values [0 ], " target" : values [1 ], " cost" : float (values [4 ])}
42
42
)
43
43
return parsed_lines
44
44
@@ -59,9 +59,9 @@ def _get_aggregated_node_list(self, data):
59
59
"""
60
60
node_list = []
61
61
for node in data :
62
- local_addresses = [node [' primary' ]]
63
- if ' secondary' in node :
64
- local_addresses += node [' secondary' ]
62
+ local_addresses = [node [" primary" ]]
63
+ if " secondary" in node :
64
+ local_addresses += node [" secondary" ]
65
65
node_list .append (local_addresses )
66
66
return node_list
67
67
@@ -72,7 +72,7 @@ def parse(self, data):
72
72
* alfred_vis
73
73
* txtinfo
74
74
"""
75
- method = getattr (self , ' _parse_{0}' .format (self ._format ))
75
+ method = getattr (self , " _parse_{0}" .format (self ._format ))
76
76
return method (data )
77
77
78
78
def _parse_alfred_vis (self , data ):
@@ -83,26 +83,26 @@ def _parse_alfred_vis(self, data):
83
83
"""
84
84
# initialize graph and list of aggregated nodes
85
85
graph = self ._init_graph ()
86
- if ' source_version' in data :
87
- self .version = data [' source_version' ]
88
- if ' vis' not in data :
86
+ if " source_version" in data :
87
+ self .version = data [" source_version" ]
88
+ if " vis" not in data :
89
89
raise ParserError ('Parse error, "vis" key not found' )
90
- node_list = self ._get_aggregated_node_list (data [' vis' ])
90
+ node_list = self ._get_aggregated_node_list (data [" vis" ])
91
91
92
92
# loop over topology section and create networkx graph
93
93
for node in data ["vis" ]:
94
94
for neigh in node ["neighbors" ]:
95
95
graph .add_node (
96
- node [' primary' ],
96
+ node [" primary" ],
97
97
** {
98
- ' local_addresses' : node .get (' secondary' , []),
99
- ' clients' : node .get (' clients' , []),
98
+ " local_addresses" : node .get (" secondary" , []),
99
+ " clients" : node .get (" clients" , []),
100
100
}
101
101
)
102
- primary_neigh = self ._get_primary_address (neigh [' neighbor' ], node_list )
102
+ primary_neigh = self ._get_primary_address (neigh [" neighbor" ], node_list )
103
103
# networkx automatically ignores duplicated edges
104
104
graph .add_edge (
105
- node [' primary' ], primary_neigh , weight = float (neigh [' metric' ])
105
+ node [" primary" ], primary_neigh , weight = float (neigh [" metric" ])
106
106
)
107
107
return graph
108
108
@@ -113,5 +113,5 @@ def _parse_txtinfo(self, data):
113
113
"""
114
114
graph = self ._init_graph ()
115
115
for link in data :
116
- graph .add_edge (link [' source' ], link [' target' ], weight = link [' cost' ])
116
+ graph .add_edge (link [" source" ], link [" target" ], weight = link [" cost" ])
117
117
return graph
0 commit comments