merge: branch 'patch-1'

Python gi examples little fix and refactor

https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/1997
This commit is contained in:
Íñigo Huguet
2024-08-06 10:17:24 +00:00
3 changed files with 41 additions and 42 deletions

View File

@@ -35,7 +35,7 @@ def show_addresses(dev, family):
addr = nm_address.get_address() addr = nm_address.get_address()
prefix = nm_address.get_prefix() prefix = nm_address.get_prefix()
print("%s/%d") % (addr, prefix) print("%s/%d" % (addr, prefix))
def show_gateway(dev, family): def show_gateway(dev, family):
@@ -75,7 +75,7 @@ def show_routes(dev, family):
next_hop = nm_route.get_next_hop() next_hop = nm_route.get_next_hop()
metric = nm_route.get_metric() metric = nm_route.get_metric()
print("%s/%d %s %d") % (dest, prefix, next_hop, metric) print("%s/%d %s %d" % (dest, prefix, next_hop, metric))
def show_dns(dev, family): def show_dns(dev, family):
@@ -88,11 +88,11 @@ def show_dns(dev, family):
print("None") print("None")
return return
print("Nameservers: %s") % (ip_cfg.get_nameservers()) print("Nameservers: %s" % ip_cfg.get_nameservers())
print("Domains: %s") % (ip_cfg.get_domains()) print("Domains: %s" % ip_cfg.get_domains())
print("Searches: %s") % (ip_cfg.get_searches()) print("Searches: %s" % ip_cfg.get_searches())
if family == socket.AF_INET: if family == socket.AF_INET:
print("WINS: %s") % (ip_cfg.get_wins_servers()) print("WINS: %s" % ip_cfg.get_wins_servers())
if __name__ == "__main__": if __name__ == "__main__":
@@ -110,39 +110,39 @@ if __name__ == "__main__":
print("IPv4 addresses:") print("IPv4 addresses:")
print("---------------") print("---------------")
show_addresses(dev, socket.AF_INET) show_addresses(dev, socket.AF_INET)
print print()
print("IPv4 gateway:") print("IPv4 gateway:")
print("-------------") print("-------------")
show_gateway(dev, socket.AF_INET) show_gateway(dev, socket.AF_INET)
print print()
print("IPv4 routes:") print("IPv4 routes:")
print("------------") print("------------")
show_routes(dev, socket.AF_INET) show_routes(dev, socket.AF_INET)
print print()
print("IPv6 addresses:") print("IPv6 addresses:")
print("---------------") print("---------------")
show_addresses(dev, socket.AF_INET6) show_addresses(dev, socket.AF_INET6)
print print()
print("IPv6 gateway:") print("IPv6 gateway:")
print("-------------") print("-------------")
show_gateway(dev, socket.AF_INET6) show_gateway(dev, socket.AF_INET6)
print print()
print("IPv6 routes:") print("IPv6 routes:")
print("------------") print("------------")
show_routes(dev, socket.AF_INET6) show_routes(dev, socket.AF_INET6)
print print()
print("IPv4 DNS:") print("IPv4 DNS:")
print("------------") print("------------")
show_dns(dev, socket.AF_INET) show_dns(dev, socket.AF_INET)
print print()
print("IPv6 DNS:") print("IPv6 DNS:")
print("------------") print("------------")
show_dns(dev, socket.AF_INET6) show_dns(dev, socket.AF_INET6)
print print()

View File

@@ -14,7 +14,7 @@
# API (like NM.Client.new()) is for simple programs but usually not best # API (like NM.Client.new()) is for simple programs but usually not best
# for using NMClient for real applications. # for using NMClient for real applications.
# #
# To learn more about GMainContext, read https://developer.gnome.org/SearchProvider/documentation/tutorials/main-contexts.html # To learn more about GMainContext, read https://developer.gnome.org/documentation/tutorials/main-contexts.html
# When I say "mainloop" or "event loop", I mean GMainContext. GMainLoop is # When I say "mainloop" or "event loop", I mean GMainContext. GMainLoop is
# a small wrapper around GMainContext to run the context with a boolean # a small wrapper around GMainContext to run the context with a boolean
# flag. # flag.

View File

@@ -42,33 +42,32 @@ if __name__ == "__main__":
# create Client object # create Client object
client = NM.Client.new(None) client = NM.Client.new(None)
all_connections = client.get_connections() try:
for c in all_connections: conn = next(c for c in client.get_connections() if c.get_uuid() == uuid)
if c.get_uuid() != uuid: except StopIteration:
continue sys.exit("not found connection with uuid=%s" % uuid)
# add IPv4 setting if it doesn't yet exist # 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: if not s_ip4:
s_ip4 = NM.SettingIP4Config.new() s_ip4 = NM.SettingIP4Config.new()
c.add_setting(s_ip4) conn.add_setting(s_ip4)
# set the method and change properties # set the method and change properties
s_ip4.set_property(NM.SETTING_IP_CONFIG_METHOD, method) s_ip4.set_property(NM.SETTING_IP_CONFIG_METHOD, method)
if method == "auto": if method == "auto":
# remove addresses and gateway # remove addresses and gateway
s_ip4.clear_addresses() s_ip4.clear_addresses()
s_ip4.props.gateway = None s_ip4.props.gateway = None
elif method == "manual": elif method == "manual":
# Add the static IP address, prefix, and (optional) gateway # Add the static IP address, prefix, and (optional) gateway
addr = NM.IPAddress.new(socket.AF_INET, sys.argv[3], int(sys.argv[4])) addr = NM.IPAddress.new(socket.AF_INET, sys.argv[3], int(sys.argv[4]))
s_ip4.add_address(addr) s_ip4.add_address(addr)
if len(sys.argv) == 6: if len(sys.argv) == 6:
s_ip4.props.gateway = sys.argv[5] s_ip4.props.gateway = sys.argv[5]
try: try:
c.commit_changes(True, None) conn.commit_changes(True, None)
print("The connection profile has been updated.") print("The connection profile has been updated.")
except Exception as e: except Exception as e:
sys.stderr.write("Error: %s\n" % e) sys.stderr.write("Error: %s\n" % e)
break