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

Source Code for Module dbus._dbus

  1  """Implementation for dbus.Bus. Not to be imported directly.""" 
  2   
  3  # Copyright (C) 2003, 2004, 2005, 2006 Red Hat Inc. <http://www.redhat.com/> 
  4  # Copyright (C) 2003 David Zeuthen 
  5  # Copyright (C) 2004 Rob Taylor 
  6  # Copyright (C) 2005, 2006 Collabora Ltd. <http://www.collabora.co.uk/> 
  7  # 
  8  # Permission is hereby granted, free of charge, to any person 
  9  # obtaining a copy of this software and associated documentation 
 10  # files (the "Software"), to deal in the Software without 
 11  # restriction, including without limitation the rights to use, copy, 
 12  # modify, merge, publish, distribute, sublicense, and/or sell copies 
 13  # of the Software, and to permit persons to whom the Software is 
 14  # furnished to do so, subject to the following conditions: 
 15  # 
 16  # The above copyright notice and this permission notice shall be 
 17  # included in all copies or substantial portions of the Software. 
 18  # 
 19  # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
 20  # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
 21  # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
 22  # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
 23  # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
 24  # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 25  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 26  # DEALINGS IN THE SOFTWARE. 
 27   
 28  from __future__ import generators 
 29   
 30  __all__ = ('Bus', 'SystemBus', 'SessionBus', 'StarterBus') 
 31  __docformat__ = 'reStructuredText' 
 32   
 33  import os 
 34  import sys 
 35  import weakref 
 36  from traceback import print_exc 
 37   
 38  from dbus.exceptions import DBusException 
 39  from _dbus_bindings import BUS_DAEMON_NAME, BUS_DAEMON_PATH,\ 
 40                             BUS_DAEMON_IFACE, UTF8String,\ 
 41                             validate_member_name, validate_interface_name,\ 
 42                             validate_bus_name, validate_object_path,\ 
 43                             BUS_SESSION, BUS_SYSTEM, BUS_STARTER,\ 
 44                             DBUS_START_REPLY_SUCCESS, \ 
 45                             DBUS_START_REPLY_ALREADY_RUNNING 
 46  from dbus.bus import BusConnection 
 47  from dbus.lowlevel import SignalMessage 
 48   
 49  try: 
 50      import thread 
 51  except ImportError: 
 52      import dummy_thread as thread 
 53   
 54   
