Skip to content

Commit 6e4dbae

Browse files
authored
Merge pull request #35 from bernhardkaindl/sync-helpers-to-sdbus_block
Thank you
2 parents ab3de2b + 1fe6c83 commit 6e4dbae

File tree

4 files changed

+70
-50
lines changed

4 files changed

+70
-50
lines changed

examples/async/delete-connection-by-uuid-async.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,24 @@
11
#!/usr/bin/env python
22
# SPDX-License-Identifier: LGPL-2.1-or-later
33
#
4-
# Update a property of a connection profile, looked up by connection id
5-
#
6-
# The IPv4 settings of connections profiles are documented here:
7-
# https://networkmanager.dev/docs/api/latest/settings-ipv4.html
4+
# Create and delete a connection profile using the unique connection uuid
85
#
96
import asyncio
107
import logging
118
import sdbus
129
from uuid import uuid4
1310
from argparse import Namespace
1411
from sdbus_async.networkmanager import NetworkManagerSettings
15-
from sdbus_async.networkmanager import NetworkConnectionSettings
12+
from sdbus_async.networkmanager import NmSettingsInvalidConnectionError
1613

1714

1815
async def delete_connection_by_uuid(uuid: str) -> bool:
1916
"""Find and delete the connection identified by the given UUID"""
20-
settings_manager = NetworkManagerSettings()
21-
connection_path = await settings_manager.get_connection_by_uuid(uuid)
22-
if not connection_path:
17+
try:
18+
await NetworkManagerSettings().delete_connection_by_uuid(uuid)
19+
except NmSettingsInvalidConnectionError:
2320
logging.getLogger().fatal(f"Connection {uuid} for deletion not found")
2421
return False
25-
connection_settings = NetworkConnectionSettings(connection_path)
26-
await connection_settings.delete()
2722
return True
2823

2924

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,27 @@
11
#!/usr/bin/env python
22
# SPDX-License-Identifier: LGPL-2.1-or-later
33
#
4-
# Update a property of a connection profile, looked up by connection id
5-
#
6-
# The IPv4 settings of connections profiles are documented here:
7-
# https://networkmanager.dev/docs/api/latest/settings-ipv4.html
4+
# Create and delete a connection profile using the unique connection uuid
85
#
96
import logging
107
import sdbus
118
from uuid import uuid4
129
from argparse import Namespace
1310
from sdbus_block.networkmanager import NetworkManagerSettings
14-
from sdbus_block.networkmanager import NetworkConnectionSettings
11+
from sdbus_block.networkmanager import NmSettingsInvalidConnectionError
1512

1613

1714
def delete_connection_by_uuid(uuid: str) -> bool:
1815
"""Find and delete the connection identified by the given UUID"""
19-
settings_manager = NetworkManagerSettings()
20-
connection_path = settings_manager.get_connection_by_uuid(uuid)
21-
if not connection_path:
16+
try:
17+
NetworkManagerSettings().delete_connection_by_uuid(uuid)
18+
except NmSettingsInvalidConnectionError:
2219
logging.getLogger().fatal(f"Connection {uuid} for deletion not found")
2320
return False
24-
connection_settings = NetworkConnectionSettings(connection_path)
25-
connection_settings.delete()
2621
return True
2722

2823

