Skip to content

Commit 6439b7d

Browse files
committed
Added ConnectionProfile.update method
Updates connection profile with the settings from another profile. Similar to dict.update
1 parent dd7f561 commit 6439b7d

File tree

4 files changed

+139
-0
lines changed

4 files changed

+139
-0
lines changed

sdbus_async/networkmanager/settings/profile.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,34 @@ def from_settings_dict(
435435
for k, v in settings_dict.items()}
436436
return cls(**unvarianted_options)
437437

438+
def update(self, other: ConnectionProfile) -> None:
439+
"""Update this connection profile with the settings from the other.
440+
441+
Similar to dict.update method.
442+
"""
443+
for f in fields(other):
444+
settings_field_name = f.name
445+
other_settings = getattr(other, settings_field_name)
446+
447+
if other_settings is None:
448+
continue
449+
450+
my_settings = getattr(self, settings_field_name)
451+
452+
if my_settings is None:
453+
setattr(self, settings_field_name, other_settings)
454+
continue
455+
456+
for setting_field in fields(other_settings):
457+
setting_field_name = setting_field.name
458+
459+
other_setting = getattr(other_settings, setting_field_name)
460+
461+
if other_setting is None:
462+
continue
463+
464+
setattr(my_settings, setting_field_name, other_setting)
465+
438466

439467
SETTING_DBUS_NAME_TO_NAME: Dict[str, str] = {
440468
f.metadata['dbus_name']: f.name

tests/__init__.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# SPDX-License-Identifier: LGPL-2.1-or-later
2+
3+
# Copyright (C) 2022 igo95862
4+
5+
# This file is part of python-sdbus
6+
7+
# This library is free software; you can redistribute it and/or
8+
# modify it under the terms of the GNU Lesser General Public
9+
# License as published by the Free Software Foundation; either
10+
# version 2.1 of the License, or (at your option) any later version.
11+
12+
# This library is distributed in the hope that it will be useful,
13+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
# Lesser General Public License for more details.
16+
17+
# You should have received a copy of the GNU Lesser General Public
18+
# License along with this library; if not, write to the Free Software
19+
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

tests/test_settings_profile.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# SPDX-License-Identifier: LGPL-2.1-or-later
2+
3+
# Copyright (C) 2022 igo95862
4+
5+
# This file is part of python-sdbus
6+
7+
# This library is free software; you can redistribute it and/or
8+
# modify it under the terms of the GNU Lesser General Public
9+
# License as published by the Free Software Foundation; either
10+
# version 2.1 of the License, or (at your option) any later version.
11+
12+
# This library is distributed in the hope that it will be useful,
13+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
# Lesser General Public License for more details.
16+
17+
# You should have received a copy of the GNU Lesser General Public
18+
# License along with this library; if not, write to the Free Software
19+
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20+
from __future__ import annotations
21+
22+
from unittest import TestCase
23+
from sdbus_async.networkmanager.settings import ConnectionProfile
24+
25+
connection_dict = {
26+
'connection': {'id': 'mlvd-wg',
27+
'interface-name': 'mlvd-wg',
28+
'timestamp': 150000,
29+
'type': 'wireguard',
30+
'uuid': 'uuid'},
31+
'ipv4': {'address-data': [{'address': '10.0.0.1', 'prefix': 32}],
32+
'dns': [150000],
33+
'dns-search': ['~'],
34+
'method': 'manual'},
35+
'ipv6': {'addr-gen-mode': 1,
36+
'address-data': [{'address': 'fc00:1',
37+
'prefix': 128}],
38+
'method': 'manual'},
39+
'wireguard': {
40+
'peers': [
41+
{'allowed-ips': ['::/0', '0.0.0.0/0'],
42+
'endpoint': '1.1.1.1:51820',
43+
'public-key': 'public_key'}]}}
44+
45+
secret_dict = {
46+
'connection': {},
47+
'ipv4': {},
48+
'ipv6': {},
49+
'proxy': {},
50+
'wireguard': {
51+
'peers': [
52+
{'public-key': 'public_key'}
53+
],
54+
'private-key': 'secret_key'}}
55+
56+
57+
class TestSettingsProfile(TestCase):
58+
def test_update(self) -> None:
59+
connection = ConnectionProfile.from_settings_dict(connection_dict)
60+
secrets = ConnectionProfile.from_settings_dict(secret_dict)
61+
62+
connection.update(secrets)
63+
64+
self.assertEqual(connection.wireguard.private_key, 'secret_key')

tools/jinja_templates/profile.py.jinja2

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,34 @@ class ConnectionProfile:
137137
for k, v in settings_dict.items()}
138138
return cls(**unvarianted_options)
139139

140+
def update(self, other: ConnectionProfile) -> None:
141+
"""Update this connection profile with the settings from the other.
142+
143+
Similar to dict.update method.
144+
"""
145+
for f in fields(other):
146+
settings_field_name = f.name
147+
other_settings = getattr(other, settings_field_name)
148+
149+
if other_settings is None:
150+
continue
151+
152+
my_settings = getattr(self, settings_field_name)
153+
154+
if my_settings is None:
155+
setattr(self, settings_field_name, other_settings)
156+
continue
157+
158+
for setting_field in fields(other_settings):
159+
setting_field_name = setting_field.name
160+
161+
other_setting = getattr(other_settings, setting_field_name)
162+
163+
if other_setting is None:
164+
continue
165+
166+
setattr(my_settings, setting_field_name, other_setting)
167+
140168

141169
SETTING_DBUS_NAME_TO_NAME: Dict[str, str] = {
142170
f.metadata['dbus_name']: f.name

0 commit comments

Comments
 (0)