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  from dbus.exceptions import DBusException 
 34  from _dbus_bindings import ( 
 35      BUS_DAEMON_IFACE, BUS_DAEMON_NAME, BUS_DAEMON_PATH, BUS_SESSION, 
 36      BUS_STARTER, BUS_SYSTEM, DBUS_START_REPLY_ALREADY_RUNNING, 
 37      DBUS_START_REPLY_SUCCESS, validate_bus_name, 
 38      validate_interface_name, validate_member_name, validate_object_path) 
 39  from dbus.bus import BusConnection 
 40  from dbus.lowlevel import SignalMessage 
 41  from dbus._compat import is_py2 
 42   
 43  if is_py2: 
 44      from _dbus_bindings import UTF8String 
 45   
 46   
47 -class Bus(BusConnection):
48 """A connection to one of three possible standard buses, the SESSION, 49 SYSTEM, or STARTER bus. This class manages shared connections to those 50 buses. 51 52 If you're trying to subclass `Bus`, you may be better off subclassing 53 `BusConnection`, which doesn't have all this magic. 54 """ 55 56 _shared_instances = {} 57
58 - def __new__(cls, bus_type=BusConnection.TYPE_SESSION, private=False, 59 mainloop=None):
60 """Constructor, returning an existing instance where appropriate. 61 62 The returned instance is actually always an instance of `SessionBus`, 63 `SystemBus` or `StarterBus`. 64 65 :Parameters: 66 `bus_type` : cls.TYPE_SESSION, cls.TYPE_SYSTEM or cls.TYPE_STARTER 67 Connect to the appropriate bus 68 `private` : bool 69 If true, never return an existing shared instance, but instead 70 return a private connection. 71 72 :Deprecated: since 0.82.3. Use dbus.bus.BusConnection for 73 private connections. 74 75 `mainloop` : dbus.mainloop.NativeMainLoop 76 The main loop to use. The default is to use the default 77 main loop if one has been set up, or raise an exception 78 if none has been. 79 :Changed: in dbus-python 0.80: 80 converted from a wrapper around a Connection to a Connection 81 subclass. 82 """ 83 if (not private and bus_type in cls._shared_instances): 84 return cls._shared_instances[bus_type] 85 86 # this is a bit odd, but we create instances of the subtypes 87 # so we can return the shared instances if someone tries to 88 # construct one of them (otherwise we'd eg try and return an 89 # instance of Bus from __new__ in SessionBus). why are there 90 # three ways to construct this class? we just don't know. 91 if bus_type == BUS_SESSION: 92 subclass = SessionBus 93 elif bus_type == BUS_SYSTEM: 94 subclass = SystemBus 95 elif bus_type == BUS_STARTER: 96 subclass = StarterBus 97 else: 98 raise ValueError('invalid bus_type %s' % bus_type) 99 100 bus = BusConnection.__new__(subclass, bus_type, mainloop=mainloop) 101 102 bus._bus_type = bus_type 103 104 if not private: 105 cls._shared_instances[bus_type] = bus 106 107 return bus
108
109 - def close(self):
110 t = self._bus_type 111 if self.__class__._shared_instances.get(t) is self: 112 del self.__class__._shared_instances[t] 113 super(Bus, self).close()
114
115 - def get_connection(self):
116 """Return self, for backwards compatibility with earlier dbus-python 117 versions where Bus was not a subclass of Connection. 118 119 :Deprecated: since 0.80.0 120 """ 121 return self
122 _connection = property(get_connection, None, None, 123 """self._connection == self, for backwards 124 compatibility with earlier dbus-python versions 125 where Bus was not a subclass of Connection.""") 126
127 - def get_session(private=False):
128 """Static method that returns a connection to the session bus. 129 130 :Parameters: 131 `private` : bool 132 If true, do not return a shared connection. 133 """ 134 return SessionBus(private=private)
135 136 get_session = staticmethod(get_session) 137
138 - def get_system(private=False):
139 """Static method that returns a connection to the system bus. 140 141 :Parameters: 142 `private` : bool 143 If true, do not return a shared connection. 144 """ 145 return SystemBus(private=private)
146 147 get_system = staticmethod(get_system) 148 149
150 - def get_starter(private=False):
151 """Static method that returns a connection to the starter bus. 152 153 :Parameters: 154 `private` : bool 155 If true, do not return a shared connection. 156 """ 157 return StarterBus(private=private)
158 159 get_starter = staticmethod(get_starter) 160
161 - def __repr__(self):
162 if self._bus_type == BUS_SESSION: 163 name = 'session' 164 elif self._bus_type == BUS_SYSTEM: 165 name = 'system' 166 elif self._bus_type == BUS_STARTER: 167 name = 'starter' 168 else: 169 name = 'unknown bus type' 170 171 return '<%s.%s (%s) at %#x>' % (self.__class__.__module__, 172 self.__class__.__name__, 173 name, id(self))
174 __str__ = __repr__
175 176 177 # FIXME: Drop the subclasses here? I can't think why we'd ever want 178 # polymorphism
179 -class SystemBus(Bus):
180 """The system-wide message bus."""
181 - def __new__(cls, private=False, mainloop=None):
182 """Return a connection to the system bus. 183 184 :Parameters: 185 `private` : bool 186 If true, never return an existing shared instance, but instead 187 return a private connection. 188 `mainloop` : dbus.mainloop.NativeMainLoop 189 The main loop to use. The default is to use the default 190 main loop if one has been set up, or raise an exception 191 if none has been. 192 """ 193 return Bus.__new__(cls, Bus.TYPE_SYSTEM, mainloop=mainloop, 194 private=private)
195
196 -class SessionBus(Bus):
197 """The session (current login) message bus."""
198 - def __new__(cls, private=False, mainloop=None):
199 """Return a connection to the session bus. 200 201 :Parameters: 202 `private` : bool 203 If true, never return an existing shared instance, but instead 204 return a private connection. 205 `mainloop` : dbus.mainloop.NativeMainLoop 206 The main loop to use. The default is to use the default 207 main loop if one has been set up, or raise an exception 208 if none has been. 209 """ 210 return Bus.__new__(cls, Bus.TYPE_SESSION, private=private, 211 mainloop=mainloop)
212
213 -class StarterBus(Bus):
214 """The bus that activated this process (only valid if 215 this process was launched by DBus activation). 216 """
217 - def __new__(cls, private=False, mainloop=None):
218 """Return a connection to the bus that activated this process. 219 220 :Parameters: 221 `private` : bool 222 If true, never return an existing shared instance, but instead 223 return a private connection. 224 `mainloop` : dbus.mainloop.NativeMainLoop 225 The main loop to use. The default is to use the default 226 main loop if one has been set up, or raise an exception 227 if none has been. 228 """ 229 return Bus.__new__(cls, Bus.TYPE_STARTER, private=private, 230 mainloop=mainloop)
231