.gitignore: ignore the generated Makefile
[dbus-python-phuang.git] / examples / example-signal-recipient.py
blob3d8a1906260ecbeac761bcedfb81df3167fa8a9b
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.decorators
16 import dbus.mainloop.glib
18 def handle_reply(msg):
19 print msg
21 def handle_error(e):
22 print str(e)
24 def emit_signal():
25 #call the emitHelloSignal method
26 object.emitHelloSignal(dbus_interface="com.example.TestService")
27 #reply_handler = handle_reply, error_handler = handle_error)
28 # exit after waiting a short time for the signal
29 gobject.timeout_add(2000, loop.quit)
31 if sys.argv[1:] == ['--exit-service']:
32 object.Exit(dbus_interface='com.example.TestService')
34 return False
36 def hello_signal_handler(hello_string):
37 print ("Received signal (by connecting using remote object) and it says: "
38 + hello_string)
40 def catchall_signal_handler(*args, **kwargs):
41 print ("Caught signal (in catchall handler) "
42 + kwargs['dbus_interface'] + "." + kwargs['member'])
43 for arg in args:
44 print " " + str(arg)
46 def catchall_hello_signals_handler(hello_string):
47 print "Received a hello signal and it says " + hello_string
49 def catchall_testservice_interface_handler(hello_string, dbus_message):
50 print "com.example.TestService interface says " + hello_string + " when it sent signal " + dbus_message.get_member()
53 if __name__ == '__main__':
54 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
56 bus = dbus.SessionBus()
57 try:
58 object = bus.get_object("com.example.TestService","/com/example/TestService/object")
60 object.connect_to_signal("HelloSignal", hello_signal_handler, dbus_interface="com.example.TestService", arg0="Hello")
61 except dbus.DBusException:
62 traceback.print_exc()
63 print usage
64 sys.exit(1)
66 #lets make a catchall
67 bus.add_signal_receiver(catchall_signal_handler, interface_keyword='dbus_interface', member_keyword='member')
69 bus.add_signal_receiver(catchall_hello_signals_handler, dbus_interface = "com.example.TestService", signal_name = "HelloSignal")
71 bus.add_signal_receiver(catchall_testservice_interface_handler, dbus_interface = "com.example.TestService", message_keyword='dbus_message')
73 # Tell the remote object to emit the signal after a short delay
74 gobject.timeout_add(2000, emit_signal)
76 loop = gobject.MainLoop()
77 loop.run()