mpd_watcher: properly schedule all IO via gobject
[~helmut/onoff.git] / dbus_client.py
index a675eb7..fbaf33a 100755 (executable)
@@ -8,7 +8,6 @@ released.
 
 import argparse
 import os
-import xml.parsers.expat
 
 from dbus.mainloop.glib import DBusGMainLoop
 from gi.repository import GObject
@@ -16,30 +15,13 @@ from gi.repository import GObject
 from onoff.common import ST_ACTIVE
 import onoff.dbusutils
 
-def wait_for_signal(proxy, signal):
+def wait_for_signal(proxy, signal, desired_state):
     loop = GObject.MainLoop()
-    state = []
     def callback(st):
-        state.append(st)
-        loop.quit()
+        if st == desired_state:
+            loop.quit()
     proxy.connect_to_signal(signal, callback)
     loop.run()
-    return state[0]
-
-def parse_introspection(xmlstring):
-    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
 
 def main():
     parser = argparse.ArgumentParser(parents=[onoff.dbusutils.dbus_options])
@@ -55,8 +37,7 @@ def main():
     DBusGMainLoop(set_as_default=True)
     if args.list:
         bus = onoff.dbusutils.get_dbus(args)
-        proxy = bus.get_object(args.busname, onoff.dbusutils.object_prefix)
-        for elem in parse_introspection(proxy.Introspect()):
+        for elem in onoff.dbusutils.list_objects(bus, args.busname):
             print(elem)
     elif args.command:
         proxy = onoff.dbusutils.get_dbus_proxy(args)
@@ -66,16 +47,16 @@ def main():
         os.close(fd)
         if st != ST_ACTIVE:
             print("state is %d waiting for signal" % st)
-            st = wait_for_signal(proxy, "changestate")
-            print("new state is %d" % st)
+            wait_for_signal(proxy, "changestate", ST_ACTIVE)
+            print("new state is actived")
         os.execvp(args.command[0], args.command)
     else:
         proxy = onoff.dbusutils.get_dbus_proxy(args)
         st = proxy.activatetime(args.duration)
         if st != ST_ACTIVE:
             print("state is %d waiting for signal" % st)
-            st = wait_for_signal(proxy, "changestate")
-            print("new state is %d" % st)
+            wait_for_signal(proxy, "changestate", ST_ACTIVE)
+            print("new state is active")
 
 if __name__ == "__main__":
     main()