Skip to main content

DBus library in Python 3

Project description

dasbus

This DBus library is written in Python 3, based on GLib and inspired by pydbus.

The code used to be part of the Anaconda Installer project. It was based on the pydbus library, but we replaced it with our own solution because of its inactivity. The dasbus library is a result of this effort.

Requirements

  • Python 3.6+
  • PyGObject 3

You can install PyGObject provided by your system or use PyPI. The system package is usually called python3-gi, python3-gobject or pygobject3. See the instructions for your platform (only for PyGObject, you don't need cairo or GTK).

The library is known to work with Python 3.8, PyGObject 3.34 and GLib 2.63, but these are not the required minimal versions.

Installation

Install the package from PyPI. Follow the instructions above to install the required dependencies.

pip3 install dasbus

Or install the RPM package on Fedora.

sudo dnf install python3-dasbus

Examples

Show the current hostname.

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.

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 send.".format(id))

Handle a closed notification.

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()

Run the service org.example.HelloWorld.

from dasbus.loop import EventLoop
loop = EventLoop()

from dasbus.connection import SessionMessageBus
bus = SessionMessageBus()

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)

bus.publish_object("/org/example/HelloWorld", HelloWorld())
bus.register_service("org.example.HelloWorld")
loop.run()

Features

Use constants to define DBus services and objects.

from dasbus.connection import SystemMessageBus
from dasbus.identifier import DBusServiceIdentifier

NETWORK_MANAGER = DBusServiceIdentifier(
    namespace=("org", "freedesktop", "NetworkManager"),
    message_bus=SystemMessageBus()
)

proxy = NETWORK_MANAGER.get_proxy()
print(proxy.NetworkingEnabled)

Use exceptions to propagate and handle DBus errors.

from dasbus.error import ErrorMapper
error_mapper = ErrorMapper()

from dasbus.connection import SessionMessageBus
bus = SessionMessageBus(error_mapper=error_mapper)

from dasbus.error import DBusError, get_error_decorator
dbus_error = get_error_decorator(error_mapper)

@dbus_error("org.freedesktop.DBus.Error.InvalidArgs")
class InvalidArgs(DBusError):
    pass

Call DBus methods asynchronously.

from dasbus.loop import EventLoop
loop = EventLoop()

def callback(call):
    print(call())

proxy = NETWORK_MANAGER.get_proxy()
proxy.GetDevices(callback=callback)
loop.run()

Generate XML specifications from Python classes.

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__)

Represent DBus structures by Python objects.

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")
}))

Create Python objects that can be published on DBus.

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 publish dynamically created Python objects.

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()))

Inspiration

Look at the complete examples or DBus services of the Anaconda Installer for more inspiration.

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

dasbus-0.4.tar.gz (41.8 kB view details)

Uploaded Source

Built Distribution

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

dasbus-0.4-py3-none-any.whl (57.8 kB view details)

Uploaded Python 3

File details

Details for the file dasbus-0.4.tar.gz.

File metadata

  • Download URL: dasbus-0.4.tar.gz
  • Upload date:
  • Size: 41.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.2 pkginfo/1.4.2 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.37.0 CPython/3.7.6

File hashes

Hashes for dasbus-0.4.tar.gz
Algorithm Hash digest
SHA256 e6768b3b6b282d86fbd2faa28a9f1d3c91ae58ef469975294fee07dddde4aa4c
MD5 1822d0483b6a32459db79101d3603e29
BLAKE2b-256 774e16566315257f43516cbe48e800d942ecce380a562b846f26eea74ffe77f4

See more details on using hashes here.

File details

Details for the file dasbus-0.4-py3-none-any.whl.

File metadata

  • Download URL: dasbus-0.4-py3-none-any.whl
  • Upload date:
  • Size: 57.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.2 pkginfo/1.4.2 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.37.0 CPython/3.7.6

File hashes

Hashes for dasbus-0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 bf6eac8e8545eddda15eb6c16ef52f6d789902b1137131606a836f65db6eba5f
MD5 4cee6486d72167570dd9913039a73470
BLAKE2b-256 d149cfd89573dffbf9a5b3943d40c9100f45f85e8e22bbb8389e02dc5f655f96

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