D-Bus#
There are several classes that can help you interact with D-Bus.
- class ignis.dbus.DBusService(*args: Any, **kwargs: Any)#
Class to help create DBus services.
- Properties:
name (
str
, required, read-only): The well-known name to own.object_path (
str
, required, read-only): An object path.info (Gio.DBusInterfaceInfo, required, read-only): A
Gio.DBusInterfaceInfo
instance. You can get it from XML usingload_interface_xml
.on_name_acquired (
Callable
, optional, read-write): Function to call whenname
is acquired.on_name_lost (
Callable
, optional, read-write): Function to call whenname
is lost.connection (Gio.DBusConnection, not argument, read-only): The
Gio.DBusConnection
instance.methods (
Dict[str, Callable]
, not argument, read-only): The dictionary of registred DBus methods. Seeregister_dbus_method()
.properties (
Dict[str, Callable]
, not argument, read-only): The dictionary of registred DBus properties. Seeregister_dbus_property()
.
- 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.
- DBus properties:
Must return GLib.Variant, as specified by interface info.
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)
- 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).
- 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).
- emit_signal(signal_name: str, parameters: 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 -- The
GLib.Variant
containing paramaters to pass with the signal.
- unown_name() None #
Release ownership of the name.
- class ignis.dbus.DBusProxy(*args: Any, **kwargs: Any)#
Class to interact with D-Bus services (create a D-Bus proxy). Unlike Gio.DBusProxy, this class also provides convenient pythonic property access.
- Properties:
name (
str
, required, read-only): A bus name (well-known or unique).object_path (
str
, required, read-only): An object path.interface_name (
str
, required, read-only): A D-Bus interface name.info (Gio.DBusInterfaceInfo, required, read-only): A
Gio.DBusInterfaceInfo
instance. You can get it from XML usingload_interface_xml
.proxy (Gio.DBusProxy, not argument, read-only): The
Gio.DBusProxy
instance.methods (
list[str]
, not argument, read-only): A list of methods exposed by D-Bus service.properties (
list[str]
, not argument, read-only): A list of properties exposed by D-Bus service.has_owner (
bool
, not argument, read-only): Whether thename
has an owner.
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. Subsequent arguments must match the provided D-Bus signature. If the D-Bus method does not accept any arguments, do not pass arguments.
from ignis.dbus import DBusProxy proxy = DBusProxy(...) proxy.MyMethod("(is)", 42, "hello")
To get a D-Bus property, also use the standart pythonic way.
from ignis.dbus import DBusProxy proxy = DBusProxy(...) value = proxy.MyValue print(value)
- 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
, optional) -- A function to call when signal is emitted.
- Returns:
a subscription ID that can be used with
signal_unsubscribe()
- Return type:
int
- signal_unsubscribe(id: int) None #
Unsubscribe from D-Bus signal.
- Parameters:
id (
int
) -- The ID of the subscription.
- watch_name(on_name_appeared: Callable | None = None, on_name_vanished: Callable | None = None) None #
Watch
name
.- Parameters:
on_name_appeared (
Callable
, optional) -- A function to call whenname
appeared.on_name_vanished (
Callable
, optional) -- A function to call whenname
vanished.
- unwatch_name() None #
Unwatch name.