examples: fix python GI examples to work after libnm changes
authorJiří Klimeš <jklimes@redhat.com>
Fri, 5 Sep 2014 11:48:56 +0000 (13:48 +0200)
committerJiří Klimeš <jklimes@redhat.com>
Fri, 5 Sep 2014 11:48:56 +0000 (13:48 +0200)
examples/python/gi/add_connection.py
examples/python/gi/firewall-zone.py
examples/python/gi/get_ips.py
examples/python/gi/list-connections.py
examples/python/gi/update-ip4-method.py

index 86a34a1..10623b6 100755 (executable)
@@ -1,5 +1,7 @@
 #!/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,7 +17,7 @@
 # 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.
 #
 
 #
@@ -37,7 +39,7 @@ def print_values(setting, key, value, flags, data):
 
 # create an Ethernet connection and return it
 def create_profile(name):
-    profile = NM.Connection.new()
+    profile = NM.SimpleConnection.new()
     s_con = NM.SettingConnection.new()
     s_con.set_property(NM.SETTING_CONNECTION_ID, name)
     s_con.set_property(NM.SETTING_CONNECTION_UUID, str(uuid.uuid4()))
@@ -84,7 +86,7 @@ if __name__ == "__main__":
     main_loop = GLib.MainLoop()
 
     # create RemoteSettings object
-    settings = NM.RemoteSettings.new(None);
+    settings = NM.RemoteSettings.new(None)
 
     # create a connection profile for NM
     con = create_profile(profile_name)
index 068ed89..8ec59fa 100755 (executable)
@@ -16,7 +16,7 @@
 # with this program; if not, write to the Free Software Foundation, Inc.,
 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 #
-# Copyright (C) 2013 Red Hat, Inc.
+# Copyright 2013 - 2014 Red Hat, Inc.
 #
 
 import sys
@@ -43,7 +43,14 @@ def connection_saved(connection, error, data):
     print ("Connection '%s' saved.") % (connection.get_id())
     main_loop.quit()
 
-def connections_read(settings, data):
+if __name__ == "__main__":
+    if len(sys.argv) != 2 and len(sys.argv) != 3:
+        sys.exit('Usage: %s <connection name or UUID> [new zone]' % sys.argv[0])
+
+    main_loop = GLib.MainLoop()
+    settings = NM.RemoteSettings.new(None)
+    connections = settings.list_connections()
+
     con_name = sys.argv[1]
     if len(sys.argv) == 3:
         new_zone = sys.argv[2]
@@ -51,7 +58,6 @@ def connections_read(settings, data):
         new_zone = None
 
     found = False
-    connections = settings.list_connections()
     for c in connections:
         if c.get_id() == con_name or c.get_uuid() == con_name:
             found = True
@@ -71,17 +77,3 @@ def connections_read(settings, data):
     if not found:
         print ("Error: connection '%s' not found.") % (con_name)
         main_loop.quit()
-
-
-if __name__ == "__main__":
-    if len(sys.argv) != 2 and len(sys.argv) != 3:
-        sys.exit('Usage: %s <connection name or UUID> [new zone]' % sys.argv[0])
-
-    main_loop = GLib.MainLoop()
-    settings = NM.RemoteSettings.new(None);
-
-    # Connections are read asynchronously, so we have to wait for the
-    # 'settings' object to tell us that all connections have been read.
-    settings.connect("connections-read", connections_read, None)
-    main_loop.run()
-
index 1d110bf..f2f1e46 100755 (executable)
@@ -17,7 +17,7 @@
 # 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.
 #
 
 import sys, socket, struct
@@ -102,21 +102,11 @@ def show_dns(self, family):
         print("None")
         return
 
+    print ("Nameservers: %s") % (ip_cfg.get_nameservers())
+    print ("Domains: %s") % (ip_cfg.get_domains())
+    print ("Searches: %s") % (ip_cfg.get_searches())
     if (family == socket.AF_INET):
-        print ("Domains: %s") % (ip_cfg.get_domains())
-        print ("Searches: %s") % (ip_cfg.get_searches())
-        print("Nameservers:")
-        nameservers = ip_cfg.get_nameservers()
-        for dns in nameservers:
-            print socket.inet_ntop(family, struct.pack("=I", dns))
-    else:
-        print ("Domains: %s") % (ip_cfg.get_domains())
-        print ("Searches: %s") % (ip_cfg.get_searches())
-        print("Nameservers:")
-        num = ip_cfg.get_num_nameservers()
-        for i in range(0,num):
-           dns = ip_cfg.get_nameserver(i)
-           print socket.inet_ntop(family, dns)
+        print ("WINS: %s") % (ip_cfg.get_wins_servers())
 
 
 if __name__ == "__main__":
index b73d6a6..b44ca1b 100755 (executable)
@@ -1,5 +1,7 @@
 #!/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
 # with this program; if not, write to the Free Software Foundation, Inc.,
 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 #
-# Copyright (C) 2012 Red Hat, Inc.
+# Copyright 2012 - 2014 Red Hat, Inc.
 #
 
-from gi.repository import GLib, NM
+from gi.repository import NM
 
 # This example asks settings service for all configured connections.
 # Unfortunately, at this time since libnm still makes heavy use of
 # GValue and GHashTable (rather than GVariant), libnm isn't fully
 # usable from GObject Introspection-ready languages. Most functions will
-# work fine, but e. g. nm_connection_to_hash() causes assertion failures.
+# work fine, but e.g. nm_connection_to_dbus() causes failures.
 
-main_loop = None
 
 def print_values(setting, key, value, flags, data):
     print "  %s.%s: %s" % (setting.get_name(), key, value)
 
-def connections_read(settings):
+if __name__ == "__main__":
+    # create RemoteSettings object
+    settings = NM.RemoteSettings.new(None)
+
+    # get all connections
     connections = settings.list_connections()
+
+    # print the connections' details
     for c in connections:
-        print "--- %s : %s" % (c.get_id(), c.get_path())
+        print "=== %s : %s ===" % (c.get_id(), c.get_path())
         c.for_each_setting_value(print_values, None)
         print "\n"
-    main_loop.quit()    
-
-if __name__ == "__main__":
-    main_loop = GLib.MainLoop()
-    settings = NM.RemoteSettings.new(None);
-
-    # connections are read asynchronously, so we need to wait for the
-    # settings object to tell us that it's read all connections
-    settings.connect("connections-read", connections_read)
-    main_loop.run()
 
index e46f979..7988923 100755 (executable)
@@ -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,7 +16,7 @@
 # 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.
 #
 
 #
@@ -40,8 +41,27 @@ def commit_cb(connection, error, data):
         print(error)
     main_loop.quit()
 
-def connections_read_cb(settings, data):
-    uuid, method, args = data
+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)
+
+    uuid = sys.argv[1]
+
+    # Convert method to NM method
+    if method == "static":
+        method = "manual"
+
+    main_loop = GLib.MainLoop()
+
+    # create RemoteSettings object
+    settings = NM.RemoteSettings.new(None)
 
     all_connections = settings.list_connections()
     for c in all_connections:
@@ -70,27 +90,6 @@ def connections_read_cb(settings, data):
 
         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 = NM.RemoteSettings.new(None)
-    settings.connect('connections-read', connections_read_cb, (sys.argv[1], method, sys.argv))
-
+    # run main loop
     main_loop.run()