Skip to content

Commit 52f26f7

Browse files
authored
Merge pull request #72 from kha-faz/main
Improvement on PR #70. Added `d8` test case to test_parse() in test_general. all tests pass
2 parents 28e26b9 + d70437e commit 52f26f7

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

nmcli/data/general.py

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,51 @@ def to_json(self):
2525
'wwan': self.wwan
2626
}
2727

28+
# case 1:
29+
# STATE CONNECTIVITY WIFI-HW WIFI WWAN-HW WWAN METERED
30+
# connected full enabled enabled missing enabled no (guessed)
31+
#
32+
# case 2:
33+
# unknown none enabled enabled enabled disabled
34+
#
35+
# case 3:
36+
# connected (local only) full disabled enabled enabled enabled
37+
#
38+
# see: https://regex101.com/r/zW1hHE/1
39+
2840
@classmethod
2941
def parse(cls, text: str) -> General:
30-
m = re.search(
31-
r'^([\S\s]+)\s{2}(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s*', text)
42+
#pattern = r'^([\S\s]+)\s{2}(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s*'
43+
44+
state_r = r'(?P<state>.+)'
45+
connectivity_r = r'(?P<connectivity>\S+)'
46+
wifi_hw_r = r'(?P<wifi_hw>\S+)'
47+
wifi_r = r'(?P<wifi>\S+)'
48+
wwan_hw_r = r'(?P<wwan_hw>\S+)'
49+
wwan_r = r'(?P<wwan>\S+)'
50+
metered_r = r'(?: (?P<metered>.+)?)?'
51+
52+
pattern = (
53+
r'^'
54+
+ state_r
55+
+ r' '
56+
+ connectivity_r
57+
+ r'\s+'
58+
+ wifi_hw_r
59+
+ r' '
60+
+ wifi_r
61+
+ r' '
62+
+ wwan_hw_r
63+
+ r' '
64+
+ wwan_r
65+
+ metered_r
66+
+ r'$'
67+
)
68+
69+
m = re.search(pattern, text)
70+
3271
if m:
33-
state, connectivity, wifi_hw, wifi, wwan_hw, wwan = m.groups()
72+
state, connectivity, wifi_hw, wifi, wwan_hw, wwan, _metered = m.groups()
3473
return General(NetworkManagerState(state),
3574
NetworkConnectivity(connectivity),
3675
wifi_hw == 'enabled',

tests/data/test_general.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ def test_parse():
5858
NetworkConnectivity.FULL,
5959
True, True, True, True)
6060

61+
d8 = 'connected full enabled enabled missing enabled no (guessed)'
62+
assert General.parse(d8) == General(NetworkManagerState.CONNECTED_GLOBAL,
63+
NetworkConnectivity.FULL,
64+
True, True, False, True)
65+
6166

6267
def test_parse_when_failed():
6368
with pytest.raises(ValueError) as e:

0 commit comments

Comments
 (0)