File: sync_api.rst

package info (click to toggle)
python-sdbus 0.14.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 980 kB
  • sloc: python: 7,783; ansic: 2,524; makefile: 9; sh: 4
file content (259 lines) | stat: -rw-r--r-- 8,137 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
Blocking API
============

Classes
+++++++++++++++

.. py:currentmodule:: sdbus

.. py:class:: DbusInterfaceCommon(interface_name)

    D-Bus interface class.
    D-Bus methods and properties should be defined using
    :py:func:`dbus_property` and :py:func:`dbus_method` decorators.

    :param str interface_name: Sets the D-Bus interface
        name that will be used for all properties and methods
        defined in the body of the class

    .. py:method:: __init__(service_name, object_path, [bus])

        Init will create a proxy to a remote object

        :param str service_name: 
            Remote object D-Bus connection name.
            For example, systemd uses ``org.freedesktop.systemd1``

        :param str object_path:
            Remote object D-Bus path.
            Should be a forward slash separated path.
            Starting object is usually ``/``.
            Example: ``/org/freedesktop/systemd/unit/dbus_2eservice``

        :param SdBus bus:
            Optional D-Bus connection object.
            If not passed the default D-Bus will be used.

    .. py:method:: dbus_ping()

        Pings the remote service using D-Bus.

        Useful to test if connection or remote service is alive.

        .. warning:: This method is ignores the particular object path
                     meaning it can NOT be used to test if object exist.

    .. py:method:: dbus_machine_id()

        Returns the machine UUID of D-Bus the object is connected to.

        :return: machine UUID
        :rtype: str

    .. py:method:: dbus_introspect()

        Get D-Bus introspection XML.

        It is users responsibility to parse that data.

        :return: string with introspection XML
        :rtype: str

    .. py:method:: properties_get_all_dict()

        Get all object properties as a dictionary where keys are member
        names and values are properties values.

        Equivalent to ``GetAll`` method of the ``org.freedesktop.DBus.Properties``
        interface but the member names are automatically translated to python
        names. (internally calls it for each interface used in class definition)

        :param str on_unknown_member: If an unknown D-Bus property was encountered
            either raise an ``"error"`` (default), ``"ignore"`` the property
            or ``"reuse"`` the D-Bus name for the member.
        :return: dictionary of properties
        :rtype: dict[str, Any]

    Example: ::

        from sdbus import (DbusInterfaceCommon,
                           dbus_method, dbus_property)


        class ExampleInterface(DbusInterfaceCommon,
                               interface_name='org.example.my'
                               ):

            # Method that takes an integer and does not return anything
            @dbus_method('u')
            def close_notification(self, an_int: int) -> None:
                raise NotImplementedError

            # Method that does not take any arguments and returns a list of str
            @dbus_method()
            def get_capabilities(self) -> list[str]:
                raise NotImplementedError

            # Method that takes a dict of {str: str} and returns an int
            @dbus_method('a{ss}')
            def count_entries(self, a_dict: dict[str, str]) -> int:
                raise NotImplementedError

            # Read only property of int
            @dbus_property()
            def test_int(self) -> int:
                raise NotImplementedError

            # Read/Write property of str
            @dbus_property('s')
            def test_string(self) -> str:
                raise NotImplementedError


.. py:class:: DbusObjectManagerInterface(interface_name)

    This class is almost identical to :py:class:`DbusInterfaceCommon`
    but implements `ObjectManager <https://dbus.freedesktop.org/doc/dbus-specification.html#standard-interfaces-objectmanager>`_
    interface.

    .. py:method:: get_managed_objects()

        Get the objects this object manager in managing.

        :return:
            Triple nested dictionary that contains all the objects
            paths with their properties values.

            dict[ObjectPath, dict[InterfaceName, dict[PropertyName, PropertyValue]]]

        :rtype: dict[str, dict[str, dict[str, Any]]]

Decorators
+++++++++++++++

.. py:decorator:: dbus_method([input_signature, [flags, [method_name]]])

    Define D-Bus method

    Decorated function becomes linked to D-Bus method.
    Always use round brackets () even when not passing any arguments.

    :param str input_signature: D-Bus input signature.
        Defaults to "" meaning method takes no arguments.
        Required if method takes any arguments.

    :param int flags: modifies behavior.
        No effect on remote connections.
        Defaults to 0 meaning no special behavior.

        See :ref:`dbus-flags` .

    :param str method_name: Explicitly define remote method name.
        Usually not required as remote method name will be constructed
        based on original method name.

    Defining methods example: ::

        from sdbus import DbusInterfaceCommon, dbus_method


        class ExampleInterface(DbusInterfaceCommon,
                               interface_name='org.example.my'
                               ):

            # Method that takes an integer and does not return anything
            @dbus_method('u')
            def close_notification(self, an_int: int) -> None:
                raise NotImplementedError

            # Method that does not take any arguments and returns a list of str
            @dbus_method()
            def get_capabilities(self) -> list[str]:
                raise NotImplementedError

            # Method that takes a dict of {str: str} and returns an int
            @dbus_method('a{ss}')
            def count_entries(self, a_dict: dict[str, str]) -> int:
                raise NotImplementedError

    Calling methods example::

        # Initialize the object
        d = ExampleInterface(
            service_name='org.example.test',
            object_path='/',
        )

        d.close_notification(1234)

        l = d.get_capabilities()

        d.count_entries({'a': 'asdasdasd', 'b': 'hgterghead213d'})


.. py:decorator:: dbus_property([property_signature, [flags, [property_name]]])

    Define D-Bus property

    Property works just like @property decorator would.
    Always use round brackets () even when not passing any arguments.

    Read only property can be indicated by passing empty D-Bus signature "".

    Trying to assign a read only property will raise :py:exc:`AttributeError`

    :param str property_signature: D-Bus property signature.
        Empty signature "" indicates read-only property.
        Defaults to empty signature "".
        Required only for writable properties.

    :param int flags: modifies behavior.
        No effect on remote connections.
        Defaults to 0 meaning no special behavior.

        See :ref:`dbus-flags` .

    :param str property_name: Explicitly define remote property name.
        Usually not required as remote property name will be constructed
        based on original method name.

    Defining properties example: ::

        from sdbus import DbusInterfaceCommon, dbus_property


        class ExampleInterface(DbusInterfaceCommon,
                               interface_name='org.example.myproperty'
                               ):

            # Property of int
            @dbus_property('i')
            def test_int(self) -> int:
                raise NotImplementedError

            # Property of str
            @dbus_property('s')
            def test_string(self) -> str:
                raise NotImplementedError

    Properties usage example::

        # Initialize the object
        d = ExampleInterface(
            service_name='org.example.test',
            object_path='/',
        )

        # Print the int
        print(d.test_int)

        # Assign new string
        d.test_string = 'some_string'

        # Print it
        print(d.test_string)


* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`