import argparse
import logging
import socket
+import xml.parsers.expat
import dbus
import dbus.service
dbus_options.add_argument("--busname", type=str, default=default_busname,
help="which busname (i.e. client) to use " +
"(default: %(default)s)")
-dbus_options.add_argument("--device", type=str, required=True,
+dbus_options.add_argument("--device", type=str,
help="which device to control")
-def get_dbus_proxy(namespace):
+def get_dbus(namespace):
"""
@param namespace: a namespace returned from a dbus_options argument parser
- @returns: a dbus object proxy
+ @rtype: dbus.Bus
+ @returns: the requested bus
"""
if namespace.bus == "session":
- bus = dbus.SessionBus()
+ return dbus.SessionBus()
elif namespace.bus == "system":
- bus = dbus.SystemBus()
+ return dbus.SystemBus()
else:
raise AssertionError("namespace.bus %r is neither session nor system",
namespace.bus)
+
+def get_dbus_proxy(namespace):
+ """
+ @param namespace: a namespace returned from a dbus_options argument parser
+ @returns: a dbus object proxy
+ """
+ bus = get_dbus(namespace)
+ if not namespace.device:
+ raise ValueError("no --device given")
objname = "%s/%s" % (object_prefix, namespace.device)
return bus.get_object(namespace.busname, objname)
s2.close()
return s1, s3
+def list_objects(bus, busname, path=None):
+ """List objects on the given bus and busname starting with path. Only the
+ trailing components after the slash are returned.
+
+ @type bus: dbus.Bus
+ @type busname: str
+ @type path: None or str
+ @param path: prefix for searching objects. Defaults to
+ dbusutils.object_prefix.
+ @rtype: [str]
+ @returns: the trailing components of the objects found
+ """
+ if path is None:
+ path = object_prefix
+ xmlstring = bus.get_object(busname, path).Introspect()
+ parser = xml.parsers.expat.ParserCreate()
+ nodes = []
+ def start_element(name, attrs):
+ if name != "node":
+ return
+ try:
+ value = attrs["name"]
+ except KeyError:
+ return
+ nodes.append(value)
+ parser.StartElementHandler = start_element
+ parser.Parse(xmlstring)
+ return nodes
+
class OnoffControl(dbus.service.Object):
domain = default_busname
path = object_prefix