Package dbus :: Module gi_service
[hide private]
[frames] | no frames]

Source Code for Module dbus.gi_service

 1  """Support code for implementing D-Bus services via PyGI.""" 
 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  __all__ = ['ExportedGObject'] 
26   
27  from gi.repository import GObject 
28  import dbus.service 
29   
30  # The odd syntax used here is required so that the code is compatible with 
31  # both Python 2 and Python 3.  It essentially creates a new class called 
32  # ExportedGObject with a metaclass of ExportGObjectType and an __init__() 
33  # function. 
34  # 
35  # Because GObject and `dbus.service.Object` both have custom metaclasses, the 
36  # naive approach using simple multiple inheritance won't work. This class has 
37  # `ExportedGObjectType` as its metaclass, which is sufficient to make it work 
38  # correctly. 
39   
40 -class ExportedGObjectType(GObject.GObjectMeta, dbus.service.InterfaceType):
41 """A metaclass which inherits from both GObjectMeta and 42 `dbus.service.InterfaceType`. Used as the metaclass for `ExportedGObject`. 43 """
44 - def __init__(cls, name, bases, dct):
45 GObject.GObjectMeta.__init__(cls, name, bases, dct) 46 dbus.service.InterfaceType.__init__(cls, name, bases, dct)
47 48
49 -def ExportedGObject__init__(self, conn=None, object_path=None, **kwargs):
50 """Initialize an exported GObject. 51 52 :Parameters: 53 `conn` : dbus.connection.Connection 54 The D-Bus connection or bus 55 `object_path` : str 56 The object path at which to register this object. 57 :Keywords: 58 `bus_name` : dbus.service.BusName 59 A bus name to be held on behalf of this object, or None. 60 `gobject_properties` : dict 61 GObject properties to be set on the constructed object. 62 63 Any unrecognised keyword arguments will also be interpreted 64 as GObject properties. 65 """ 66 bus_name = kwargs.pop('bus_name', None) 67 gobject_properties = kwargs.pop('gobject_properties', None) 68 69 if gobject_properties is not None: 70 kwargs.update(gobject_properties) 71 GObject.GObject.__init__(self, **kwargs) 72 dbus.service.Object.__init__(self, conn=conn, 73 object_path=object_path, 74 bus_name=bus_name)
75 76 ExportedGObject__doc__ = 'A GObject which is exported on the D-Bus.' 77 78 ExportedGObject = ExportedGObjectType( 79 'ExportedGObject', 80 (GObject.GObject, dbus.service.Object), 81 {'__init__': ExportedGObject__init__, 82 '__doc__': ExportedGObject__doc__, 83 }) 84