diff --git a/README.md b/README.md index 53943ae..18f75bf 100644 --- a/README.md +++ b/README.md @@ -160,6 +160,16 @@ Use `active` argument to show only the active profile. nmcli.connection.show(name: str, show_secrets: bool = False, active: bool = False) -> ConnectionDetails ``` +#### nmcli.connection.show_all + +Show all connections. + +Use `active` argument to show only active connections. + +``` +nmcli.connection.show_all(active: bool = False) -> List[Connection] +``` + #### nmcli.connection.reload Reload all connection files from disk. diff --git a/nmcli/_connection.py b/nmcli/_connection.py index e581bc7..032c117 100644 --- a/nmcli/_connection.py +++ b/nmcli/_connection.py @@ -34,6 +34,9 @@ def down(self, name: str, wait: int = None) -> None: def show(self, name: str, show_secrets: bool = False, active: bool = False) -> ConnectionDetails: raise NotImplementedError + def show_all(self, active: bool = False) -> List[Connection]: + raise NotImplementedError + def reload(self) -> None: raise NotImplementedError @@ -104,5 +107,17 @@ def show(self, name: str, show_secrets: bool = False, active: bool = False) -> C results[key] = None if value in ('--', '""') else value return results + def show_all(self, active: bool = False) -> List[Connection]: + cmd = ['connection', 'show'] + if active: + cmd += ['--active'] + r = self._syscmd.nmcli(cmd) + results = [] + for row in r.split('\n')[1:]: + if len(row) == 0: + continue + results.append(Connection.parse(row)) + return results + def reload(self) -> None: self._syscmd.nmcli(['connection', 'reload']) diff --git a/nmcli/dummy/_connection.py b/nmcli/dummy/_connection.py index 124cb7a..9cd95fe 100644 --- a/nmcli/dummy/_connection.py +++ b/nmcli/dummy/_connection.py @@ -31,6 +31,10 @@ def down_args(self): def show_args(self): return self._show_args + @property + def show_all_args(self): + return self._show_all_args + @property def called_reload(self) -> int: return self._called_reload @@ -38,16 +42,19 @@ def called_reload(self) -> int: def __init__(self, result_call: List[Connection] = None, result_show: ConnectionDetails = None, + result_show_all: List[Connection] = None, raise_error: Exception = None): self._raise_error = raise_error self._result_call = result_call or [] self._result_show = result_show + self._result_show_all = result_show_all or [] self._add_args: List[Tuple] = [] self._modify_args: List[Tuple] = [] self._delete_args: List[Tuple] = [] self._up_args: List[Tuple] = [] self._down_args: List[Tuple] = [] self._show_args: List[Tuple] = [] + self._show_all_args: List[Tuple] = [] self._called_reload = 0 def __call__(self) -> List[Connection]: @@ -86,6 +93,11 @@ def show(self, name: str, show_secrets: bool = False, active: bool = False) -> C return self._result_show raise ValueError("'result_show' is not properly initialized") + def show_all(self, active: bool = False) -> List[Connection]: + self._raise_error_if_needed() + self._show_all_args.append((active,)) + return self._result_show_all + def reload(self) -> None: self._raise_error_if_needed() self._called_reload += 1 diff --git a/tests/dummy/test_connection.py b/tests/dummy/test_connection.py index 5300cfa..5a3848c 100644 --- a/tests/dummy/test_connection.py +++ b/tests/dummy/test_connection.py @@ -134,6 +134,28 @@ def test_show_when_no_arguments_are_passed(): c.show('MyHome') +def test_show_all(): + result_show_all = [ + Connection('AP1', '3eac760c-de77-4823-9ab8-773c276daca3', + 'wifi', 'wlan0'), + Connection('Home', '700f5b18-cbb3-4d38-9c61-e3bc3a3852b9', + 'ethernet', 'eth0') + ] + c = DummyConnectionControl(result_show_all=result_show_all) + + assert c.show_all() == result_show_all + assert c.show_all_args[0] == (False,) + + c.show_all(active=True) + assert c.show_all_args[1] == (True,) + + +def test_show_all_when_raise_error(): + c = DummyConnectionControl(raise_error=Exception) + with pytest.raises(Exception): + c.show_all() + + def test_reload(): c = DummyConnectionControl() c.reload() diff --git a/tests/test_connection.py b/tests/test_connection.py index 6b13bf5..5f6fb07 100644 --- a/tests/test_connection.py +++ b/tests/test_connection.py @@ -132,6 +132,28 @@ def test_show(): 'connection', 'show', "--active", name] +def test_show_all(): + s = DummySystemCommand('''NAME UUID TYPE DEVICE +AP1 3eac760c-de77-4823-9ab8-773c276daca3 wifi wlan0 +Home 700f5b18-cbb3-4d38-9c61-e3bc3a3852b9 ethernet eth0 +Wired connection 1 700f5b18-cbb3-4d38-9c61-999999999999 ethernet eth1''') + connection = ConnectionControl(s) + + r = connection.show_all() + assert s.passed_parameters == ['connection', 'show'] + assert r == [ + Connection('AP1', '3eac760c-de77-4823-9ab8-773c276daca3', + 'wifi', 'wlan0'), + Connection('Home', '700f5b18-cbb3-4d38-9c61-e3bc3a3852b9', + 'ethernet', 'eth0'), + Connection('Wired connection 1', + '700f5b18-cbb3-4d38-9c61-999999999999', 'ethernet', 'eth1') + ] + + connection.show_all(active=True) + assert s.passed_parameters == ['connection', 'show', '--active'] + + def test_reload(): s = DummySystemCommand() connection = ConnectionControl(s)