Skip to content

Commit b077a60

Browse files
authored
Merge pull request #8 from bernhardkaindl/helpers/get_connections_by_id
next merge: Helper NetworkManagerSettings.get_connections_by_id()
2 parents 195ac4e + 5976efd commit b077a60

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

sdbus_async/networkmanager/objects.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2222
from __future__ import annotations
2323

24-
from typing import Optional
24+
from typing import List, Optional
2525

2626
from sdbus.sd_bus_internals import SdBus
2727

@@ -154,6 +154,24 @@ def __init__(self, bus: Optional[SdBus] = None) -> None:
154154
'/org/freedesktop/NetworkManager/Settings',
155155
bus)
156156

157+
async def get_connections_by_id(self, connection_id: str) -> List[str]:
158+
"""Helper method to get a list of connection profile paths
159+
which use the given connection identifier.
160+
161+
:param str connection_id: The connection identifier of the connections,
162+
e.g. "Ethernet connection 1"
163+
:return: List of connection profile paths using the given identifier.
164+
"""
165+
connection_paths_with_matching_id = []
166+
connection_paths: List[str] = await self.connections
167+
for connection_path in connection_paths:
168+
settings = NetworkConnectionSettings(connection_path)
169+
settings_properites = await settings.get_settings()
170+
# settings_properites["connection"]["id"][1] gives the id value:
171+
if settings_properites["connection"]["id"][1] == connection_id:
172+
connection_paths_with_matching_id.append(connection_path)
173+
return connection_paths_with_matching_id
174+
157175

158176
class NetworkConnectionSettings(
159177
NetworkManagerSettingsConnectionInterfaceAsync):

sdbus_block/networkmanager/objects.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2222
from __future__ import annotations
2323

24-
from typing import Optional
24+
from typing import List, Optional
2525

2626
from sdbus.sd_bus_internals import SdBus
2727

@@ -149,6 +149,22 @@ def __init__(self, bus: Optional[SdBus] = None) -> None:
149149
'/org/freedesktop/NetworkManager/Settings',
150150
bus)
151151

152+
def get_connections_by_id(self, connection_id: str) -> List[str]:
153+
"""Helper method to get a list of connection profile paths
154+
which use the given connection identifier.
155+
156+
:param str connection_id: The connection identifier of the connections,
157+
e.g. "Ethernet connection 1"
158+
:return: List of connection profile paths using the given identifier.
159+
"""
160+
connection_paths_with_matching_id = []
161+
for connection_path in self.connections:
162+
profile = NetworkConnectionSettings(connection_path)
163+
# profile.get_settings()["connection"]["id"][1] gives the id value:
164+
if profile.get_settings()["connection"]["id"][1] == connection_id:
165+
connection_paths_with_matching_id.append(connection_path)
166+
return connection_paths_with_matching_id
167+
152168

153169
class NetworkConnectionSettings(
154170
NetworkManagerSettingsConnectionInterface):

0 commit comments

Comments
 (0)