29-
def create_and_delete_wifi_psk_connection_(args: Namespace) -> bool:
24+
def create_and_delete_wifi_psk_connection_async(args: Namespace) -> bool:
3025
"""Add a temporary (not yet saved) network connection profile
3126
:param Namespace args: autoconnect, conn_id, psk, save, ssid, uuid
3227
:return: dbus connection path of the created connection profile
@@ -41,5 +36,5 @@ def create_and_delete_wifi_psk_connection_(args: Namespace) -> bool:
4136
logging.basicConfig(format="%(message)s", level=logging.WARNING)
4237
sdbus.set_default_bus(sdbus.sd_bus_open_system())
4338
args = Namespace(conn_id="Example", uuid=uuid4(), ssid="S", psk="Password")
44-
if create_and_delete_wifi_psk_connection_(args):
39+
if create_and_delete_wifi_psk_connection_async(args):
4540
print(f"Succeeded in creating and deleting connection {args.uuid}")

examples/block/list-connections.py

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,39 +17,41 @@
1717
# | ipv6: method: disabled
1818
import sdbus
1919
from sdbus_block.networkmanager import (
20+
ConnectionType,
2021
NetworkManagerSettings,
2122
NetworkConnectionSettings,
2223
)
2324

2425

2526
def list_connection_profiles_blocking() -> None:
26-
for connection_path in NetworkManagerSettings().connections:
27-
connection_settings = NetworkConnectionSettings(connection_path)
28-
settings = connection_settings.get_settings()
29-
connection = settings["connection"]
30-
31-
# Skip connection profiles for bridges and wireless networks
32-
if connection["type"][1] in ("bridge", "802-11-wireless"):
33-
continue
34-
35-
print("-------------------------------------------------------------")
36-
print("name:", connection["id"][1])
37-
print("uuid:", connection["uuid"][1])
38-
print("type:", connection["type"][1])
39-
if "interface-name" in connection:
40-
print(" interface-name:", connection["interface-name"][1])
41-
42-
if "ipv4" in settings:
43-
ipv4 = settings["ipv4"]
44-
print("ipv4: method:", ipv4["method"][1])
45-
if "address-data" in ipv4:
46-
for a in ipv4["address-data"][1]:
47-
print(f' ipaddr: {a["address"][1]}/{a["prefix"][1]}')
48-
if "route-metric" in ipv4:
49-
print(f' route-metric: {ipv4["route-metric"][1]}')
50-
51-
if "ipv6" in settings:
52-
print("ipv6: method:", settings["ipv6"]["method"][1])
27+
"""Call print_connection_profile_blocking() for all connection profiles"""
28+
networkmanager_settings = NetworkManagerSettings()
29+
for dbus_connection_path in networkmanager_settings.connections:
30+
print_connection_profile_blocking(dbus_connection_path)
31+
32+
33+
def print_connection_profile_blocking(connection_path: str) -> None:
34+
"""Show the use of NetworkConnectionSettings(path).connection_profile()"""
35+
profile = NetworkConnectionSettings(connection_path).connection_profile()
36+
print("-------------------------------------------------------------")
37+
print("name:", profile.connection.connection_id)
38+
print("uuid:", profile.connection.uuid)
39+
print("type:", profile.connection.connection_type)
40+
if profile.connection.interface_name:
41+
print(" interface-name:", profile.connection.interface_name)
42+
if profile.ipv4:
43+
print("ipv4: method:", profile.ipv4.method)
44+
if profile.ipv4.address_data:
45+
for address in profile.ipv4.address_data:
46+
print(f' ipaddr: {address.address}/{address.prefix}')
47+
if profile.ipv4.route_metric:
48+
print(f' route-metric: {profile.ipv4.route_metric}')
49+
if profile.ipv6:
50+
print("ipv6: method:", profile.ipv6.method)
51+
if profile.connection.connection_type == ConnectionType.WIFI:
52+
assert profile.wireless
53+
assert profile.wireless.ssid
54+
print("ssid:", profile.wireless.ssid.decode())
5355

5456

5557
if __name__ == "__main__":

sdbus_block/networkmanager/objects.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@
6767
NetworkManagerVPNConnectionInterface,
6868
NetworkManagerWifiP2PPeerInterface,
6969
)
70+
from .settings.profile import ConnectionProfile
71+
from .types import NetworkManagerConnectionProperties
7072

7173
NETWORK_MANAGER_SERVICE_NAME = 'org.freedesktop.NetworkManager'
7274

@@ -158,7 +160,7 @@ def get_connections_by_id(self, connection_id: str) -> List[str]:
158160
which use the given connection identifier.
159161
160162
:param str connection_id: The connection identifier of the connections,
161-
e.g. "Ethernet connection 1"
163+
e.g. "Wired connection 1"
162164
:return: List of connection profile paths using the given identifier.
163165
"""
164166
connection_paths_with_matching_id = []
@@ -169,6 +171,25 @@ def get_connections_by_id(self, connection_id: str) -> List[str]:
169171
connection_paths_with_matching_id.append(connection_path)
170172
return connection_paths_with_matching_id
171173

174+
def get_settings_by_uuid(
175+
self, connection_uuid: str
176+
) -> NetworkManagerConnectionProperties:
177+
"""Helper to get a nested settings dict of a connection profile by uuid.
178+
179+
:param str connection_uuid: The connection uuid of the connection profile
180+
:return: Nested dictionary of all settings of the given connection profile
181+
"""
182+
connection = self.get_connection_by_uuid(connection_uuid)
183+
return NetworkConnectionSettings(connection).get_settings()
184+
185+
def delete_connection_by_uuid(self, connection_uuid: str) -> None:
186+
"""Helper to delete a connection profile identified by the connection uuid.
187+
188+
:param str connection_uuid: The connection uuid of the connection profile
189+
"""
190+
conn_dbus_path = self.get_connection_by_uuid(connection_uuid)
191+
NetworkConnectionSettings(conn_dbus_path).delete()
192+
172193

173194
class NetworkConnectionSettings(
174195
NetworkManagerSettingsConnectionInterface):
@@ -192,6 +213,13 @@ def __init__(self, settings_path: str,
192213
settings_path,
193214
bus)
194215

216+
def connection_profile(self) -> ConnectionProfile:
217+
"""Return a ConnectionProfile object containing all profile settings
218+
219+
:return: Nested dataclass containing all settings of the profile
220+
"""
221+
return ConnectionProfile.from_dbus(self.get_settings())
222+
195223

196224
class NetworkDeviceGeneric(
197225
NetworkManagerDeviceInterface,

0 commit comments

Comments
 (0)