summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorDan Williams <dcbw@redhat.com>2012-06-11 14:46:33 -0500
committerDan Williams <dcbw@redhat.com>2012-06-11 14:46:33 -0500
commit53f6539f54c6c49dcb53de8d1e3cdb3a6a84c960 (patch)
tree10f10965b0df5d6b4945056a53d4bbc819cd45c8 /examples
parentc114344977d6ad3132b29b0163c5d1c4a34ff2c5 (diff)
downloadNetworkManager-53f6539f54c6c49dcb53de8d1e3cdb3a6a84c960.tar.gz
examples: add simply python GObject Introspection example
Unfortunately since libnm-glib/libnm-util make heavy use of GHashTable and GValue, functions that deal with these types can't be used from Python when using GObject Introspection, since pygobject can't handle conversion between python types and GValue/GHashTable very well. You'll likely encounter assertions like: ERROR:pygi-argument.c:1755:_pygi_argument_to_object: assertion failed: (g_type_info_get_tag (key_type_info) != GI_TYPE_TAG_VOID) Aborted
Diffstat (limited to 'examples')
-rw-r--r--examples/python/Makefile.am3
-rwxr-xr-xexamples/python/goi-list-connections.py52
2 files changed, 54 insertions, 1 deletions
diff --git a/examples/python/Makefile.am b/examples/python/Makefile.am
index 59e16088d4..5b465ae3c5 100644
--- a/examples/python/Makefile.am
+++ b/examples/python/Makefile.am
@@ -7,4 +7,5 @@ EXTRA_DIST = \
show-bssids.py \
disconnect-device.py \
get-active-connections.py \
- list-devices.py
+ list-devices.py \
+ goi-list-connections.py
diff --git a/examples/python/goi-list-connections.py b/examples/python/goi-list-connections.py
new file mode 100755
index 0000000000..10b93b8e89
--- /dev/null
+++ b/examples/python/goi-list-connections.py
@@ -0,0 +1,52 @@
+#!/usr/bin/env python
+# -*- Mode: Python; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
+#
+# 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
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# 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.
+#
+
+from gi.repository import GObject, NetworkManager, NMClient
+
+# This example asks settings service for all configured connections.
+# Unfortunately, at this time since libnm-glib still makes heavy use of
+# GValue and GHashTable (rather than GVariant), libnm-glib isn't fully
+# usable from GObject Introspection-ready languages. Most functions will
+# work fine, but those involving connection secrets (like
+# nm_remote_connection_get_secrets() or nm_connection_to_hash()) cause assertion
+# failures since pygobject cannot handle complex GHashTable elements.
+
+main_loop = None
+
+def print_values(setting, key, value, flags, data):
+ print " %s.%s: %s" % (setting.get_name(), key, value)
+
+def connections_read(settings):
+ connections = settings.list_connections()
+ for c in connections:
+ 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 = GObject.MainLoop()
+ settings = NMClient.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()
+