55 -class Bus(BusConnection):
56 """A connection to one of three possible standard buses, the SESSION, 57 SYSTEM, or STARTER bus. This class manages shared connections to those 58 buses. 59 60 If you're trying to subclass `Bus`, you may be better off subclassing 61 `BusConnection`, which doesn't have all this magic. 62 """ 63 64 _shared_instances = {} 65
66 - def __new__(cls, bus_type=BusConnection.TYPE_SESSION, private=False, 67 mainloop=None):
68 """Constructor, returning an existing instance where appropriate. 69 70 The returned instance is actually always an instance of `SessionBus`, 71 `SystemBus` or `StarterBus`. 72 73 :Parameters: 74 `bus_type` : cls.TYPE_SESSION, cls.TYPE_SYSTEM or cls.TYPE_STARTER 75 Connect to the appropriate bus 76 `private` : bool 77 If true, never return an existing shared instance, but instead 78 return a private connection. 79 80 :Deprecated: since 0.82.3. Use dbus.bus.BusConnection for 81 private connections. 82 83 `mainloop` : dbus.mainloop.NativeMainLoop 84 The main loop to use. The default is to use the default 85 main loop if one has been set up, or raise an exception 86 if none has been. 87 :Changed: in dbus-python 0.80: 88 converted from a wrapper around a Connection to a Connection 89 subclass. 90 """ 91 if (not private and bus_type in cls._shared_instances): 92 return cls._shared_instances[bus_type] 93 94 # this is a bit odd, but we create instances of the subtypes 95 # so we can return the shared instances if someone tries to 96 # construct one of them (otherwise we'd eg try and return an 97 # instance of Bus from __new__ in SessionBus). why are there 98 # three ways to construct this class? we just don't know. 99 if bus_type == BUS_SESSION: 100 subclass = SessionBus 101 elif bus_type == BUS_SYSTEM: 102 subclass = SystemBus 103 elif bus_type == BUS_STARTER: 104 subclass = StarterBus 105 else: 106 raise ValueError('invalid bus_type %s' % bus_type) 107 108 bus = BusConnection.__new__(subclass, bus_type, mainloop=mainloop) 109 110 bus._bus_type = bus_type 111 112 if not private: 113 cls._shared_instances[bus_type] = bus 114 115 return bus
116
117 - def close(self):
118 t = self._bus_type 119 if self.__class__._shared_instances.get(t) is self: 120 del self.__class__._shared_instances[t] 121 super(Bus, self).close()
122
123 - def get_connection(self):
124 """Return self, for backwards compatibility with earlier dbus-python 125 versions where Bus was not a subclass of Connection. 126 127 :Deprecated: since 0.80.0 128 """ 129 return self
130 _connection = property(get_connection, None, None, 131 """self._connection == self, for backwards 132 compatibility with earlier dbus-python versions 133 where Bus was not a subclass of Connection.""") 134
135 - def get_session(private=False):
136 """Static method that returns a connection to the session bus. 137 138 :Parameters: 139 `private` : bool 140 If true, do not return a shared connection. 141 """ 142 return SessionBus(private=private)
143 144 get_session = staticmethod(get_session) 145
146 - def get_system(private=False):
147 """Static method that returns a connection to the system bus. 148 149 :Parameters: 150 `private` : bool 151 If true, do not return a shared connection. 152 """ 153 return SystemBus(private=private)
154 155 get_system = staticmethod(get_system) 156 157
158 - def get_starter(private=False):
159 """Static method that returns a connection to the starter bus. 160 161 :Parameters: 162 `private` : bool 163 If true, do not return a shared connection. 164 """ 165 return StarterBus(private=private)
166 167 get_starter = staticmethod(get_starter) 168
169 - def __repr__(self):
170 if self._bus_type == BUS_SESSION: 171 name = 'session' 172 elif self._bus_type == BUS_SYSTEM: 173 name = 'system' 174 elif self._bus_type == BUS_STARTER: 175 name = 'starter' 176 else: 177 name = 'unknown bus type' 178 179 return '<%s.%s (%s) at %#x>' % (self.__class__.__module__, 180 self.__class__.__name__, 181 name, id(self))
182 __str__ = __repr__
183 184 185 # FIXME: Drop the subclasses here? I can't think why we'd ever want 186 # polymorphism
187 -class SystemBus(Bus):
188 """The system-wide message bus."""
189 - def __new__(cls, private=False, mainloop=None):
190 """Return a connection to the system bus. 191 192 :Parameters: 193 `private` : bool 194 If true, never return an existing shared instance, but instead 195 return a private connection. 196 `mainloop` : dbus.mainloop.NativeMainLoop 197 The main loop to use. The default is to use the default 198 main loop if one has been set up, or raise an exception 199 if none has been. 200 """ 201 return Bus.__new__(cls, Bus.TYPE_SYSTEM, mainloop=mainloop, 202 private=private)
203
204 -class SessionBus(Bus):
205 """The session (current login) message bus."""
206 - def __new__(cls, private=False, mainloop=None):
207 """Return a connection to the session bus. 208 209 :Parameters: 210 `private` : bool 211 If true, never return an existing shared instance, but instead 212 return a private connection. 213 `mainloop` : dbus.mainloop.NativeMainLoop 214 The main loop to use. The default is to use the default 215 main loop if one has been set up, or raise an exception 216 if none has been. 217 """ 218 return Bus.__new__(cls, Bus.TYPE_SESSION, private=private, 219 mainloop=mainloop)
220
221 -class StarterBus(Bus):
222 """The bus that activated this process (only valid if 223 this process was launched by DBus activation). 224 """
225 - def __new__(cls, private=False, mainloop=None):
226 """Return a connection to the bus that activated this process. 227 228 :Parameters: 229 `private` : bool 230 If true, never return an existing shared instance, but instead 231 return a private connection. 232 `mainloop` : dbus.mainloop.NativeMainLoop 233 The main loop to use. The default is to use the default 234 main loop if one has been set up, or raise an exception 235 if none has been. 236 """ 237 return Bus.__new__(cls, Bus.TYPE_STARTER, private=private, 238 mainloop=mainloop)
239 240 241 if 'DBUS_PYTHON_NO_DEPRECATED' not in os.environ: 242
243 - class _DBusBindingsEmulation:
244 """A partial emulation of the dbus_bindings module."""
245 - def __str__(self):
246 return '_DBusBindingsEmulation()'
247 - def __repr__(self):
248 return '_DBusBindingsEmulation()'
249 - def __getattr__(self, attr):
250 global dbus_bindings 251 import dbus.dbus_bindings as m 252 dbus_bindings = m 253 return getattr(m, attr)
254 255 dbus_bindings = _DBusBindingsEmulation() 256 """Deprecated, don't use.""" 257