Initial import package dbus-python: D-Bus Python Bindings
[external/dbus-python.git] / examples / example-signal-recipient.py
1 #!/usr/bin/env python
2
3 usage = """Usage:
4 python example-signal-emitter.py &
5 python example-signal-recipient.py
6 python example-signal-recipient.py --exit-service
7 """
8
9 # Copyright (C) 2004-2006 Red Hat Inc. <http://www.redhat.com/>
10 # Copyright (C) 2005-2007 Collabora Ltd. <http://www.collabora.co.uk/>
11 #
12 # Permission is hereby granted, free of charge, to any person
13 # obtaining a copy of this software and associated documentation
14 # files (the "Software"), to deal in the Software without
15 # restriction, including without limitation the rights to use, copy,
16 # modify, merge, publish, distribute, sublicense, and/or sell copies
17 # of the Software, and to permit persons to whom the Software is
18 # furnished to do so, subject to the following conditions:
19 #
20 # The above copyright notice and this permission notice shall be
21 # included in all copies or substantial portions of the Software.
22 #
23 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
27 # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
28 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
30 # DEALINGS IN THE SOFTWARE.
31
32 import sys
33 import traceback
34
35 import gobject
36
37 import dbus
38 import dbus.mainloop.glib
39
40 def handle_reply(msg):
41     print msg
42
43 def handle_error(e):
44     print str(e)
45
46 def emit_signal():
47    #call the emitHelloSignal method 
48    object.emitHelloSignal(dbus_interface="com.example.TestService")
49                           #reply_handler = handle_reply, error_handler = handle_error)
50    # exit after waiting a short time for the signal
51    gobject.timeout_add(2000, loop.quit)
52
53    if sys.argv[1:] == ['--exit-service']:
54       object.Exit(dbus_interface='com.example.TestService')
55
56    return False
57
58 def hello_signal_handler(hello_string):
59     print ("Received signal (by connecting using remote object) and it says: "
60            + hello_string)
61
62 def catchall_signal_handler(*args, **kwargs):
63     print ("Caught signal (in catchall handler) "
64            + kwargs['dbus_interface'] + "." + kwargs['member'])
65     for arg in args:
66         print "        " + str(arg)
67
68 def catchall_hello_signals_handler(hello_string):
69     print "Received a hello signal and it says " + hello_string
70     
71 def catchall_testservice_interface_handler(hello_string, dbus_message):
72     print "com.example.TestService interface says " + hello_string + " when it sent signal " + dbus_message.get_member()
73
74
75 if __name__ == '__main__':
76     dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
77
78     bus = dbus.SessionBus()
79     try:
80         object  = bus.get_object("com.example.TestService","/com/example/TestService/object")
81
82         object.connect_to_signal("HelloSignal", hello_signal_handler, dbus_interface="com.example.TestService", arg0="Hello")
83     except dbus.DBusException:
84         traceback.print_exc()
85         print usage
86         sys.exit(1)
87
88     #lets make a catchall
89     bus.add_signal_receiver(catchall_signal_handler, interface_keyword='dbus_interface', member_keyword='member')
90
91     bus.add_signal_receiver(catchall_hello_signals_handler, dbus_interface = "com.example.TestService", signal_name = "HelloSignal")
92
93     bus.add_signal_receiver(catchall_testservice_interface_handler, dbus_interface = "com.example.TestService", message_keyword='dbus_message')
94
95     # Tell the remote object to emit the signal after a short delay
96     gobject.timeout_add(2000, emit_signal)
97
98     loop = gobject.MainLoop()
99     loop.run()