DBusProxy
Added in version 2.26.
Superclasses: Object
Implemented Interfaces: AsyncInitable
, DBusInterface
, Initable
GDBusProxy
is a base class used for proxies to access a D-Bus
interface on a remote object. A GDBusProxy
can be constructed for
both well-known and unique names.
By default, GDBusProxy
will cache all properties (and listen to
changes) of the remote object, and proxy all signals that get
emitted. This behaviour can be changed by passing suitable
DBusProxyFlags
when the proxy is created. If the proxy is for a
well-known name, the property cache is flushed when the name owner
vanishes and reloaded when a name owner appears.
The unique name owner of the proxy’s name is tracked and can be read from
g_name_owner
. Connect to the
notify
signal to get notified of changes.
Additionally, only signals and property changes emitted from the current name
owner are considered and calls are always sent to the current name owner.
This avoids a number of race conditions when the name is lost by one owner
and claimed by another. However, if no name owner currently exists,
then calls will be sent to the well-known name which may result in
the message bus launching an owner (unless
G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START
is set).
If the proxy is for a stateless D-Bus service, where the name owner may
be started and stopped between calls, the
g_name_owner
tracking of GDBusProxy
will cause the
proxy to drop signal and property changes from the service after it has
restarted for the first time. When interacting with a stateless D-Bus
service, do not use GDBusProxy
— use direct D-Bus method calls and signal
connections.
The generic g_properties_changed
and
g_signal
signals are not very convenient to work
with. Therefore, the recommended way of working with proxies is to subclass
GDBusProxy
, and have more natural properties and signals in your derived
class. This example shows how
this can easily be done using the `gdbus-codegen
<gdbus-codegen.html>`_ tool.
A GDBusProxy
instance can be used from multiple threads but note
that all signals (e.g. g_signal
,
g_properties_changed
and
notify
) are emitted in the thread-default main
context (see push_thread_default
) of the thread
where the instance was constructed.
An example using a proxy for a well-known name can be found in
`gdbus-example-watch-proxy.c
<https://gitlab.gnome.org/GNOME/glib/-/blob/HEAD/gio/tests/gdbus-example-watch-proxy.c>`_.
Constructors
- class DBusProxy
- classmethod new_finish(res: AsyncResult) DBusProxy
Finishes creating a
DBusProxy
.Added in version 2.26.
- Parameters:
res – A
AsyncResult
obtained from theAsyncReadyCallback
function passed tonew()
.
- classmethod new_for_bus_finish(res: AsyncResult) DBusProxy
Finishes creating a
DBusProxy
.Added in version 2.26.
- Parameters:
res – A
AsyncResult
obtained from theAsyncReadyCallback
function passed tonew_for_bus()
.
- classmethod new_for_bus_sync(bus_type: BusType, flags: DBusProxyFlags, info: DBusInterfaceInfo | None, name: str, object_path: str, interface_name: str, cancellable: Cancellable | None = None) DBusProxy
Like
new_sync()
but takes aBusType
instead of aDBusConnection
.DBusProxy
is used in this [example][gdbus-wellknown-proxy].Added in version 2.26.
- Parameters:
bus_type – A
BusType
.flags – Flags used when constructing the proxy.
info – A
DBusInterfaceInfo
specifying the minimal interface thatproxy
conforms to orNone
.name – A bus name (well-known or unique).
object_path – An object path.
interface_name – A D-Bus interface name.
cancellable – A
Cancellable
orNone
.
- classmethod new_sync(connection: DBusConnection, flags: DBusProxyFlags, info: DBusInterfaceInfo | None, name: str | None, object_path: str, interface_name: str, cancellable: Cancellable | None = None) DBusProxy
Creates a proxy for accessing
interface_name
on the remote object atobject_path
owned byname
atconnection
and synchronously loads D-Bus properties unless theDO_NOT_LOAD_PROPERTIES
flag is used.If the
DO_NOT_CONNECT_SIGNALS
flag is not set, also sets up match rules for signals. Connect to theDBusProxy
::g-signal signal to handle signals from the remote object.If both
DO_NOT_LOAD_PROPERTIES
andDO_NOT_CONNECT_SIGNALS
are set, this constructor is guaranteed to return immediately without blocking.If
name
is a well-known name and theDO_NOT_AUTO_START
andDO_NOT_AUTO_START_AT_CONSTRUCTION
flags aren’t set and no name owner currently exists, the message bus will be requested to launch a name owner for the name.This is a synchronous failable constructor. See
new()
andnew_finish()
for the asynchronous version.DBusProxy
is used in this [example][gdbus-wellknown-proxy].Added in version 2.26.
- Parameters:
connection – A
DBusConnection
.flags – Flags used when constructing the proxy.
info – A
DBusInterfaceInfo
specifying the minimal interface thatproxy
conforms to orNone
.name – A bus name (well-known or unique) or
None
ifconnection
is not a message bus connection.object_path – An object path.
interface_name – A D-Bus interface name.
cancellable – A
Cancellable
orNone
.
Methods
- class DBusProxy
- call(method_name: str, parameters: Variant | None, flags: DBusCallFlags, timeout_msec: int, cancellable: Cancellable | None = None, callback: Callable[[...], None] | None = None, *user_data: Any) None
Asynchronously invokes the
method_name
method onproxy
.If
method_name
contains any dots, thenname
is split into interface and method name parts. This allows usingproxy
for invoking methods on other interfaces.If the
DBusConnection
associated withproxy
is closed then the operation will fail withCLOSED
. Ifcancellable
is canceled, the operation will fail withCANCELLED
. Ifparameters
contains a value not compatible with the D-Bus protocol, the operation fails withINVALID_ARGUMENT
.If the
parameters
Variant
is floating, it is consumed. This allows convenient ‘inline’ use ofnew()
, e.g.:g_dbus_proxy_call (proxy, "TwoStrings", g_variant_new ("(ss)", "Thing One", "Thing Two"), G_DBUS_CALL_FLAGS_NONE, -1, NULL, (GAsyncReadyCallback) two_strings_done, &data);
If
proxy
has an expected interface (seeDBusProxy
:g-interface-info) andmethod_name
is referenced by it, then the return value is checked against the return type.This is an asynchronous method. When the operation is finished,
callback
will be invoked in the [thread-default main context][g-main-context-push-thread-default] of the thread you are calling this method from. You can then callcall_finish()
to get the result of the operation. Seecall_sync()
for the synchronous version of this method.If
callback
isNone
then the D-Bus method call message will be sent with theNO_REPLY_EXPECTED
flag set.Added in version 2.26.
- Parameters:
method_name – Name of method to invoke.
parameters – A
Variant
tuple with parameters for the signal orNone
if not passing parameters.flags – Flags from the
DBusCallFlags
enumeration.timeout_msec – The timeout in milliseconds (with
%G_MAXINT
meaning “infinite”) or -1 to use the proxy default timeout.cancellable – A
Cancellable
orNone
.callback – A
AsyncReadyCallback
to call when the request is satisfied orNone
if you don’t care about the result of the method invocation.user_data – The data to pass to
callback
.
- call_finish(res: AsyncResult) Variant
Finishes an operation started with
call()
.Added in version 2.26.
- Parameters:
res – A
AsyncResult
obtained from theAsyncReadyCallback
passed tocall()
.
- call_sync(method_name: str, parameters: Variant | None, flags: DBusCallFlags, timeout_msec: int, cancellable: Cancellable | None = None) Variant
Synchronously invokes the
method_name
method onproxy
.If
method_name
contains any dots, thenname
is split into interface and method name parts. This allows usingproxy
for invoking methods on other interfaces.If the
DBusConnection
associated withproxy
is disconnected then the operation will fail withCLOSED
. Ifcancellable
is canceled, the operation will fail withCANCELLED
. Ifparameters
contains a value not compatible with the D-Bus protocol, the operation fails withINVALID_ARGUMENT
.If the
parameters
Variant
is floating, it is consumed. This allows convenient ‘inline’ use ofnew()
, e.g.:g_dbus_proxy_call_sync (proxy, "TwoStrings", g_variant_new ("(ss)", "Thing One", "Thing Two"), G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
The calling thread is blocked until a reply is received. See
call()
for the asynchronous version of this method.If
proxy
has an expected interface (seeDBusProxy
:g-interface-info) andmethod_name
is referenced by it, then the return value is checked against the return type.Added in version 2.26.
- Parameters:
method_name – Name of method to invoke.
parameters – A
Variant
tuple with parameters for the signal orNone
if not passing parameters.flags – Flags from the
DBusCallFlags
enumeration.timeout_msec – The timeout in milliseconds (with
%G_MAXINT
meaning “infinite”) or -1 to use the proxy default timeout.cancellable – A
Cancellable
orNone
.
- call_with_unix_fd_list(method_name: str, parameters: Variant | None, flags: DBusCallFlags, timeout_msec: int, fd_list: UnixFDList | None = None, cancellable: Cancellable | None = None, callback: Callable[[...], None] | None = None, *user_data: Any) None
Like
call()
but also takes aUnixFDList
object.This method is only available on UNIX.
Added in version 2.30.
- Parameters:
method_name – Name of method to invoke.
parameters – A
Variant
tuple with parameters for the signal orNone
if not passing parameters.flags – Flags from the
DBusCallFlags
enumeration.timeout_msec – The timeout in milliseconds (with
%G_MAXINT
meaning “infinite”) or -1 to use the proxy default timeout.fd_list – A
UnixFDList
orNone
.cancellable – A
Cancellable
orNone
.callback – A
AsyncReadyCallback
to call when the request is satisfied orNone
if you don’t care about the result of the method invocation.user_data – The data to pass to
callback
.
- call_with_unix_fd_list_finish(res: AsyncResult) tuple[Variant, UnixFDList]
Finishes an operation started with
call_with_unix_fd_list()
.Added in version 2.30.
- Parameters:
res – A
AsyncResult
obtained from theAsyncReadyCallback
passed tocall_with_unix_fd_list()
.
- call_with_unix_fd_list_sync(method_name: str, parameters: Variant | None, flags: DBusCallFlags, timeout_msec: int, fd_list: UnixFDList | None = None, cancellable: Cancellable | None = None) tuple[Variant, UnixFDList]
Like
call_sync()
but also takes and returnsUnixFDList
objects.This method is only available on UNIX.
Added in version 2.30.
- Parameters:
method_name – Name of method to invoke.
parameters – A
Variant
tuple with parameters for the signal orNone
if not passing parameters.flags – Flags from the
DBusCallFlags
enumeration.timeout_msec – The timeout in milliseconds (with
%G_MAXINT
meaning “infinite”) or -1 to use the proxy default timeout.fd_list – A
UnixFDList
orNone
.cancellable – A
Cancellable
orNone
.
- get_cached_property(property_name: str) Variant | None
Looks up the value for a property from the cache. This call does no blocking IO.
If
proxy
has an expected interface (seeDBusProxy
:g-interface-info) andproperty_name
is referenced by it, thenvalue
is checked against the type of the property.Added in version 2.26.
- Parameters:
property_name – Property name.
- get_cached_property_names() list[str] | None
Gets the names of all cached properties on
proxy
.Added in version 2.26.
- get_connection() DBusConnection
Gets the connection
proxy
is for.Added in version 2.26.
- get_default_timeout() int
Gets the timeout to use if -1 (specifying default timeout) is passed as
timeout_msec
in thecall()
andcall_sync()
functions.See the
DBusProxy
:g-default-timeout property for more details.Added in version 2.26.
- get_flags() DBusProxyFlags
Gets the flags that
proxy
was constructed with.Added in version 2.26.
- get_interface_info() DBusInterfaceInfo | None
Returns the
DBusInterfaceInfo
, if any, specifying the interface thatproxy
conforms to. See theDBusProxy
:g-interface-info property for more details.Added in version 2.26.
- get_name() str | None
Gets the name that
proxy
was constructed for.When connected to a message bus, this will usually be non-
None
. However, it may beNone
for a proxy that communicates using a peer-to-peer pattern.Added in version 2.26.
- get_name_owner() str | None
The unique name that owns the name that
proxy
is for orNone
if no-one currently owns that name. You may connect to theObject
::notify signal to track changes to theDBusProxy
:g-name-owner property.Added in version 2.26.
- new(connection: DBusConnection, flags: DBusProxyFlags, info: DBusInterfaceInfo | None, name: str | None, object_path: str, interface_name: str, cancellable: Cancellable | None = None, callback: Callable[[...], None] | None = None, *user_data: Any) None
Creates a proxy for accessing
interface_name
on the remote object atobject_path
owned byname
atconnection
and asynchronously loads D-Bus properties unless theDO_NOT_LOAD_PROPERTIES
flag is used. Connect to theDBusProxy
::g-properties-changed signal to get notified about property changes.If the
DO_NOT_CONNECT_SIGNALS
flag is not set, also sets up match rules for signals. Connect to theDBusProxy
::g-signal signal to handle signals from the remote object.If both
DO_NOT_LOAD_PROPERTIES
andDO_NOT_CONNECT_SIGNALS
are set, this constructor is guaranteed to complete immediately without blocking.If
name
is a well-known name and theDO_NOT_AUTO_START
andDO_NOT_AUTO_START_AT_CONSTRUCTION
flags aren’t set and no name owner currently exists, the message bus will be requested to launch a name owner for the name.This is a failable asynchronous constructor - when the proxy is ready,
callback
will be invoked and you can usenew_finish()
to get the result.See
new_sync()
and for a synchronous version of this constructor.DBusProxy
is used in this [example][gdbus-wellknown-proxy].Added in version 2.26.
- Parameters:
connection – A
DBusConnection
.flags – Flags used when constructing the proxy.
info – A
DBusInterfaceInfo
specifying the minimal interface thatproxy
conforms to orNone
.name – A bus name (well-known or unique) or
None
ifconnection
is not a message bus connection.object_path – An object path.
interface_name – A D-Bus interface name.
cancellable – A
Cancellable
orNone
.callback – Callback function to invoke when the proxy is ready.
user_data – User data to pass to
callback
.
- new_for_bus(bus_type: BusType, flags: DBusProxyFlags, info: DBusInterfaceInfo | None, name: str, object_path: str, interface_name: str, cancellable: Cancellable | None = None, callback: Callable[[...], None] | None = None, *user_data: Any) None
Like
new()
but takes aBusType
instead of aDBusConnection
.DBusProxy
is used in this [example][gdbus-wellknown-proxy].Added in version 2.26.
- Parameters:
bus_type – A
BusType
.flags – Flags used when constructing the proxy.
info – A
DBusInterfaceInfo
specifying the minimal interface thatproxy
conforms to orNone
.name – A bus name (well-known or unique).
object_path – An object path.
interface_name – A D-Bus interface name.
cancellable – A
Cancellable
orNone
.callback – Callback function to invoke when the proxy is ready.
user_data – User data to pass to
callback
.
- set_cached_property(property_name: str, value: Variant | None = None) None
If
value
is notNone
, sets the cached value for the property with nameproperty_name
to the value invalue
.If
value
isNone
, then the cached value is removed from the property cache.If
proxy
has an expected interface (seeDBusProxy
:g-interface-info) andproperty_name
is referenced by it, thenvalue
is checked against the type of the property.If the
value
Variant
is floating, it is consumed. This allows convenient ‘inline’ use ofnew()
, e.g.g_dbus_proxy_set_cached_property (proxy, "SomeProperty", g_variant_new ("(si)", "A String", 42));
Normally you will not need to use this method since
proxy
is tracking changes using theorg.freedesktop.DBus.Properties.PropertiesChanged
D-Bus signal. However, for performance reasons an object may decide to not use this signal for some properties and instead use a proprietary out-of-band mechanism to transmit changes.As a concrete example, consider an object with a property
ChatroomParticipants
which is an array of strings. Instead of transmitting the same (long) array every time the property changes, it is more efficient to only transmit the delta using e.g. signalsChatroomParticipantJoined(String name)
andChatroomParticipantParted(String name)
.Added in version 2.26.
- Parameters:
property_name – Property name.
value – Value for the property or
None
to remove it from the cache.
- set_default_timeout(timeout_msec: int) None
Sets the timeout to use if -1 (specifying default timeout) is passed as
timeout_msec
in thecall()
andcall_sync()
functions.See the
DBusProxy
:g-default-timeout property for more details.Added in version 2.26.
- Parameters:
timeout_msec – Timeout in milliseconds.
- set_interface_info(info: DBusInterfaceInfo | None = None) None
Ensure that interactions with
proxy
conform to the given interface. See theDBusProxy
:g-interface-info property for more details.Added in version 2.26.
- Parameters:
info – Minimum interface this proxy conforms to or
None
to unset.
Properties
- class DBusProxy
- props.g_bus_type: BusType
If this property is not
NONE
, thenDBusProxy
:g-connection must beNone
and will be set to theDBusConnection
obtained by callingbus_get()
with the value of this property.Added in version 2.26.
- props.g_connection: DBusConnection
The
DBusConnection
the proxy is for.Added in version 2.26.
- props.g_default_timeout: int
The timeout to use if -1 (specifying default timeout) is passed as
timeout_msec
in thecall()
andcall_sync()
functions.This allows applications to set a proxy-wide timeout for all remote method invocations on the proxy. If this property is -1, the default timeout (typically 25 seconds) is used. If set to
%G_MAXINT
, then no timeout is used.Added in version 2.26.
- props.g_flags: DBusProxyFlags
Flags from the
DBusProxyFlags
enumeration.Added in version 2.26.
- props.g_interface_info: DBusInterfaceInfo
Ensure that interactions with this proxy conform to the given interface. This is mainly to ensure that malformed data received from the other peer is ignored. The given
DBusInterfaceInfo
is said to be the “expected interface”.The checks performed are:
When completing a method call, if the type signature of the reply message isn’t what’s expected, the reply is discarded and the
Error
is set toINVALID_ARGUMENT
.Received signals that have a type signature mismatch are dropped and a warning is logged via
warning()
.Properties received via the initial
GetAll()
call or via the::PropertiesChanged
signal (on the org.freedesktop.DBus.Properties interface) or set usingset_cached_property()
with a type signature mismatch are ignored and a warning is logged viawarning()
.
Note that these checks are never done on methods, signals and properties that are not referenced in the given
DBusInterfaceInfo
, since extending a D-Bus interface on the service-side is not considered an ABI break.Added in version 2.26.
Signals
- class DBusProxy.signals
- g_properties_changed(changed_properties: Variant, invalidated_properties: Sequence[str]) None
Emitted when one or more D-Bus properties on
proxy
changes. The local cache has already been updated when this signal fires. Note that bothchanged_properties
andinvalidated_properties
are guaranteed to never beNone
(either may be empty though).If the proxy has the flag
GET_INVALIDATED_PROPERTIES
set, theninvalidated_properties
will always be empty.This signal corresponds to the
PropertiesChanged
D-Bus signal on theorg.freedesktop.DBus.Properties
interface.Added in version 2.26.
- Parameters:
changed_properties – A
Variant
containing the properties that changed (type:a{sv}
)invalidated_properties – A
None
terminated array of properties that was invalidated
- g_signal(sender_name: str | None, signal_name: str, parameters: Variant) None
Emitted when a signal from the remote object and interface that
proxy
is for, has been received.Since 2.72 this signal supports detailed connections. You can connect to the detailed signal
g-signal::x
in order to receive callbacks only when signalx
is received from the remote object.Added in version 2.26.
- Parameters:
sender_name – The sender of the signal or
None
if the connection is not a bus connection.signal_name – The name of the signal.
parameters – A
Variant
tuple with parameters for the signal.