1 """Bus mixin, for use within dbus-python only. See `_BusMixin`."""
3 # Copyright (C) 2007 Collabora Ltd. <http://www.collabora.co.uk/>
5 # Licensed under the Academic Free License version 2.1
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU Lesser General Public License as published
9 # by the Free Software Foundation; either version 2.1 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 BUS_DAEMON_NAME
= 'org.freedesktop.DBus'
22 BUS_DAEMON_PATH
= '/org/freedesktop/DBus'
23 BUS_DAEMON_IFACE
= BUS_DAEMON_NAME
25 from _dbus_bindings
import validate_interface_name
, validate_member_name
,\
26 validate_bus_name
, validate_object_path
,\
30 class _BusDaemonMixin(object):
31 """This mixin provides simple blocking wrappers for various methods on
32 the org.freedesktop.DBus bus-daemon object, to reduce the amount of C
36 def get_unix_user(self
, bus_name
):
37 """Get the numeric uid of the process owning the given bus name.
41 A bus name, either unique or well-known
42 :Returns: a `dbus.UInt32`
44 validate_bus_name(bus_name
)
45 return self
.call_blocking(BUS_DAEMON_NAME
, BUS_DAEMON_PATH
,
46 BUS_DAEMON_IFACE
, 'GetConnectionUnixUser',
49 def start_service_by_name(self
, bus_name
, flags
=0):
50 """Start a service which will implement the given bus name on this Bus.
54 The well-known bus name to be activated.
56 Flags to pass to StartServiceByName (currently none are
59 :Returns: A tuple of 2 elements. The first is always True, the
60 second is either START_REPLY_SUCCESS or
61 START_REPLY_ALREADY_RUNNING.
63 :Raises DBusException: if the service could not be started.
65 validate_bus_name(bus_name
)
66 return (True, self
.call_blocking(BUS_DAEMON_NAME
, BUS_DAEMON_PATH
,
69 'su', (bus_name
, flags
)))
71 # XXX: it might be nice to signal IN_QUEUE, EXISTS by exception,
72 # but this would not be backwards-compatible
73 def request_name(self
, name
, flags
=0):
74 """Request a bus name.
78 The well-known name to be requested
80 A bitwise-OR of 0 or more of the flags
81 `DBUS_NAME_FLAG_ALLOW_REPLACEMENT`,
82 `DBUS_NAME_FLAG_REPLACE_EXISTING`
83 and `DBUS_NAME_FLAG_DO_NOT_QUEUE`
84 :Returns: `DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER`,
85 `DBUS_REQUEST_NAME_REPLY_IN_QUEUE`,
86 `DBUS_REQUEST_NAME_REPLY_EXISTS` or
87 `DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER`
88 :Raises DBusException: if the bus daemon cannot be contacted or
91 validate_bus_name(name
, allow_unique
=False)
92 return self
.call_blocking(BUS_DAEMON_NAME
, BUS_DAEMON_PATH
,
93 BUS_DAEMON_IFACE
, 'RequestName',
96 def release_name(self
, name
):
97 """Release a bus name.
101 The well-known name to be released
102 :Returns: `DBUS_RELEASE_NAME_REPLY_RELEASED`,
103 `DBUS_RELEASE_NAME_REPLY_NON_EXISTENT`
104 or `DBUS_RELEASE_NAME_REPLY_NOT_OWNER`
105 :Raises DBusException: if the bus daemon cannot be contacted or
108 validate_bus_name(name
, allow_unique
=False)
109 return self
.call_blocking(BUS_DAEMON_NAME
, BUS_DAEMON_PATH
,
110 BUS_DAEMON_IFACE
, 'ReleaseName',
113 def name_has_owner(self
, bus_name
):
114 """Return True iff the given bus name has an owner on this bus.
118 The bus name to look up
121 return bool(self
.call_blocking(BUS_DAEMON_NAME
, BUS_DAEMON_PATH
,
122 BUS_DAEMON_IFACE
, 'NameHasOwner',
125 def add_match_string(self
, rule
):
126 """Arrange for this application to receive messages on the bus that
127 match the given rule. This version will block.
132 :Raises: `DBusException` on error.
134 self
.call_blocking(BUS_DAEMON_NAME
, BUS_DAEMON_PATH
,
135 BUS_DAEMON_IFACE
, 'AddMatch', 's', (rule
,))
137 # FIXME: add an async success/error handler capability?
138 # (and the same for remove_...)
139 def add_match_string_non_blocking(self
, rule
):
140 """Arrange for this application to receive messages on the bus that
141 match the given rule. This version will not block, but any errors
148 :Raises: `DBusException` on error.
150 self
.call_async(BUS_DAEMON_NAME
, BUS_DAEMON_PATH
,
151 BUS_DAEMON_IFACE
, 'AddMatch', 's', (rule
,),
154 def remove_match_string(self
, rule
):
155 """Arrange for this application to receive messages on the bus that
156 match the given rule. This version will block.
161 :Raises: `DBusException` on error.
163 self
.call_blocking(BUS_DAEMON_NAME
, BUS_DAEMON_PATH
,
164 BUS_DAEMON_IFACE
, 'RemoveMatch', 's', (rule
,))
166 def remove_match_string_non_blocking(self
, rule
):
167 """Arrange for this application to receive messages on the bus that
168 match the given rule. This version will not block, but any errors
175 :Raises: `DBusException` on error.
177 self
.call_async(BUS_DAEMON_NAME
, BUS_DAEMON_PATH
,
178 BUS_DAEMON_IFACE
, 'RemoveMatch', 's', (rule
,),