examples: take lines out of loop (refactor)

since they will only be executed once.

Also, an error is raised if a connection is not found
This commit is contained in:
Isidro Arias
2024-07-19 15:16:54 +02:00
committed by Íñigo Huguet
parent f141f4bbe7
commit 4484397a0c

View File

@@ -42,16 +42,16 @@ if __name__ == "__main__":
# create Client object
client = NM.Client.new(None)
all_connections = client.get_connections()
for c in all_connections:
if c.get_uuid() != uuid:
continue
try:
conn = next(c for c in client.get_connections() if c.get_uuid() == uuid)
except StopIteration:
sys.exit("not found connection with uuid=%s" % uuid)
# add IPv4 setting if it doesn't yet exist
s_ip4 = c.get_setting_ip4_config()
s_ip4 = conn.get_setting_ip4_config()
if not s_ip4:
s_ip4 = NM.SettingIP4Config.new()
c.add_setting(s_ip4)
conn.add_setting(s_ip4)
# set the method and change properties
s_ip4.set_property(NM.SETTING_IP_CONFIG_METHOD, method)
@@ -67,8 +67,7 @@ if __name__ == "__main__":
s_ip4.props.gateway = sys.argv[5]
try:
c.commit_changes(True, None)
conn.commit_changes(True, None)
print("The connection profile has been updated.")
except Exception as e:
sys.stderr.write("Error: %s\n" % e)
break