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 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
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
87
88
89
90
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
114
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
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
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
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
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
178
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
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
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