Skip to content

Commit

Permalink
Implemented OrderedDict in iwconfig, closes #36
Browse files Browse the repository at this point in the history
  • Loading branch information
nemesifier committed Nov 12, 2014
1 parent 9367560 commit 35ed3b3
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 17 deletions.
1 change: 1 addition & 0 deletions netengine/utils/ifconfig.py
@@ -1,6 +1,7 @@
import re
import json
import ipaddress
from collections import OrderedDict


class IfConfig(object):
Expand Down
31 changes: 16 additions & 15 deletions netengine/utils/iwconfig.py
@@ -1,5 +1,6 @@
import re
import json
from collections import OrderedDict


class IwConfig(object):
Expand All @@ -26,7 +27,7 @@ def __init__(self, output):
self.interfaces.append(self._parse_block(block))

def _parse_block(self, output):
result = {}
result = OrderedDict()
lines = output.split('\n')
# first line is special, split it in parts,
# use double whitespace as delimeter
Expand Down Expand Up @@ -60,22 +61,22 @@ def to_netjson(self, python=False, **kwargs):
""" convert to netjson format """
result = []
for i in self.interfaces:
wireless = {
'bitrate': i.get('bit_rate'),
'standard': i.get('ieee'),
'essid': i.get('essid'),
'mode': self.MODE_MAP.get(i['mode']),
'rts_threshold': i.get('rts_thr'),
'frag_threshold': i.get('fragment_thr')
}
wireless = OrderedDict((
('bitrate', i.get('bit_rate')),
('standard', i.get('ieee')),
('essid', i.get('essid')),
('mode', self.MODE_MAP.get(i['mode'])),
('rts_threshold', i.get('rts_thr')),
('frag_threshold', i.get('fragment_thr'))
))
if 'encryption_key' in i:
wireless['encryption'] = i['encryption_key'] != 'off'
result.append({
'name': i['name'],
'mac': i['access_point'],
'wireless': wireless
})

result.append(OrderedDict((
('name', i['name']),
('mac', i['access_point']),
('wireless', wireless)
)))
# can return both python and json
if python:
return result
Expand Down
11 changes: 9 additions & 2 deletions tests/utils/iwconfig.py
@@ -1,5 +1,6 @@
import unittest
import json
from collections import OrderedDict

from netengine.utils.iwconfig import IwConfig

Expand Down Expand Up @@ -32,12 +33,11 @@ def test_openwrt_backfire(self):
Tx excessive retries:3 Invalid misc:4 Missed beacon:5
setup00 no wireless extensions."""

i = IwConfig(output).to_python()

self.assertEqual(len(i), 1)
self.assertEqual(len(i[0].keys()), 21)
self.assertTrue(type(i[0]) is dict)
self.assertTrue(type(i[0]) is OrderedDict)
self.assertEqual(i[0]['name'], 'r11v16')
self.assertEqual(i[0]['ieee'], '802.11g')
self.assertEqual(i[0]['essid'], 'ExampleWifi')
Expand Down Expand Up @@ -85,6 +85,13 @@ def test_to_netjson(self):
Link Quality=69/70 Signal level=-27 dBm Noise level=-96 dBm
Rx invalid nwid:630390 Rx invalid crypt:1 Rx invalid frag:2
Tx excessive retries:3 Invalid misc:4 Missed beacon:5"""

i = IwConfig(output).to_netjson(python=True)
self.assertEqual(len(i), 1)
self.assertTrue(type(i[0]) is OrderedDict)
self.assertEqual(len(i[0].keys()), 3)
self.assertEqual(len(i[0]['wireless'].keys()), 7)

json_output = IwConfig(output).to_netjson()
i = json.loads(json_output)
self.assertEqual(len(i), 1)
Expand Down

0 comments on commit 35ed3b3

Please sign in to comment.