Try to avoid importing things from _dbus_bindings when they could be imported from...
[dbus-python-phuang.git] / examples / example-signal-recipient.py
blob65c6466f10a486f6ac83e1be2d78dd8020802faa
1 #!/usr/bin/env python
3 usage = """Usage:
4 python example-signal-emitter.py &
5 python example-signal-recipient.py
6 python example-signal-recipient.py --exit-service
7 """
9 import sys
10 import traceback
12 import gobject
14 import dbus
15 import dbus.mainloop.glib
17 def handle_reply(msg):
18 print msg
20 def handle_error(e):
21 print str(e)
23 def emit_signal():
24 #call the emitHelloSignal method
25 object.emitHelloSignal(dbus_interface="com.example.TestService")
26 #reply_handler = handle_reply, error_handler = handle_error)
27 # exit after waiting a short time for the signal
28 gobject.timeout_add(2000, loop.quit)
30 if sys.argv[1:] == ['--exit-service']:
31 object.Exit(dbus_interface='com.example.TestService')
33 return False
35 def hello_signal_handler(hello_string):
36 print ("Received signal (by connecting using remote object) and it says: "
37 + hello_string)
39 def catchall_signal_handler(*args, **kwargs):
40 print ("Caught signal (in catchall handler) "
41 + kwargs['dbus_interface'] + "." + kwargs['member'])
42 for arg in args:
43 print " " + str(arg)
45 def catchall_hello_signals_handler(hello_string):
46 print "Received a hello signal and it says " + hello_string
48 def catchall_testservice_interface_handler(hello_string, dbus_message):
49 print "com.example.TestService interface says " + hello_string + " when it sent signal " + dbus_message.get_member()
52 if __name__ == '__main__':
53 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
55 bus = dbus.SessionBus()
56 try:
57 object = bus.get_object("com.example.TestService","/com/example/TestService/object")
59 object.connect_to_signal("HelloSignal", hello_signal_handler, dbus_interface="com.example.TestService", arg0="Hello")
60 except dbus.DBusException:
61 traceback.print_exc()
62 print usage
63 sys.exit(1)
65 #lets make a catchall
66 bus.add_signal_receiver(catchall_signal_handler, interface_keyword='dbus_interface', member_keyword='member')
68 bus.add_signal_receiver(catchall_hello_signals_handler, dbus_interface = "com.example.TestService", signal_name = "HelloSignal")
70 bus.add_signal_receiver(catchall_testservice_interface_handler, dbus_interface = "com.example.TestService", message_keyword='dbus_message')
72 # Tell the remote object to emit the signal after a short delay
73 gobject.timeout_add(2000, emit_signal)
75 loop = gobject.MainLoop()
76 loop.run()