Skip to content

Commit 955190d

Browse files
committed
Added get_profile and update_profile to Connection interface
`get_profile` returns the profile object and can fetch secrets. `update_profile` updates connection using the profile object
1 parent caec60d commit 955190d

File tree

3 files changed

+91
-1
lines changed

3 files changed

+91
-1
lines changed

sdbus_async/networkmanager/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,6 @@
194194
NetworkManagerSettings,
195195
WiFiP2PPeer,
196196
)
197-
198197
from .types import (
199198
NetworkManagerConnectionProperties,
200199
NetworkManagerSetting,

sdbus_async/networkmanager/interfaces_other.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
dbus_signal_async,
2929
)
3030

31+
from .settings import ConnectionProfile
3132
from .types import NetworkManagerConnectionProperties
3233

3334

@@ -613,6 +614,51 @@ def removed(self) -> None:
613614
"""Signal when connection is removed"""
614615
raise NotImplementedError
615616

617+
async def update_profile(self,
618+
profile: ConnectionProfile,
619+
save_to_disk: bool = True) -> None:
620+
"""Update connection using the profile dataclass.
621+
622+
:param ConnectionProfile profile: Connection profile to update
623+
with.
624+
625+
:param bool save_to_disk: Make changes permanent by saving
626+
updated values to disk.
627+
628+
By default changes are temporary. (saved only to RAM)
629+
"""
630+
flags = 0
631+
632+
if save_to_disk:
633+
flags |= 0x1
634+
else:
635+
flags |= 0x2
636+
637+
await self.update2(profile.to_dbus(), flags, {})
638+
639+
async def get_profile(self,
640+
fetch_secrets: bool = True) -> ConnectionProfile:
641+
"""Get the connection settings as the profile object.
642+
643+
:param bool fetch_secrets: Retrieve secret values. (like VPN passwords)
644+
Makes additional calls to NetworkManager.
645+
"""
646+
profile = ConnectionProfile.from_dbus(await self.get_settings())
647+
648+
if fetch_secrets:
649+
secrets_name_generator = profile.update_secrets_generator()
650+
try:
651+
secrets_name = next(secrets_name_generator)
652+
while True:
653+
secret_profile = ConnectionProfile.from_dbus(
654+
await self.get_secrets(secrets_name))
655+
656+
secrets_name = secrets_name_generator.send(secret_profile)
657+
except StopIteration:
658+
...
659+
660+
return profile
661+
616662

617663
class NetworkManagerSettingsInterfaceAsync(
618664
DbusInterfaceCommonAsync,

sdbus_block/networkmanager/interfaces_other.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
from sdbus import DbusInterfaceCommon, dbus_method, dbus_property
2525

26+
from .settings import ConnectionProfile
2627
from .types import NetworkManagerConnectionProperties
2728

2829

@@ -581,6 +582,50 @@ def filename(self) -> str:
581582
"""File that stores connection settings"""
582583
raise NotImplementedError
583584

585+
def update_profile(self,
586+
profile: ConnectionProfile,
587+
save_to_disk: bool = True) -> None:
588+
"""Update connection using the profile dataclass.
589+
590+
:param ConnectionProfile profile: Connection profile to update
591+
with.
592+
593+
:param bool save_to_disk: Make changes permanent by saving
594+
updated values to disk.
595+
596+
By default changes are temporary. (saved only to RAM)
597+
"""
598+
flags = 0
599+
600+
if save_to_disk:
601+
flags |= 0x1
602+
else:
603+
flags |= 0x2
604+
605+
self.update2(profile.to_dbus(), flags, {})
606+
607+
def get_profile(self, fetch_secrets: bool = True) -> ConnectionProfile:
608+
"""Get the connection settings as the profile object.
609+
610+
:param bool fetch_secrets: Retrieve secret values. (like VPN passwords)
611+
Makes additional calls to NetworkManager.
612+
"""
613+
profile = ConnectionProfile.from_dbus(self.get_settings())
614+
615+
if fetch_secrets:
616+
secrets_name_generator = profile.update_secrets_generator()
617+
try:
618+
secrets_name = next(secrets_name_generator)
619+
while True:
620+
secret_profile = ConnectionProfile.from_dbus(
621+
self.get_secrets(secrets_name))
622+
623+
secrets_name = secrets_name_generator.send(secret_profile)
624+
except StopIteration:
625+
...
626+
627+
return profile
628+
584629

585630
class NetworkManagerSettingsInterface(
586631
DbusInterfaceCommon,

0 commit comments

Comments
 (0)