Add wrapper for DBusServer.
[dbus-python-phuang.git] / examples / example-service.py
blobc42b5268054561a2b7d4fa184a82ddad13b340ce
1 #!/usr/bin/env python
3 usage = """Usage:
4 python example-service.py &
5 python example-client.py
6 python example-async-client.py
7 python example-client.py --exit-service
8 """
10 # Copyright (C) 2004-2006 Red Hat Inc. <http://www.redhat.com/>
11 # Copyright (C) 2005-2007 Collabora Ltd. <http://www.collabora.co.uk/>
13 # Permission is hereby granted, free of charge, to any person
14 # obtaining a copy of this software and associated documentation
15 # files (the "Software"), to deal in the Software without
16 # restriction, including without limitation the rights to use, copy,
17 # modify, merge, publish, distribute, sublicense, and/or sell copies
18 # of the Software, and to permit persons to whom the Software is
19 # furnished to do so, subject to the following conditions:
21 # The above copyright notice and this permission notice shall be
22 # included in all copies or substantial portions of the Software.
24 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
28 # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
29 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
31 # DEALINGS IN THE SOFTWARE.
33 import gobject
35 import dbus
36 import dbus.service
37 import dbus.mainloop.glib
39 class DemoException(dbus.DBusException):
40 _dbus_error_name = 'com.example.DemoException'
42 class SomeObject(dbus.service.Object):
44 @dbus.service.method("com.example.SampleInterface",
45 in_signature='s', out_signature='as')
46 def HelloWorld(self, hello_message):
47 print (str(hello_message))
48 return ["Hello", " from example-service.py", "with unique name",
49 session_bus.get_unique_name()]
51 @dbus.service.method("com.example.SampleInterface",
52 in_signature='', out_signature='')
53 def RaiseException(self):
54 raise DemoException('The RaiseException method does what you might '
55 'expect')
57 @dbus.service.method("com.example.SampleInterface",
58 in_signature='', out_signature='(ss)')
59 def GetTuple(self):
60 return ("Hello Tuple", " from example-service.py")
62 @dbus.service.method("com.example.SampleInterface",
63 in_signature='', out_signature='a{ss}')
64 def GetDict(self):
65 return {"first": "Hello Dict", "second": " from example-service.py"}
67 @dbus.service.method("com.example.SampleInterface",
68 in_signature='', out_signature='')
69 def Exit(self):
70 mainloop.quit()
73 if __name__ == '__main__':
74 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
76 session_bus = dbus.SessionBus()
77 name = dbus.service.BusName("com.example.SampleService", session_bus)
78 object = SomeObject(session_bus, '/SomeObject')
80 mainloop = gobject.MainLoop()
81 print "Running example service."
82 print usage
83 mainloop.run()