Skip to content

Commit a0f78ff

Browse files
committed
[fix] Fix parsing of wifi interface without ifname
1 parent 72c5570 commit a0f78ff

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

netjsonconfig/backends/openwrt/converters/wireless.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,14 +144,23 @@ def to_netjson_loop(self, block, result, index):
144144
interface = self.__get_netjson_interface(block)
145145
if not interface:
146146
is_new = True
147-
interface = {'name': block['ifname'], 'type': 'wireless'}
147+
ifname = self.__netjson_wifi_get_ifname(block)
148+
interface = {'name': ifname, 'type': 'wireless'}
148149
wifi = self.__netjson_wifi(block, interface)
149150
interface['wireless'] = wifi
150151
if is_new:
151152
result.setdefault('interfaces', [])
152153
result['interfaces'].append(interface)
153154
return result
154155

156+
def __netjson_wifi_get_ifname(self, block):
157+
"""
158+
returns the ifname or alternatively returns
159+
the UCI block name as interface name
160+
(because ifname in wifi devices is optional)
161+
"""
162+
return block.get('ifname', block['.name'].replace('wifi_', ''))
163+
155164
def __netjson_wifi(self, wifi, interface):
156165
_name = wifi.pop('.name')
157166
if _name != self.__get_auto_name(interface):
@@ -177,7 +186,7 @@ def __netjson_wifi(self, wifi, interface):
177186
if uci_key not in wifi:
178187
continue
179188
wifi[netjson_key] = int(wifi.pop(uci_key))
180-
ifname = wifi.pop('ifname', None)
189+
ifname = wifi.pop('ifname', interface['name'])
181190
if 'network' in wifi:
182191
if wifi['network'] in [ifname, interface.get('network')]:
183192
del wifi['network']
@@ -309,7 +318,8 @@ def __netjson_encryption_typecast(self, encryption):
309318

310319
def __get_netjson_interface(self, wifi):
311320
for interface in self.netjson.get('interfaces', []):
312-
if interface['name'] == wifi['ifname']:
321+
ifname = self.__netjson_wifi_get_ifname(wifi)
322+
if interface['name'] == ifname:
313323
interface['type'] = 'wireless'
314324
return interface
315325

tests/openwrt/test_wireless.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ def test_parse_wifi_interface(self):
6868
o = OpenWrt(native=self._wifi_uci)
6969
self.assertEqual(o.config, self._wifi_netjson)
7070

71+
def test_parse_wifi_interface_without_ifname(self):
72+
o = OpenWrt(native=self._wifi_uci.replace(" option ifname 'wlan0'\n", ''))
73+
self.assertEqual(o.config, self._wifi_netjson)
74+
7175
def test_parse_wifi_interface_partial(self):
7276
o = OpenWrt(
7377
native="""package wireless

0 commit comments

Comments
 (0)