Modern Python D-Bus library. Based on sd-bus from libsystemd.
Project description
Modern Python library for D-Bus
Features:
- Asyncio and blocking calls.
- Type hints. (
mypy --strict
compatible) - No Python 2 legacy.
- Based on fast sd-bus from systemd.
- Unified client/server interface classes. Write interface once!
- Dbus methods can have keyword and default arguments.
See the documentation for tutorial and API reference.
Also includes a repository of well-known DBus interfaces such as Notifications interface.
Interfaces included
- D-Bus
- Freedesktop Notifications
- systemd (incomplete)
More incoming. (NetworkManager, secrets... )
Requirements
- Python 3.8 or higher. (3.7 might work but is not supported)
- libsystemd. (comes with systemd)
Installation
From AUR: https://aur.archlinux.org/packages/python-sdbus-git/
PyPI: https://pypi.org/project/sdbus/
pip install sdbus
Example code
Interface example_interface.py
file:
from sdbus import (DbusInterfaceCommonAsync, dbus_method_async,
dbus_property_async, dbus_signal_async)
# This is file only contains interface definition for easy import
# in server and client files
class ExampleInterface(
DbusInterfaceCommonAsync,
interface_name='org.example.interface'
):
@dbus_method_async(
input_signature='s',
result_signature='s',
)
async def upper(self, string: str) -> str:
return string.upper()
@dbus_property_async(
property_signature='s',
)
def hello_world(self) -> str:
return 'Hello, World!'
@dbus_signal_async(
signal_signature='i'
)
def clock(self) -> int:
raise NotImplementedError
Server example_server.py
file:
from asyncio import get_event_loop, sleep
from random import randint
from time import time
from example_interface import ExampleInterface
from sdbus import request_default_bus_name_async
loop = get_event_loop()
export_object = ExampleInterface()
async def clock() -> None:
"""
This coroutine will sleep a random time and emit
a signal with current clock
"""
while True:
await sleep(randint(2, 7)) # Sleep a random time
current_time = int(time()) # The interface we defined uses integers
export_object.clock.emit(current_time)
async def startup() -> None:
"""Perform async startup actions"""
# Acquire a known name on the bus
# Client will use that name to connect to this server
await request_default_bus_name_async('org.example.test')
# Export the object to dbus
export_object.export_to_dbus('/')
loop.run_until_complete(startup())
loop.create_task(clock())
loop.run_forever()
Client example_client.py
file:
from asyncio import get_event_loop
from example_interface import ExampleInterface
# Create a new binded object
example_object = ExampleInterface.new_connect('org.example.test', '/')
async def print_clock() -> None:
# Use async for loop to print clock signals we receive
async for x in example_object.clock:
print('Got clock: ', x)
async def call_upper() -> None:
s = 'test string'
s_after = await example_object.upper(s)
print('Initial string: ', s)
print('After call: ', s_after)
async def get_hello_world() -> None:
print('Remote property: ', await example_object.hello_world)
loop = get_event_loop()
loop.create_task(call_upper())
loop.create_task(print_clock())
loop.create_task(get_hello_world())
loop.run_forever()
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
sdbus-0.8rc2.tar.gz
(51.4 kB
view hashes)
Built Distributions
Close
Hashes for sdbus-0.8rc2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 561573e0863c7e81f389067bdda0c3d859a33acfcb90244c09bc979551eb58ba |
|
MD5 | 1916ed0cfb09646131dad439cc57ed30 |
|
BLAKE2b-256 | b140d61ca10a5f735931a4259b0a0718b9d94b1b3cb274f3f6883f57dd58d797 |
Close
Hashes for sdbus-0.8rc2-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ecc1b83d5b2e7cac7557ac0ee1c53580a8ecb8c08789f1219f73026aa12c9337 |
|
MD5 | ba934e7fd421d54337f23f6b69ee1b52 |
|
BLAKE2b-256 | 800319c5a7a887633d63b12c25f6d02c5280ad9060c63d3563ec81027e9d4545 |
Close
Hashes for sdbus-0.8rc2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 648d5db506fec6c560f2b194cbf64e18284666ca906806430d56b5e65927c01d |
|
MD5 | 9be22d45ad7892b9b54a3716199e5cd7 |
|
BLAKE2b-256 | ed37dc541807c4cefffaccd1b212f9496b833bb8098cc85a56c617f5ff6bc999 |
Close
Hashes for sdbus-0.8rc2-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 36636cac7a38ed07f52b70da3f314dfd7b647b0ab8057ef23619d7945e2c0491 |
|
MD5 | 202933c86c86f41a2f4dcf62b8d3d1f3 |
|
BLAKE2b-256 | b1ab01e99a8f7b828fa02ede18ce328e6e31e2c71fa3e1cfb0c00a12a7a04f49 |
Close
Hashes for sdbus-0.8rc2-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ce8dfe0b9332640ec769c08d4671cbef0e522bd6a57606d0b9b3550d21894334 |
|
MD5 | b42a2ee8f19cf11ee3fe1fd34ab5e6b7 |
|
BLAKE2b-256 | 5dd55d2a92d2e11e1294456b5e7dfe37ab4e04db7b2f3284e5d466bf02923340 |
Close
Hashes for sdbus-0.8rc2-cp37-cp37m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ec29d4b27a3d09887499494382308c08956fe54386aa7cd5af9d256dee289548 |
|
MD5 | 6c3ac1cdec445f9232b965c275a18d50 |
|
BLAKE2b-256 | cb3ea0e3d7fc4edbbb74ad099fdb45dee6ea06edd1bfe7451073b849a684b016 |