Skip to content

Commit c83f092

Browse files
committed
Document dbus-python heritage and static method implementation
Point out that DBusMock objects are built on top of standard dbus-python service objects, and thus can use all of its API. Explain that AddMethod() is just a "remote control" API for dynamically adding methods. Templates can also implement them in the usual static way.
1 parent eb0b1aa commit c83f092

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,11 @@ with the template's convenience methods like
220220
or calling `AddObject()` yourself with the desired properties, of
221221
course.
222222

223+
Templates commonly implement some non-trivial functionality with actual Python
224+
methods and the standard [dbus-python](https://dbus.freedesktop.org/doc/dbus-python/)
225+
[`@dbus.service.method`](https://dbus.freedesktop.org/doc/dbus-python/dbus.service.html#dbus.service.method)
226+
decorator.
227+
223228
If you want to contribute a template, look at
224229
dbusmock/templates/upower.py for a real-life implementation. You can
225230
copy dbusmock/templates/SKELETON to your new template file name and

dbusmock/mockobject.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ class DBusMockObject(dbus.service.Object): # pylint: disable=too-many-instance-
126126
This can be configured to have arbitrary methods (including code execution)
127127
and properties via methods on the org.freedesktop.DBus.Mock interface, so
128128
that you can control the mock from any programming language.
129+
130+
Beyond that "remote control" API, this is a standard dbus-python service object, see
131+
<https://dbus.freedesktop.org/doc/dbus-python/tutorial.html#exporting-objects>.
129132
'''
130133

131134
def __init__(self, bus_name: str, path: str, interface: str, props: PropsType,
@@ -262,7 +265,7 @@ def Set(self, interface_name: str, property_name: str, value: Any, *_, **__) ->
262265
in_signature='ssa{sv}a(ssss)',
263266
out_signature='')
264267
def AddObject(self, path: str, interface: str, properties: PropsType, methods: List[MethodType]) -> None:
265-
'''Add a new D-Bus object to the mock
268+
'''Dynamically add a new D-Bus object to the mock
266269
267270
path: D-Bus object path
268271
interface: Primary D-Bus interface name of this object (where
@@ -359,7 +362,7 @@ def Reset(self) -> None:
359362
in_signature='sssss',
360363
out_signature='')
361364
def AddMethod(self, interface, name: str, in_sig: str, out_sig: str, code: str) -> None:
362-
'''Add a method to this object
365+
'''Dynamically add a method to this object
363366
364367
interface: D-Bus interface to add this to. For convenience you can
365368
specify '' here to add the method to the object's main
@@ -385,6 +388,14 @@ def AddMethod(self, interface, name: str, in_sig: str, out_sig: str, code: str)
385388
386389
When specifying '', the method will not do anything (except
387390
logging) and return None.
391+
392+
393+
This is meant for adding a method to a mock at runtime, from any programming language.
394+
You can also use it in templates in the load() function.
395+
396+
For implementing non-trivial and static methods in templates, it is recommended to
397+
implement them in the normal dbus-python way with using the @dbus.service.method
398+
decorator instead.
388399
'''
389400
# pylint: disable=protected-access
390401

0 commit comments

Comments
 (0)