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