Skip to main content

Modern Python D-Bus library. Based on sd-bus from libsystemd.

Project description

Documentation Status PyPI - Version

Modern Python library for D-Bus

Packaging status

Features:

  • Asyncio and blocking calls.
  • Type hints. (mypy --strict compatible)
  • No Python 2 legacy.
  • Based on fast sd-bus from systemd. (also supports elogind)
  • Unified client/server interface classes. Write interface once!
  • D-Bus methods can have keyword and default arguments.

See the documentation for tutorial and API reference.

List of implemented interfaces

More incoming. (systemd, Bluez, screen saver... )

Community interfaces

Stability

Python-sdbus is under development and its API is not stable. Generally anything documented in the official documentation is considered stable but might be deprecated. Using deprecated feature will raise a warning and the feature will be eventually removed.

See the deprecations list.

If there is a feature that is not documented but you would like to use please open a new issue.

Requirements

Binary package from PyPI

  • Python 3.9 or higher.
  • x86_64, aarch64 or armv7l architecture.
  • glibc 2.28 or higher. (Debian 10+, Ubuntu 18.10+, CentOS/RHEL 8+)
  • pip 20.3 or higher.

libsystemd is statically linked and is not required to be installed on the system.

Pass --only-binary ':all:' to pip to ensure that it installs binary package.

i686, ppc64le and s390x can be supported if there is a demand. Please open an issue if you are interested in those platforms.

Source package or compiling from source

  • Python 3.9 or higher.
  • Python headers. (python3-dev package on ubuntu)
  • GCC.
  • libsystemd or libelogind
  • libsystemd headers. (libsystemd-dev package on ubuntu)
  • Python setuptools.
  • pkg-config

Systemd version should be higher than 246.

Optional dependencies

  • Jinja2 for code generator.
  • Sphinx for autodoc.

Installation

PyPI

URL: https://pypi.org/project/sdbus/

pip install --only-binary ':all:' sdbus

AUR

URL: https://aur.archlinux.org/packages/python-sdbus-git/

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 new_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 = new_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
    # Clients will use that name to address this server
    await request_default_bus_name_async('org.example.test')
    # Export the object to D-Bus
    export_object.export_to_dbus('/')


loop.run_until_complete(startup())
task_clock = loop.create_task(clock())
loop.run_forever()

Client example_client.py file:

from asyncio import new_event_loop

from example_interface import ExampleInterface

# Create a new proxied object
example_object = ExampleInterface.new_proxy('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 = new_event_loop()

# Always bind your tasks to a variable
task_upper = loop.create_task(call_upper())
task_clock = loop.create_task(print_clock())
task_hello_world = loop.create_task(get_hello_world())

loop.run_forever()

License

Python-sdbus is licensed under LGPL-2.1-or-later.

The LGPL license is an extension of GPL license therefore both licenses' texts are required.

Project details


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.14.1.post0.tar.gz (89.9 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

sdbus-0.14.1.post0-cp39-abi3-manylinux_2_28_x86_64.whl (741.9 kB view details)

Uploaded CPython 3.9+manylinux: glibc 2.28+ x86-64

sdbus-0.14.1.post0-cp39-abi3-manylinux_2_28_armv7l.whl (697.8 kB view details)

Uploaded CPython 3.9+manylinux: glibc 2.28+ ARMv7l

sdbus-0.14.1.post0-cp39-abi3-manylinux_2_28_aarch64.whl (770.5 kB view details)

Uploaded CPython 3.9+manylinux: glibc 2.28+ ARM64

File details

Details for the file sdbus-0.14.1.post0.tar.gz.

File metadata

  • Download URL: sdbus-0.14.1.post0.tar.gz
  • Upload date:
  • Size: 89.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for sdbus-0.14.1.post0.tar.gz
Algorithm Hash digest
SHA256 ae3915ab3e3f0a116632e1e5876df992b9529e82b0b492006eb22f8791ed6deb
MD5 48ba3d23852b3d3f4f0623194f637872
BLAKE2b-256 b94f549089131ba4ba5379517655cdcbfe3925fd1cc0eafd93aac5f73c2a6d40

See more details on using hashes here.

File details

Details for the file sdbus-0.14.1.post0-cp39-abi3-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for sdbus-0.14.1.post0-cp39-abi3-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 a76bfac9728217a7492bdac6379e2bd7b7134bf60ff53def05c6ff9b28141539
MD5 170b8db75902697070098fc790bdd195
BLAKE2b-256 5e8dc6015b49d18073dc66eb78f8b6c9e95225b779844886e1c604faed774eb4

See more details on using hashes here.

File details

Details for the file sdbus-0.14.1.post0-cp39-abi3-manylinux_2_28_armv7l.whl.

File metadata

File hashes

Hashes for sdbus-0.14.1.post0-cp39-abi3-manylinux_2_28_armv7l.whl
Algorithm Hash digest
SHA256 cce4b73dbeb3e7aea2f1fa557d51d1cb39d32769afa75275966c9d09ead35780
MD5 b8fe688403d314cefc7f9103970e3e20
BLAKE2b-256 549c013eb3d09ad372a109eacc6353fce25ec0b0350463e8a6d6fb790e2d6833

See more details on using hashes here.

File details

Details for the file sdbus-0.14.1.post0-cp39-abi3-manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for sdbus-0.14.1.post0-cp39-abi3-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 2ec48808f13e118ab23a0919c4230f7c3bc3a167e4215b5c26ba37a8b6d93964
MD5 f1a54c313bf9a04d0679f8641ae27e7a
BLAKE2b-256 3d2f190a52909468fe3d67e22193c69d70d60d4a8377f9479725b2d34a37f49c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page