/
DBusTree.py
55 lines (46 loc) · 1.89 KB
/
DBusTree.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from dbus.exceptions import DBusException
from dbus.mainloop.glib import DBusGMainLoop
from xml.dom import minidom
import dbus
import logging
logging.disable(logging.CRITICAL)
def findInterfaces(bus, path):
introspect = sessionBus.get_object(bus, path).Introspect(dbus_interface = 'org.freedesktop.DBus.Introspectable')
dom = minidom.parseString(introspect)
# Gather all interfaces & methods
for interface in dom.getElementsByTagName("interface"):
print "\t\tInterface: %s" % interface.getAttribute("name")
for method in interface.getElementsByTagName("method"):
print "\t\t\tMethod: %s" % method.getAttribute("name")
def findPaths(bus, path = "/"):
try:
introspect = sessionBus.get_object(bus, path).Introspect(dbus_interface = 'org.freedesktop.DBus.Introspectable')
dom = minidom.parseString(introspect)
dom = dom.documentElement # Ignore the root node
newPath = path
if newPath[-1] is not "/":
newPath = newPath + "/"
# Grab all available nodes
nodes = dom.getElementsByTagName("node")
for node in nodes:
foundPath = newPath + node.getAttribute("name")
findPaths(bus, foundPath)
# If this path has more content, then print
print "\tPath: %s" % path
findInterfaces(bus, path)
except DBusException: pass
busAliases = {}
uniqueBuses = []
sessionBus = dbus.SessionBus(mainloop = DBusGMainLoop())
# Gather all common names and map them to unique names
for bus in sessionBus.list_names():
if not bus.startswith(":"):
busAliases[str(sessionBus.get_name_owner(bus))] = bus
else: uniqueBuses.append(bus)
# Display common names with unique names
for bus in uniqueBuses:
readableName = ""
if bus in busAliases.keys():
readableName = "%s" % busAliases.get(bus)
print "Bus: %s (%s)" % (bus, readableName)
findPaths(bus)