| 1 |
Examples |
| 2 |
======== |
| 3 |
|
| 4 |
Look at the `complete examples <https://github.com/rhinstaller/dasbus/tree/master/examples>`_ or |
| 5 |
`DBus services <https://github.com/rhinstaller/anaconda/tree/master/pyanaconda/modules>`_ of |
| 6 |
the Anaconda Installer for more inspiration. |
| 7 |
|
| 8 |
Basic usage |
| 9 |
----------- |
| 10 |
|
| 11 |
Show the current hostname. |
| 12 |
|
| 13 |
.. code-block:: python |
| 14 |
|
| 15 |
from dasbus.connection import SystemMessageBus |
| 16 |
bus = SystemMessageBus() |
| 17 |
|
| 18 |
proxy = bus.get_proxy( |
| 19 |
"org.freedesktop.hostname1", |
| 20 |
"/org/freedesktop/hostname1" |
| 21 |
) |
| 22 |
|
| 23 |
print(proxy.Hostname) |
| 24 |
|
| 25 |
Send a notification to the notification server. |
| 26 |
|
| 27 |
.. code-block:: python |
| 28 |
|
| 29 |
from dasbus.connection import SessionMessageBus |
| 30 |
bus = SessionMessageBus() |
| 31 |
|
| 32 |
proxy = bus.get_proxy( |
| 33 |
"org.freedesktop.Notifications", |
| 34 |
"/org/freedesktop/Notifications" |
| 35 |
) |
| 36 |
|
| 37 |
id = proxy.Notify( |
| 38 |
"", 0, "face-smile", "Hello World!", |
| 39 |
"This notification can be ignored.", |
| 40 |
[], {}, 0 |
| 41 |
) |
| 42 |
|
| 43 |
print("The notification {} was sent.".format(id)) |
| 44 |
|
| 45 |
Handle a closed notification. |
| 46 |
|
| 47 |
.. code-block:: python |
| 48 |
|
| 49 |
from dasbus.loop import EventLoop |
| 50 |
loop = EventLoop() |
| 51 |
|
| 52 |
from dasbus.connection import SessionMessageBus |
| 53 |
bus = SessionMessageBus() |
| 54 |
|
| 55 |
proxy = bus.get_proxy( |
| 56 |
"org.freedesktop.Notifications", |
| 57 |
"/org/freedesktop/Notifications" |
| 58 |
) |
| 59 |
|
| 60 |
def callback(id, reason): |
| 61 |
print("The notification {} was closed.".format(id)) |
| 62 |
|
| 63 |
proxy.NotificationClosed.connect(callback) |
| 64 |
loop.run() |
| 65 |
|
| 66 |
Asynchronously fetch a list of network devices. |
| 67 |
|
| 68 |
.. code-block:: python |
| 69 |
|
| 70 |
from dasbus.loop import EventLoop |
| 71 |
loop = EventLoop() |
| 72 |
|
| 73 |
from dasbus.connection import SystemMessageBus |
| 74 |
bus = SystemMessageBus() |
| 75 |
|
| 76 |
proxy = bus.get_proxy( |
| 77 |
"org.freedesktop.NetworkManager", |
| 78 |
"/org/freedesktop/NetworkManager" |
| 79 |
) |
| 80 |
|
| 81 |
def callback(call): |
| 82 |
print(call()) |
| 83 |
|
| 84 |
proxy.GetDevices(callback=callback) |
| 85 |
loop.run() |
| 86 |
|
| 87 |
Define the org.example.HelloWorld service. |
| 88 |
|
| 89 |
.. code-block:: python |
| 90 |
|
| 91 |
class HelloWorld(object): |
| 92 |
__dbus_xml__ = """ |
| 93 |
<node> |
| 94 |
<interface name="org.example.HelloWorld"> |
| 95 |
<method name="Hello"> |
| 96 |
<arg direction="in" name="name" type="s" /> |
| 97 |
<arg direction="out" name="return" type="s" /> |
| 98 |
</method> |
| 99 |
</interface> |
| 100 |
</node> |
| 101 |
""" |
| 102 |
|
| 103 |
def Hello(self, name): |
| 104 |
return "Hello {}!".format(name) |
| 105 |
|
| 106 |
Define the org.example.HelloWorld service with an automatically generated XML specification. |
| 107 |
|
| 108 |
.. code-block:: python |
| 109 |
|
| 110 |
from dasbus.server.interface import dbus_interface |
| 111 |
from dasbus.typing import Str |
| 112 |
|
| 113 |
@dbus_interface("org.example.HelloWorld") |
| 114 |
class HelloWorld(object): |
| 115 |
|
| 116 |
def Hello(self, name: Str) -> Str: |
| 117 |
return "Hello {}!".format(name) |
| 118 |
|
| 119 |
print(HelloWorld.__dbus_xml__) |
| 120 |
|
| 121 |
Publish the org.example.HelloWorld service on the session message bus. |
| 122 |
|
| 123 |
.. code-block:: python |
| 124 |
|
| 125 |
from dasbus.connection import SessionMessageBus |
| 126 |
bus = SessionMessageBus() |
| 127 |
bus.publish_object("/org/example/HelloWorld", HelloWorld()) |
| 128 |
bus.register_service("org.example.HelloWorld") |
| 129 |
|
| 130 |
from dasbus.loop import EventLoop |
| 131 |
loop = EventLoop() |
| 132 |
loop.run() |
| 133 |
|
| 134 |
Support for Unix file descriptors |
| 135 |
--------------------------------- |
| 136 |
|
| 137 |
The support for Unix file descriptors is disabled by default. It needs to be explicitly enabled |
| 138 |
when you create a DBus proxy or publish a DBus object that could send or receive Unix file |
| 139 |
descriptors. |
| 140 |
|
| 141 |
.. warning:: |
| 142 |
|
| 143 |
This functionality is supported only on UNIX. |
| 144 |
|
| 145 |
Send and receive Unix file descriptors with a DBus proxy. |
| 146 |
|
| 147 |
.. code-block:: python |
| 148 |
|
| 149 |
import os |
| 150 |
from dasbus.connection import SystemMessageBus |
| 151 |
from dasbus.unix import GLibClientUnix |
| 152 |
bus = SystemMessageBus() |
| 153 |
|
| 154 |
proxy = bus.get_proxy( |
| 155 |
"org.freedesktop.login1", |
| 156 |
"/org/freedesktop/login1", |
| 157 |
client=GLibClientUnix |
| 158 |
) |
| 159 |
|
| 160 |
fd = proxy.Inhibit( |
| 161 |
"sleep", "my-example", "Running an example", "block" |
| 162 |
) |
| 163 |
|
| 164 |
proxy.ListInhibitors() |
| 165 |
os.close(fd) |
| 166 |
|
| 167 |
Allow to send and receive Unix file descriptors within the /org/example/HelloWorld DBus object. |
| 168 |
|
| 169 |
.. code-block:: python |
| 170 |
|
| 171 |
from dasbus.unix import GLibServerUnix |
| 172 |
bus.publish_object( |
| 173 |
"/org/example/HelloWorld", |
| 174 |
HelloWorld(), |
| 175 |
server=GLibServerUnix |
| 176 |
) |
| 177 |
|
| 178 |
Management of DBus names and paths |
| 179 |
---------------------------------- |
| 180 |
|
| 181 |
Use constants to define DBus services and objects. |
| 182 |
|
| 183 |
.. code-block:: python |
| 184 |
|
| 185 |
from dasbus.connection import SystemMessageBus |
| 186 |
from dasbus.identifier import DBusServiceIdentifier, DBusObjectIdentifier |
| 187 |
|
| 188 |
NETWORK_MANAGER_NAMESPACE = ( |
| 189 |
"org", "freedesktop", "NetworkManager" |
| 190 |
) |
| 191 |
|
| 192 |
NETWORK_MANAGER = DBusServiceIdentifier( |
| 193 |
namespace=NETWORK_MANAGER_NAMESPACE, |
| 194 |
message_bus=SystemMessageBus() |
| 195 |
) |
| 196 |
|
| 197 |
NETWORK_MANAGER_SETTINGS = DBusObjectIdentifier( |
| 198 |
namespace=NETWORK_MANAGER_NAMESPACE, |
| 199 |
basename="Settings" |
| 200 |
) |
| 201 |
|
| 202 |
Create a proxy of the org.freedesktop.NetworkManager service. |
| 203 |
|
| 204 |
.. code-block:: python |
| 205 |
|
| 206 |
proxy = NETWORK_MANAGER.get_proxy() |
| 207 |
print(proxy.NetworkingEnabled) |
| 208 |
|
| 209 |
Create a proxy of the /org/freedesktop/NetworkManager/Settings object. |
| 210 |
|
| 211 |
.. code-block:: python |
| 212 |
|
| 213 |
proxy = NETWORK_MANAGER.get_proxy(NETWORK_MANAGER_SETTINGS) |
| 214 |
print(proxy.Hostname) |
| 215 |
|
| 216 |
See `a complete example <https://github.com/rhinstaller/dasbus/tree/master/examples/05_chat>`__. |
| 217 |
|
| 218 |
Error handling |
| 219 |
-------------- |
| 220 |
|
| 221 |
Use exceptions to propagate and handle DBus errors. Create an error mapper and a decorator for |
| 222 |
mapping Python exception classes to DBus error names. |
| 223 |
|
| 224 |
.. code-block:: python |
| 225 |
|
| 226 |
from dasbus.error import ErrorMapper, DBusError, get_error_decorator |
| 227 |
error_mapper = ErrorMapper() |
| 228 |
dbus_error = get_error_decorator(error_mapper) |
| 229 |
|
| 230 |
Use the decorator to register Python exceptions that represent DBus errors. These exceptions |
| 231 |
can be raised by DBus services and caught by DBus clients in the try-except block. |
| 232 |
|
| 233 |
.. code-block:: python |
| 234 |
|
| 235 |
@dbus_error("org.freedesktop.DBus.Error.InvalidArgs") |
| 236 |
class InvalidArgs(DBusError): |
| 237 |
pass |
| 238 |
|
| 239 |
The message bus will use the specified error mapper to automatically transform Python exceptions |
| 240 |
to DBus errors and back. |
| 241 |
|
| 242 |
.. code-block:: python |
| 243 |
|
| 244 |
from dasbus.connection import SessionMessageBus |
| 245 |
bus = SessionMessageBus(error_mapper=error_mapper) |
| 246 |
|
| 247 |
See `a complete example <https://github.com/rhinstaller/dasbus/tree/master/examples/04_register>`__. |
| 248 |
|
| 249 |
Timeout for a DBus call |
| 250 |
----------------------- |
| 251 |
|
| 252 |
Call DBus methods with a timeout (specified in milliseconds). |
| 253 |
|
| 254 |
.. code-block:: python |
| 255 |
|
| 256 |
proxy = NETWORK_MANAGER.get_proxy() |
| 257 |
|
| 258 |
try: |
| 259 |
proxy.CheckConnectivity(timeout=3) |
| 260 |
except TimeoutError: |
| 261 |
print("The call timed out!") |
| 262 |
|
| 263 |
|
| 264 |
Support for DBus structures |
| 265 |
--------------------------- |
| 266 |
|
| 267 |
Represent DBus structures by Python objects. A DBus structure is a dictionary of attributes that |
| 268 |
maps attribute names to variants with attribute values. Use Python objects to define such |
| 269 |
structures. They can be easily converted to a dictionary, send via DBus and converted back to |
| 270 |
an object. |
| 271 |
|
| 272 |
.. code-block:: python |
| 273 |
|
| 274 |
from dasbus.structure import DBusData |
| 275 |
from dasbus.typing import Str, get_variant |
| 276 |
|
| 277 |
class UserData(DBusData): |
| 278 |
def __init__(self): |
| 279 |
self._name = "" |
| 280 |
|
| 281 |
@property |
| 282 |
def name(self) -> Str: |
| 283 |
return self._name |
| 284 |
|
| 285 |
@name.setter |
| 286 |
def name(self, name): |
| 287 |
self._name = name |
| 288 |
|
| 289 |
data = UserData() |
| 290 |
data.name = "Alice" |
| 291 |
|
| 292 |
print(UserData.to_structure(data)) |
| 293 |
print(UserData.from_structure({ |
| 294 |
"name": get_variant(Str, "Bob") |
| 295 |
})) |
| 296 |
|
| 297 |
See `a complete example <https://github.com/rhinstaller/dasbus/tree/master/examples/04_register>`__. |
| 298 |
|
| 299 |
Management of dynamic DBus objects |
| 300 |
---------------------------------- |
| 301 |
|
| 302 |
Create Python objects that can be automatically published on DBus. These objects are usually |
| 303 |
managed by DBus containers and published on demand. |
| 304 |
|
| 305 |
.. code-block:: python |
| 306 |
|
| 307 |
from dasbus.server.interface import dbus_interface |
| 308 |
from dasbus.server.template import InterfaceTemplate |
| 309 |
from dasbus.server.publishable import Publishable |
| 310 |
from dasbus.typing import Str |
| 311 |
|
| 312 |
@dbus_interface("org.example.Chat") |
| 313 |
class ChatInterface(InterfaceTemplate): |
| 314 |
|
| 315 |
def Send(self, message: Str): |
| 316 |
return self.implementation.send() |
| 317 |
|
| 318 |
class Chat(Publishable): |
| 319 |
|
| 320 |
def for_publication(self): |
| 321 |
return ChatInterface(self) |
| 322 |
|
| 323 |
def send(self, message): |
| 324 |
print(message) |
| 325 |
|
| 326 |
Use DBus containers to automatically publish dynamically created Python objects. A DBus container |
| 327 |
converts publishable Python objects into DBus paths and back. It generates unique DBus paths in |
| 328 |
the specified namespace and assigns them to objects. Each object is published when its DBus path |
| 329 |
is requested for the first time. |
| 330 |
|
| 331 |
.. code-block:: python |
| 332 |
|
| 333 |
from dasbus.connection import SessionMessageBus |
| 334 |
from dasbus.server.container import DBusContainer |
| 335 |
|
| 336 |
container = DBusContainer( |
| 337 |
namespace=("org", "example", "Chat"), |
| 338 |
message_bus=SessionMessageBus() |
| 339 |
) |
| 340 |
|
| 341 |
print(container.to_object_path(Chat())) |
| 342 |
|
| 343 |
See `a complete example <https://github.com/rhinstaller/dasbus/tree/master/examples/05_chat>`__. |