Skip to content

Commit

Permalink
Added ConnectionProfile.update method
Browse files Browse the repository at this point in the history
Updates connection profile with the settings from another profile.
Similar to dict.update
  • Loading branch information
igo95862 committed Dec 3, 2022
1 parent dd7f561 commit 6439b7d
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 0 deletions.
28 changes: 28 additions & 0 deletions sdbus_async/networkmanager/settings/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,34 @@ def from_settings_dict(
for k, v in settings_dict.items()}
return cls(**unvarianted_options)

def update(self, other: ConnectionProfile) -> None:
"""Update this connection profile with the settings from the other.
Similar to dict.update method.
"""
for f in fields(other):
settings_field_name = f.name
other_settings = getattr(other, settings_field_name)

if other_settings is None:
continue

my_settings = getattr(self, settings_field_name)

if my_settings is None:
setattr(self, settings_field_name, other_settings)
continue

for setting_field in fields(other_settings):
setting_field_name = setting_field.name

other_setting = getattr(other_settings, setting_field_name)

if other_setting is None:
continue

setattr(my_settings, setting_field_name, other_setting)


SETTING_DBUS_NAME_TO_NAME: Dict[str, str] = {
f.metadata['dbus_name']: f.name
Expand Down
19 changes: 19 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# SPDX-License-Identifier: LGPL-2.1-or-later

# Copyright (C) 2022 igo95862

# This file is part of python-sdbus

# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.

# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.

# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
64 changes: 64 additions & 0 deletions tests/test_settings_profile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# SPDX-License-Identifier: LGPL-2.1-or-later

# Copyright (C) 2022 igo95862

# This file is part of python-sdbus

# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.

# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.

# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
from __future__ import annotations

from unittest import TestCase
from sdbus_async.networkmanager.settings import ConnectionProfile

connection_dict = {
'connection': {'id': 'mlvd-wg',
'interface-name': 'mlvd-wg',
'timestamp': 150000,
'type': 'wireguard',
'uuid': 'uuid'},
'ipv4': {'address-data': [{'address': '10.0.0.1', 'prefix': 32}],
'dns': [150000],
'dns-search': ['~'],
'method': 'manual'},
'ipv6': {'addr-gen-mode': 1,
'address-data': [{'address': 'fc00:1',
'prefix': 128}],
'method': 'manual'},
'wireguard': {
'peers': [
{'allowed-ips': ['::/0', '0.0.0.0/0'],
'endpoint': '1.1.1.1:51820',
'public-key': 'public_key'}]}}

secret_dict = {
'connection': {},
'ipv4': {},
'ipv6': {},
'proxy': {},
'wireguard': {
'peers': [
{'public-key': 'public_key'}
],
'private-key': 'secret_key'}}


class TestSettingsProfile(TestCase):
def test_update(self) -> None:
connection = ConnectionProfile.from_settings_dict(connection_dict)
secrets = ConnectionProfile.from_settings_dict(secret_dict)

connection.update(secrets)

self.assertEqual(connection.wireguard.private_key, 'secret_key')
28 changes: 28 additions & 0 deletions tools/jinja_templates/profile.py.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,34 @@ class ConnectionProfile:
for k, v in settings_dict.items()}
return cls(**unvarianted_options)

def update(self, other: ConnectionProfile) -> None:
"""Update this connection profile with the settings from the other.

Similar to dict.update method.
"""
for f in fields(other):
settings_field_name = f.name
other_settings = getattr(other, settings_field_name)

if other_settings is None:
continue

my_settings = getattr(self, settings_field_name)

if my_settings is None:
setattr(self, settings_field_name, other_settings)
continue

for setting_field in fields(other_settings):
setting_field_name = setting_field.name

other_setting = getattr(other_settings, setting_field_name)

if other_setting is None:
continue

setattr(my_settings, setting_field_name, other_setting)


SETTING_DBUS_NAME_TO_NAME: Dict[str, str] = {
f.metadata['dbus_name']: f.name
Expand Down

0 comments on commit 6439b7d

Please sign in to comment.