Skip to content

Instantly share code, notes, and snippets.

@ikolar
Created July 21, 2009 07:15
Show Gist options
  • Save ikolar/151180 to your computer and use it in GitHub Desktop.
Save ikolar/151180 to your computer and use it in GitHub Desktop.
# Copyright 2009 Igor Kolar <igor.kolar@gmail.com>
#
# Licensed under the EUPL, Version 1.1 or – as soon they
# will be approved by the European Commission - subsequent
# versions of the EUPL (the "Licence");
# You may not use this work except in compliance with the
# Licence.
# You may obtain a copy of the Licence at:
# http://ec.europa.eu/idabc/eupl5
#
# Unless required by applicable law or agreed to in
# writing, software distributed under the Licence is
# distributed on an "AS IS" basis,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
# express or implied.
# See the Licence for the specific language governing
# permissions and limitations under the Licence.
import dbus # dbus wrapper
import gobject # dbus main loop
from dbus.mainloop.glib import DBusGMainLoop
import libxml2 # for parsing introspection replies
"""
Useful example of the type of info available on the dbus
list properties of detected WiFi access points
- object org.freedesktop.NetworkManager
- path /org/freedesktop/NetworkManager/AccessPoints
"""
def list_access_points():
bus = dbus.SystemBus()
for ap in list_objects(bus, 'org.freedesktop.NetworkManager', path="/org/freedesktop/NetworkManager/AccessPoint"):
proxy = bus.get_object('org.freedesktop.NetworkManager', ap)
interface = dbus.Interface(proxy, "org.freedesktop.DBus.Properties")
ssid = ""
ssid_bytes = interface.Get("", "Ssid") # dbus.Array
for byte in ssid_bytes:
ssid += str(byte)
print "Access point '%s'" % (ssid,)
props = interface.GetAll("")
del props['Ssid']
for prop in props.keys():
print "\t%s => %s" % (prop, props[prop])
"""
list all sub-paths for specific proxy name, starting with the specified path
>>> print list_objects(dbus.SystemBus(), 'org.freedesktop.NetworkManager')
>>> print list_objects(dbus.SystemBus(), 'org.freedesktop.NetworkManager', path="/org/freedesktop/NetworkManager/AccessPoint")
"""
def list_objects(bus, name, path="/", include_initial=False):
results = []
if include_initial:
results.append(path)
proxy = bus.get_object(name, path)
interface = dbus.Interface(proxy, "org.freedesktop.DBus.Introspectable")
xml = interface.Introspect()
doc = libxml2.parseDoc(xml)
for child in doc.xpathEval("/node/node/@name"):
child_path = "%s/%s" % (path, child.content)
if child_path.startswith("//"):
child_path = child_path[1:]
if not include_initial:
results.append(child_path)
results.extend(list_objects(bus, name, child_path))
return results
"""
list all the named objects currently exported on the passed dbus
most will have generic names like :1.18, some verbose ones like "org.gnome.SettingsDaemon"
"""
def list_names(bus):
names = map(lambda x: unicode(x), bus.list_names())
names.sort()
print names
def listen():
# setup event loop and start it
DBusGMainLoop(set_as_default=True)
loop = gobject.MainLoop()
bus = dbus.SessionBus()
proxy = bus.get_object("org.gnome.Totem", "/org/gnome/Totem/Signal")
interface = dbus.interface(proxy, "org.gnome.Totem")
interface.connect_to_singal("Buurek", burekHandler, sender_keyword="sender")
loop.run()
def burekHandler(sender=None):
print "got signal from %r" % sender
list_access_points()
# list_names(dbus.SessionBus())
# listen()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment