1 """Implementation for dbus.Bus. Not to be imported directly."""
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
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
95
96
97
98
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
122
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
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
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
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
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
186
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
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
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
244 """A partial emulation of the dbus_bindings module."""
246 return '_DBusBindingsEmulation()'
248 return '_DBusBindingsEmulation()'
254
255 dbus_bindings = _DBusBindingsEmulation()
256 """Deprecated, don't use."""
257