examples: group python examples - dbus vs. gi

Move examples using dbus-python ('dbus' module) and GObject introspection into
their own directories.
This commit is contained in:
Jiří Klimeš
2013-09-24 12:35:19 +02:00
parent 217cb5fbca
commit 8a04ab9135
17 changed files with 19 additions and 14 deletions

View File

@@ -0,0 +1,4 @@
EXTRA_DIST = \
list-connections.py \
device-state-ip4config.py \
firewall-zone.py

View File

@@ -0,0 +1,64 @@
#!/usr/bin/env python
#
# vim: ft=python ts=4 sts=4 sw=4 et ai
# -*- 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.
#
import sys
from gi.repository import GObject, NetworkManager, NMClient
#
# This example shows how to get NMIP4Config from NMDevice after it is activated.
#
# We listen to notify::ip4-config glib signal. This signal is trigered by D-Bus
# PropertiesChanged for IP4Config that comes after StateChanged for NMDevice.
#
main_loop = None
def do_notify(self, property):
print "notify: %s" % property
ip4cfg = self.get_ip4_config()
if ip4cfg is not None:
print "ip4-config: %s" % ip4cfg.get_path()
main_loop.quit()
def state_changed(obj, arg1, arg2, arg3):
print "State changed: New: %d, Old: %d, Reason: %d" % (arg1, arg2, arg3)
# Device is connected
if arg1 == 100:
obj.connect('notify::ip4-config', do_notify)
if __name__ == "__main__":
if len(sys.argv) != 2:
sys.exit('Usage: %s <interface>' % sys.argv[0])
dev_iface = sys.argv[1]
c = NMClient.Client.new()
dev = c.get_device_by_iface(dev_iface)
if dev is None:
sys.exit('Device \'%s\' not found' % dev_iface)
print "Device: %s - %s" % (dev_iface, dev.get_device_type().value_name)
print "---------------------------------------"
dev.connect('state-changed', state_changed)
main_loop = GObject.MainLoop()
main_loop.run()

View File

@@ -0,0 +1,87 @@
#!/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
# 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) 2013 Red Hat, Inc.
#
import sys
from gi.repository import GObject, NetworkManager, NMClient
#
# This example demonstrates how to get and change firewall zone in a
# connection. It uses GObject Introspection instead of direct D-Bus calls.
# 'zone' is a property of 'connection' setting in a connection. You can't
# get/set individual properties directly. Rather you ask for the whole
# connection, change a property and update the connection back into
# NetworkManager.
# If you used D-Bus calls, you would call GetSettings() and then Update().
#
# Links:
# https://projects.gnome.org/NetworkManager/developers/libnm-glib/09/index.html
# https://wiki.gnome.org/GObjectIntrospection
# https://wiki.gnome.org/PyGObject
#
main_loop = None
def connection_saved(connection, error, data):
print ("Connection '%s' saved.") % (connection.get_id())
main_loop.quit()
def connections_read(settings, data):
con_name = sys.argv[1]
if len(sys.argv) == 3:
new_zone = sys.argv[2]
else:
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
s_con = c.get_setting_connection()
if new_zone is None:
zone = s_con.get_zone()
if zone is not None:
print("'%s' zone is '%s'") % (c.get_id(), zone)
else:
print("'%s' zone is empty") % (c.get_id())
main_loop.quit()
else:
s_con.set_property("zone", new_zone)
c.commit_changes(connection_saved, None)
print("'%s' zone set to '%s'") % (c.get_id(), new_zone)
break
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 = GObject.MainLoop()
settings = NMClient.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()

View File

@@ -0,0 +1,50 @@
#!/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 e. g. nm_connection_to_hash() causes assertion 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):
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()