A faster version of dbus-next
Actively maintained. Last release 8 weeks ago, commits in 8 of the last 26 weeks. A safe choice.
pip install dbus-fastA faster version of dbus-next
Actively maintained. Last release 8 weeks ago, commits in 8 of the last 26 weeks. A safe choice.
pip install dbus-fastA faster version of dbus-next originally from the great DBus next library ❤️
Install this via pip (or your favourite package manager):
pip install dbus-fast
dbus-fast is a Python library for DBus that aims to be a performant fully featured high level library primarily geared towards integration of applications into Linux desktop and mobile environments.
Desktop application developers can use this library for integrating their applications into desktop environments by implementing common DBus standard interfaces or creating custom plugin interfaces.
Desktop users can use this library to create their own scripts and utilities to interact with those interfaces for customization of their desktop environment.
dbus-fast plans to improve over other DBus libraries for Python in the following ways:
This library is available on PyPi as dbus-fast.
pip3 install dbus-fast
The PyPI wheels bundle an optional Cython extension that speeds up the
marshalling/unmarshalling hot paths but adds a few MB to the installed size.
If you're bundling dbus-fast into a size-sensitive application and don't
need the extra performance, install the pure-Python version by forcing a
source build with SKIP_CYTHON=1:
SKIP_CYTHON=1 pip3 install --no-binary dbus-fast dbus-fast
See the installation docs for details.
To use a service on the bus, the library constructs a proxy object you can use to call methods, get and set properties, and listen to signals.
For more information, see the overview for the high-level client.
This example connects to a media player and controls it with the MPRIS DBus interface.
from dbus_fast.aio import MessageBus
import asyncio
async def main():
bus = await MessageBus().connect()
# the introspection xml would normally be included in your project, but
# this is convenient for development
introspection = await bus.introspect(
"org.mpris.MediaPlayer2.vlc", "/org/mpris/MediaPlayer2"
)
obj = bus.get_proxy_object(
"org.mpris.MediaPlayer2.vlc", "/org/mpris/MediaPlayer2", introspection
)
player = obj.get_interface("org.mpris.MediaPlayer2.Player")
properties = obj.get_interface("org.freedesktop.DBus.Properties")
# call methods on the interface (this causes the media player to play)
await player.call_play()
volume = await player.get_volume()
print(f"current volume: {volume}, setting to 0.5")
await player.set_volume(0.5)
# listen to signals
def on_properties_changed(
interface_name, changed_properties, invalidated_properties
):
for changed, variant in changed_properties.items():
print(f"property changed: {changed} - {variant.value}")
properties.on_properties_changed(on_properties_changed)
await asyncio.Event().wait()
asyncio.run(main())
To define a service on the bus, use the ServiceInterface class and decorate class methods to specify DBus methods, properties, and signals with their type signatures.
For more information, see the overview for the high-level service.
from dbus_fast.annotations import DBusStr, DBusDict
from dbus_fast.service import ServiceInterface, dbus_method, dbus_property, dbus_signal
from dbus_fast import Variant
from dbus_fast.aio import MessageBus
import asyncio
class ExampleInterface(ServiceInterface):
def __init__(self, name):
super().__init__(name)
self._string_prop = "kevin"
@dbus_method()
def Echo(self, what: DBusStr) -> DBusStr:
return what
@dbus_method()
def GetVariantDict(self) -> DBusDict:
return {
"foo": Variant("s", "bar"),
"bat": Variant("x", -55),
"a_list": Variant("as", ["hello", "world"]),
}
@dbus_property()
def string_prop(self) -> DBusStr:
return self._string_prop
@string_prop.setter
def string_prop_setter(self, val: DBusStr):
self._string_prop = val
@dbus_signal()
def signal_simple(self) -> DBusStr:
return "hello"
async def main():
bus = await MessageBus().connect()
interface = ExampleInterface("test.interface")
bus.export("/test/path", interface)
# now that we are ready to handle requests, we can request name from D-Bus
await bus.request_name("test.name")
# wait indefinitely
await asyncio.Event().wait()
asyncio.run(main())
The low-level interface works with DBus messages directly.
For more information, see the overview for the low-level interface.
from dbus_fast.message import Message, MessageType
from dbus_fast.aio import MessageBus
import asyncio
import json
async def main():
bus = await MessageBus().connect()
reply = await bus.call(
Message(
destination="org.freedesktop.DBus",
path="/org/freedesktop/DBus",
interface="org.freedesktop.DBus",
member="ListNames",
)
)
if reply.message_type == MessageType.ERROR:
raise Exception(reply.body[0])
print(json.dumps(reply.body[0], indent=2))
asyncio.run(main())
Contributions are welcome. Development happens on Github.
Before you commit, run pre-commit run --all-files to run the linter, code formatter, and the test suite.
You can use this code under an MIT license (see LICENSE).
Thanks goes to these wonderful people (emoji key):
This project follows the all-contributors specification. Contributions of any kind welcome!
This package was created with Cookiecutter and the browniebroke/cookiecutter-pypackage project template.
Use instead of dbus-fast: tools that do the same job, closest first.
Compare with dbus-next →Compare with dbus-python →Compare with sdbus →Compare all 4 →
A zero-dependency DBus library for Python with asyncio support
Python bindings for libdbus
Modern Python D-Bus library. Based on sd-bus from libsystemd.