D-Bus#

There are several classes that can help you interact with D-Bus.

class ignis.dbus.DBusService(name: str, object_path: str, info: gi.repository.Gio.DBusInterfaceInfo, on_name_acquired: Callable | None = None, on_name_lost: Callable | None = None)#

A class that helps create a D-Bus service.

from gi.repository import Gio, GLib
from ignis.dbus import DBusService

def _MyMethod(invocation: Gio.DBusMethodInvocation, prop1: str, prop2: str, *args) -> GLib.Variant:
    print("do something")
    return GLib.Variant("(is)", (42, "hello"))

def _MyProperty() -> GLib.Variant:
    return GLib.Variant("(b)", (False,))

dbus = DBusService(...)
dbus.register_dbus_method("MyMethod", _MyMethod)
dbus.register_dbus_property("MyProperty", _MyProperty)
property name: str#
  • required, read-only

The well-known name to own.

property object_path: str#
  • required, read-only

An object path.

property info: gi.repository.Gio.DBusInterfaceInfo#
  • required, read-only

An instance of Gio.DBusInterfaceInfo

You can get it from XML using load_interface_xml().

property on_name_acquired: Callable#
  • optional, read-write

The function to call when name is acquired.

property on_name_lost: Callable#
  • optional, read-write

The function to call when name is lost.

property connection: gi.repository.Gio.DBusConnection#
  • not argument, read-only

The instance of Gio.DBusConnection for this service.

property methods: dict[str, Callable]#
  • not argument, read-only

The dictionary of registred DBus methods. See register_dbus_method().

property properties: dict[str, Callable]#
  • not argument, read-only

The dictionary of registred DBus properties. See register_dbus_property().

register_dbus_method(name: str, method: Callable) None#

Register a D-Bus method for this service.

Parameters:
  • name (str) -- The name of the method to register.

  • method (Callable) -- A function to call when the method is invoked (from D-Bus).

Return type:

None

DBus methods:
  • Must accept Gio.DBusMethodInvocation as the first argument.

  • Must accept all other arguments typical for this method (specified by interface info).

  • Must return GLib.Variant or None, as specified by interface info.

register_dbus_property(name: str, method: Callable) None#

Register D-Bus property for this service.

Parameters:
  • name (str) -- The name of the property to register.

  • method (Callable) -- A function to call when the property is accessed (from DBus).

Return type:

None

DBus properties:
emit_signal(signal_name: str, parameters: gi.repository.GLib.Variant | None = None) None#

Emit a D-Bus signal on this service.

Parameters:
  • signal_name (str) -- The name of the signal to emit.

  • parameters (Variant | None, default: None) -- The GLib.Variant containing paramaters to pass with the signal.

Return type:

None

unown_name() None#

Release ownership of the name.

Return type:

None

class ignis.dbus.DBusProxy(name: str, object_path: str, interface_name: str, info: gi.repository.Gio.DBusInterfaceInfo, bus_type: Literal['session', 'system'] = 'session')#

A class to interact with D-Bus services (create a D-Bus proxy). Unlike Gio.DBusProxy>, this class also provides convenient pythonic property access.

To call a D-Bus method, use the standart pythonic way. The first argument always needs to be the DBus signature tuple of the method call. Next arguments must match the provided D-Bus signature. If the D-Bus method does not accept any arguments, do not pass them.

from ignis.dbus import DBusProxy
proxy = DBusProxy(...)
result = proxy.MyMethod("(is)", 42, "hello")
print(result)

To get a D-Bus property:

from ignis.dbus import DBusProxy
proxy = DBusProxy(...)
print(proxy.MyValue)

To set a D-Bus property:

from ignis.dbus import DBusProxy
proxy = DBusProxy(...)
# pass GLib.Variant as new property value
proxy.MyValue = GLib.Variant("s", "Hello world!")
property name: str#
  • required, read-only

A bus name (well-known or unique).

property object_path: str#
  • required, read-only

An object path.

property interface_name: str#
  • required, read-only

A D-Bus interface name.

property info: gi.repository.Gio.DBusInterfaceInfo#
  • required, read-only

A Gio.DBusInterfaceInfo instance.

You can get it from XML using load_interface_xml.

property bus_type: Literal['session', 'system']#
  • optional, read-only

The type of the bus.

Default: session.

property proxy: gi.repository.Gio.DBusProxy#
  • not argument, read-only

The Gio.DBusProxy instance.

property connection: gi.repository.Gio.DBusConnection#
  • not argument, read-only

The instance of Gio.DBusConnection for this proxy.

property methods: list[str]#
  • not argument, read-only

A list of methods exposed by D-Bus service.

property properties: list[str]#
  • not argument, read-only

A list of properties exposed by D-Bus service.

property has_owner: bool#
  • not argument, read-only

Whether the name has an owner.

signal_subscribe(signal_name: str, callback: Callable | None = None) int#

Subscribe to D-Bus signal.

Parameters:
  • signal_name (str) -- The signal name to subscribe.

  • callback (Callable | None, default: None) -- A function to call when signal is emitted.

Return type:

int

Returns:

A subscription ID that can be used with signal_unsubscribe()

signal_unsubscribe(id: int) None#

Unsubscribe from D-Bus signal.

Parameters:

id (int) -- The ID of the subscription.

Return type:

None

watch_name(on_name_appeared: Callable | None = None, on_name_vanished: Callable | None = None) None#

Watch name.

Parameters:
  • on_name_appeared (Callable | None, default: None) -- A function to call when name appeared.

  • on_name_vanished (Callable | None, default: None) -- A function to call when name vanished.

Return type:

None

unwatch_name() None#

Unwatch name.

Return type:

None