Skip to content

Commit

Permalink
Implemented to_netjson method in iwconfig parser #36
Browse files Browse the repository at this point in the history
  • Loading branch information
nemesifier committed Nov 12, 2014
1 parent a11ac70 commit 9367560
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
36 changes: 36 additions & 0 deletions netengine/utils/iwconfig.py
Expand Up @@ -5,6 +5,16 @@
class IwConfig(object):
""" iwconfig parser class """

MODE_MAP = {
'Master': 'ap',
'Managed': 'sta',
'Ad-Hoc': 'adhoc',
'Repeater': 'wds',
'Secondary': 'wds',
'Monitor': 'mon',
'Auto': 'auto'
}

def __init__(self, output):
"""
:param output: iwconfig text output
Expand Down Expand Up @@ -45,3 +55,29 @@ def to_python(self):
def to_json(self, **kwargs):
""" returns json representation of ifconfig output """
return json.dumps(self.interfaces, **kwargs)

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')
}
if 'encryption_key' in i:
wireless['encryption'] = i['encryption_key'] != 'off'

result.append({
'name': i['name'],
'mac': i['access_point'],
'wireless': wireless
})
# can return both python and json
if python:
return result
else:
return json.dumps(result, **kwargs)
29 changes: 28 additions & 1 deletion tests/utils/iwconfig.py
Expand Up @@ -47,6 +47,7 @@ def test_openwrt_backfire(self):
self.assertEqual(i[0]['bit_rate'], '0 kb/s')
self.assertEqual(i[0]['tx_power'], '16 dBm')
self.assertEqual(i[0]['rts_thr'], 'off')
self.assertEqual(i[0]['fragment_thr'], 'off')
self.assertEqual(i[0]['encryption_key'], 'off')
self.assertEqual(i[0]['power_management'], 'off')
self.assertEqual(i[0]['link_quality'], '69/70')
Expand All @@ -69,8 +70,34 @@ def test_to_json(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"""

json_output = IwConfig(output).to_json()
i = json.loads(json_output)
self.assertEqual(len(i), 1)
self.assertEqual(len(i[0].keys()), 21)

def test_to_netjson(self):
output = """wlan0 IEEE 802.11g ESSID:"ExampleWifi"
Mode:Master Frequency:2.417 GHz Access Point: 00:12:0E:B8:92:AF
Bit Rate:0 kb/s Tx-Power=16 dBm
RTS thr:off Fragment thr:off
Encryption key:off
Power Management:off
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"""
json_output = IwConfig(output).to_netjson()
i = json.loads(json_output)
self.assertEqual(len(i), 1)
self.assertEqual(len(i[0].keys()), 3)
self.assertEqual(len(i[0]['wireless'].keys()), 7)
self.assertEqual(i[0]['mac'], '00:12:0E:B8:92:AF')
self.assertEqual(i[0]['name'], 'wlan0')
self.assertEqual(i[0]['wireless'], {
"bitrate": "0 kb/s",
"encryption": False,
"essid": "ExampleWifi",
"frag_threshold": "off",
"mode": "ap",
"rts_threshold": "off",
"standard": "802.11g"
})

0 comments on commit 9367560

Please sign in to comment.