Skip to content

Commit

Permalink
Merge pull request #72 from kha-faz/main
Browse files Browse the repository at this point in the history
Improvement on PR #70. Added `d8` test case to test_parse() in test_general. all tests pass
  • Loading branch information
ushiboy authored Sep 15, 2024
2 parents 28e26b9 + d70437e commit 52f26f7
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
45 changes: 42 additions & 3 deletions nmcli/data/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,51 @@ def to_json(self):
'wwan': self.wwan
}

# case 1:
# STATE CONNECTIVITY WIFI-HW WIFI WWAN-HW WWAN METERED
# connected full enabled enabled missing enabled no (guessed)
#
# case 2:
# unknown none enabled enabled enabled disabled
#
# case 3:
# connected (local only) full disabled enabled enabled enabled
#
# see: https://regex101.com/r/zW1hHE/1

@classmethod
def parse(cls, text: str) -> General:
m = re.search(
r'^([\S\s]+)\s{2}(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s*', text)
#pattern = r'^([\S\s]+)\s{2}(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s*'

state_r = r'(?P<state>.+)'
connectivity_r = r'(?P<connectivity>\S+)'
wifi_hw_r = r'(?P<wifi_hw>\S+)'
wifi_r = r'(?P<wifi>\S+)'
wwan_hw_r = r'(?P<wwan_hw>\S+)'
wwan_r = r'(?P<wwan>\S+)'
metered_r = r'(?: (?P<metered>.+)?)?'

pattern = (
r'^'
+ state_r
+ r' '
+ connectivity_r
+ r'\s+'
+ wifi_hw_r
+ r' '
+ wifi_r
+ r' '
+ wwan_hw_r
+ r' '
+ wwan_r
+ metered_r
+ r'$'
)

m = re.search(pattern, text)

if m:
state, connectivity, wifi_hw, wifi, wwan_hw, wwan = m.groups()
state, connectivity, wifi_hw, wifi, wwan_hw, wwan, _metered = m.groups()
return General(NetworkManagerState(state),
NetworkConnectivity(connectivity),
wifi_hw == 'enabled',
Expand Down
5 changes: 5 additions & 0 deletions tests/data/test_general.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ def test_parse():
NetworkConnectivity.FULL,
True, True, True, True)

d8 = 'connected full enabled enabled missing enabled no (guessed)'
assert General.parse(d8) == General(NetworkManagerState.CONNECTED_GLOBAL,
NetworkConnectivity.FULL,
True, True, False, True)


def test_parse_when_failed():
with pytest.raises(ValueError) as e:
Expand Down

0 comments on commit 52f26f7

Please sign in to comment.