about summary refs log tree commit diff
path: root/examples/python/gi/update-ip4-method.py
diff options
context:
space:
mode:
authorMichael Biebl <biebl@debian.org>2015-01-22 00:29:39 +0100
committerMichael Biebl <biebl@debian.org>2015-01-22 00:29:39 +0100
commit2c032d8f1c6292c1338a615e6ec40252889ba85c (patch)
tree1f77182220b2b0264288ba4a476ab47e5bc48716 /examples/python/gi/update-ip4-method.py
parent33491bc4279481db8ae47213e34a6d695a0e8830 (diff)
Imported Upstream version 1.0.0 upstream/1.0.0
Diffstat (limited to 'examples/python/gi/update-ip4-method.py')
-rwxr-xr-xexamples/python/gi/update-ip4-method.py91
1 files changed, 40 insertions, 51 deletions
diff --git a/examples/python/gi/update-ip4-method.py b/examples/python/gi/update-ip4-method.py
index 2fe12693..983578cf 100755
--- a/examples/python/gi/update-ip4-method.py
+++ b/examples/python/gi/update-ip4-method.py
@@ -1,5 +1,6 @@
 #!/usr/bin/env python
 # -*- Mode: Python; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
+# vim: ft=python ts=4 sts=4 sw=4 et ai
 #
 # This program is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
@@ -15,35 +16,43 @@
 # with this program; if not, write to the Free Software Foundation, Inc.,
 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 #
-# Copyright (C) 2014 Red Hat, Inc.
+# Copyright 2014 Red Hat, Inc.
 #
 
 #
 # This example updates a connection's IPv4 method with the Update() method
-# using the libnm-glib GObject-based convenience APIs.
+# 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, NetworkManager, NMClient
-import sys, struct, socket
+from gi.repository import GLib, NM
+import sys, socket
 
-def ip_to_int(ip_string):
-    return struct.unpack("=I", socket.inet_aton(ip_string))[0]
+if __name__ == "__main__":
+    # parse and validate arguments
+    if len(sys.argv) < 3:
+        print "Usage: %s <uuid> <auto|static> [address prefix gateway]" % sys.argv[0]
+        sys.exit(1)
+
+    method = sys.argv[2]
+    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)
 
-# 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()
+    uuid = sys.argv[1]
 
-def connections_read_cb(settings, data):
-    uuid, method, args = data
+    # Convert method to NM method
+    if method == "static":
+        method = "manual"
+
+    main_loop = GLib.MainLoop()
 
-    all_connections = settings.list_connections()
+    # create Client object
+    client = NM.Client.new(None)
+
+    all_connections = client.get_connections()
     for c in all_connections:
         if c.get_uuid() != uuid:
             continue
@@ -51,46 +60,26 @@ def connections_read_cb(settings, data):
         # add IPv4 setting if it doesn't yet exist
         s_ip4 = c.get_setting_ip4_config()
         if not s_ip4:
-            s_ip4 = NetworkManager.SettingIP4Config.new()
+            s_ip4 = NM.SettingIP4Config.new()
             c.add_setting(s_ip4)
 
         # set the method and change properties
-        s_ip4.set_property(NetworkManager.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 = NetworkManager.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)
-
-        c.commit_changes(commit_cb, None)
-
-if __name__ == "__main__":
-    # parse and validate arguments
-    if len(sys.argv) < 3:
-        print "Usage: %s <uuid> <auto|static> [address prefix gateway]" % sys.argv[0]
-        sys.exit(1)
-
-    method = sys.argv[2]
-    if method == "static" and len(sys.argv) < 5:
-        print "Usage: %s %s static address prefix [gateway]" % (sys.argv[0], sys.argv[1])
-        sys.exit(1)
-
-    # Convert method to NM method
-    if method == "static":
-        method = "manual"
-
-    main_loop = GLib.MainLoop()
-
-    # create RemoteSettings object and attach to the "connections-read" signal
-    # to wait for connections to be loaded asynchronously
-    settings = NMClient.RemoteSettings.new(None)
-    settings.connect('connections-read', connections_read_cb, (sys.argv[1], method, sys.argv))
-
-    main_loop.run()
+            if len(sys.argv) == 6:
+                s_ip4.props.gateway = sys.argv[5]
+
+        try:
+            c.commit_changes(True, None)
+            print("The connection profile has been updated.")
+        except Exception, e:
+            print("Error: %s" % e)
+        break