From 85909d080f74c85f91f418909def3997fc4e898f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ji=C5=99=C3=AD=20Klime=C5=A1?= Date: Fri, 5 Sep 2014 13:48:56 +0200 Subject: [PATCH] examples: fix python GI examples to work after libnm changes --- examples/python/gi/add_connection.py | 8 ++-- examples/python/gi/firewall-zone.py | 26 +++++-------- examples/python/gi/get_ips.py | 20 +++------- examples/python/gi/list-connections.py | 29 +++++++-------- examples/python/gi/update-ip4-method.py | 49 ++++++++++++------------- 5 files changed, 56 insertions(+), 76 deletions(-) diff --git a/examples/python/gi/add_connection.py b/examples/python/gi/add_connection.py index 86a34a1cbe..10623b6ec6 100755 --- a/examples/python/gi/add_connection.py +++ b/examples/python/gi/add_connection.py @@ -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) diff --git a/examples/python/gi/firewall-zone.py b/examples/python/gi/firewall-zone.py index 068ed89c71..8ec59fa0e1 100755 --- a/examples/python/gi/firewall-zone.py +++ b/examples/python/gi/firewall-zone.py @@ -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 [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 [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() - diff --git a/examples/python/gi/get_ips.py b/examples/python/gi/get_ips.py index 1d110bfd41..f2f1e46ed3 100755 --- a/examples/python/gi/get_ips.py +++ b/examples/python/gi/get_ips.py @@ -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__": diff --git a/examples/python/gi/list-connections.py b/examples/python/gi/list-connections.py index b73d6a6c27..b44ca1b858 100755 --- a/examples/python/gi/list-connections.py +++ b/examples/python/gi/list-connections.py @@ -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,36 +17,31 @@ # 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() diff --git a/examples/python/gi/update-ip4-method.py b/examples/python/gi/update-ip4-method.py index e46f979be8..79889232ca 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,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 [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 [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()