examples: update python examples
Update the raw D-Bus python examples to use newer APIs where appropriate (and split the add-connection example into 1.0-only and 0.9-compatible versions). Update the gi-based python examples for the various API changes since they were last updated. Also add a comment to the ruby add-connection example pointing out that it's still using the old settings APIs.
This commit is contained in:
@@ -24,22 +24,11 @@
|
||||
# using the libnm GObject-based convenience APIs.
|
||||
#
|
||||
# Configuration settings are described at
|
||||
# https://developer.gnome.org/NetworkManager/0.9/ref-settings.html
|
||||
# https://developer.gnome.org/NetworkManager/1.0/ref-settings.html
|
||||
#
|
||||
|
||||
from gi.repository import GLib, NM
|
||||
import sys, struct, socket
|
||||
|
||||
def ip_to_int(ip_string):
|
||||
return struct.unpack("=I", socket.inet_aton(ip_string))[0]
|
||||
|
||||
# callback function
|
||||
def commit_cb(connection, error, data):
|
||||
if error is (None):
|
||||
print("The connection profile has been updated.")
|
||||
else:
|
||||
print(error)
|
||||
main_loop.quit()
|
||||
import sys, socket
|
||||
|
||||
if __name__ == "__main__":
|
||||
# parse and validate arguments
|
||||
@@ -48,7 +37,7 @@ if __name__ == "__main__":
|
||||
sys.exit(1)
|
||||
|
||||
method = sys.argv[2]
|
||||
if method == "static" or method == "manual" and len(sys.argv) < 5:
|
||||
if (method == "static" or method == "manual") and len(sys.argv) < 5:
|
||||
print "Usage: %s %s static address prefix [gateway]" % (sys.argv[0], sys.argv[1])
|
||||
sys.exit(1)
|
||||
|
||||
@@ -60,10 +49,10 @@ if __name__ == "__main__":
|
||||
|
||||
main_loop = GLib.MainLoop()
|
||||
|
||||
# create RemoteSettings object
|
||||
settings = NM.RemoteSettings.new(None)
|
||||
# create Client object
|
||||
client = NM.Client.new(None)
|
||||
|
||||
all_connections = settings.list_connections()
|
||||
all_connections = client.get_connections()
|
||||
for c in all_connections:
|
||||
if c.get_uuid() != uuid:
|
||||
continue
|
||||
@@ -75,21 +64,22 @@ if __name__ == "__main__":
|
||||
c.add_setting(s_ip4)
|
||||
|
||||
# set the method and change properties
|
||||
s_ip4.set_property(NM.SETTING_IP4_CONFIG_METHOD, method)
|
||||
s_ip4.set_property(NM.SETTING_IP_CONFIG_METHOD, method)
|
||||
if method == "auto":
|
||||
# remove addresses
|
||||
# remove addresses and gateway
|
||||
s_ip4.clear_addresses()
|
||||
s_ip4.props.gateway = None
|
||||
elif method == "manual":
|
||||
# Add the static IP address, prefix, and (optional) gateway
|
||||
addr = NM.IP4Address.new()
|
||||
addr.set_address(ip_to_int(sys.argv[3]))
|
||||
addr.set_prefix(int(sys.argv[4]))
|
||||
if len(sys.argv) == 6:
|
||||
addr.set_gateway(ip_to_int(sys.argv[5]))
|
||||
addr = NM.IPAddress.new(socket.AF_INET, sys.argv[3], int(sys.argv[4]))
|
||||
s_ip4.add_address(addr)
|
||||
if len(sys.argv) == 6:
|
||||
s_ip4.props.gateway = sys.argv[5]
|
||||
|
||||
c.commit_changes(commit_cb, None)
|
||||
|
||||
# run main loop
|
||||
main_loop.run()
|
||||
try:
|
||||
c.commit_changes(True, None)
|
||||
print("The connection profile has been updated.")
|
||||
except Exception, e:
|
||||
print("Error: %s" % e)
|
||||
break
|
||||
|
||||
|
Reference in New Issue
Block a user