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:
Dan Winship
2014-11-13 14:27:26 -05:00
parent 94157ce900
commit 66936decfa
12 changed files with 227 additions and 137 deletions

View File

@@ -21,15 +21,14 @@
#
# This example updates a connection's IPv4 method with the Update() method.
#
# This uses the new NM 1.0 setting properties. See add-connection-compat.py
# for a similar example using the backward-compatible properties
#
# 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
#
import socket, struct, dbus, sys
def ip_to_int(ip_string):
return struct.unpack("=I", socket.inet_aton(ip_string))[0]
import dbus, sys
if len(sys.argv) < 3:
print "Usage: %s <uuid> <auto|static> [address prefix gateway]" % sys.argv[0]
@@ -61,18 +60,22 @@ for c_path in settings.ListConnections():
if 'ipv4' not in c_settings:
c_settings['ipv4'] = {}
# clear existing address info
if c_settings['ipv4'].has_key('addresses'):
del c_settings['ipv4']['addresses']
if c_settings['ipv4'].has_key('address-data'):
del c_settings['ipv4']['address-data']
if c_settings['ipv4'].has_key('gateway'):
del c_settings['ipv4']['gateway']
# set the method and change properties
c_settings['ipv4']['method'] = method
if method == "auto":
# remove addresses
c_settings['ipv4']['addresses'] = dbus.Array([], signature=dbus.Signature('au'))
elif method == "manual":
if method == "manual":
# Add the static IP address, prefix, and (optional) gateway
gw = 0
addr = dbus.Dictionary({'address': sys.argv[3], 'prefix': dbus.UInt32(int(sys.argv[4]))})
c_settings['ipv4']['address-data'] = dbus.Array([addr], signature=dbus.Signature('a{sv}'))
if len(sys.argv) == 6:
gw = ip_to_int(sys.argv[5])
addr = dbus.Array([ip_to_int(sys.argv[3]), dbus.UInt32(int(sys.argv[4])), gw], signature=dbus.Signature('u'))
c_settings['ipv4']['addresses'] = dbus.Array([addr], signature=dbus.Signature('au'))
c_settings['ipv4']['gateway'] = sys.argv[5]
# Save all the updated settings back to NetworkManager
c_obj.Update(c_settings)