Branched from 2.0alpha and pushed for 2.0
[profile/ivi/dbus-python.git] / dbus / gobject_service.py
1 """Support code for implementing D-Bus services via GObjects."""
2
3 # Copyright (C) 2007 Collabora Ltd. <http://www.collabora.co.uk/>
4 #
5 # Permission is hereby granted, free of charge, to any person
6 # obtaining a copy of this software and associated documentation
7 # files (the "Software"), to deal in the Software without
8 # restriction, including without limitation the rights to use, copy,
9 # modify, merge, publish, distribute, sublicense, and/or sell copies
10 # of the Software, and to permit persons to whom the Software is
11 # furnished to do so, subject to the following conditions:
12 #
13 # The above copyright notice and this permission notice shall be
14 # included in all copies or substantial portions of the Software.
15 #
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 # DEALINGS IN THE SOFTWARE.
24
25 import gobject
26 import dbus.service
27
28 class ExportedGObjectType(gobject.GObjectMeta, dbus.service.InterfaceType):
29     """A metaclass which inherits from both GObjectMeta and
30     `dbus.service.InterfaceType`. Used as the metaclass for `ExportedGObject`.
31     """
32     def __init__(cls, name, bases, dct):
33         gobject.GObjectMeta.__init__(cls, name, bases, dct)
34         dbus.service.InterfaceType.__init__(cls, name, bases, dct)
35
36 class ExportedGObject(gobject.GObject, dbus.service.Object):
37     """A GObject which is exported on the D-Bus.
38
39     Because GObject and `dbus.service.Object` both have custom metaclasses,
40     the naive approach using simple multiple inheritance won't work. This
41     class has `ExportedGObjectType` as its metaclass, which is sufficient
42     to make it work correctly.
43     """
44     __metaclass__ = ExportedGObjectType
45
46     def __init__(self, conn=None, object_path=None, **kwargs):
47         """Initialize an exported GObject.
48
49         :Parameters:
50             `conn` : dbus.connection.Connection
51                 The D-Bus connection or bus
52             `object_path` : str
53                 The object path at which to register this object.
54         :Keywords:
55             `bus_name` : dbus.service.BusName
56                 A bus name to be held on behalf of this object, or None.
57             `gobject_properties` : dict
58                 GObject properties to be set on the constructed object.
59
60                 Any unrecognised keyword arguments will also be interpreted
61                 as GObject properties.
62             """
63         bus_name = kwargs.pop('bus_name', None)
64         gobject_properties = kwargs.pop('gobject_properties', None)
65
66         if gobject_properties is not None:
67             kwargs.update(gobject_properties)
68         gobject.GObject.__init__(self, **kwargs)
69         dbus.service.Object.__init__(self, conn=conn,
70                                      object_path=object_path,
71                                      bus_name=bus_name)