We have moved to a new Sailfish OS Forum. Please start new discussions there.
1 | initial version | posted 2018-03-20 01:53:44 +0200 |
So turns out it's finally possible to easily use DBUS from Python applications on Sailfish OS via the (pure Python) Pydbus library. I'm familiar with Pydbus from using it in the Anaconda installer, so I've decided to give it a try on Sailfish OS as well.
And it turns to be really, really easy - I've got a client-server example running in ~5 minutes. So I've decided to write this short tutorial to get other started as well. :)
From Zero to Python DBUS client/server in a few easy steps
Requirements:
0) install the gobject-python3 package (required to use Pydbus):
pkcon install python3-gobject
1) clone the pydbus repo:
git clone https://github.com/LEW21/pydbus
2) in one shell run the server:
cd pydbus
PYTHONPATH=. python3 examples/clientserver/server.py
3) in the second shell run the client:
cd pydbus
PYTHONPATH=. python3 examples/clientserver/client.py
4) the client should get output like this (reply from the server & a string passed from the client and returned by the server):
Hello, World!
test 123
Traceback (most recent call last):
File "examples/clientserver/client.py", line 21, in <module>
the_object.Quit()
File "/home/nemo/software/pydbus/pydbus/proxy_method.py", line 75, in __call__
0, timeout_to_glib(timeout), None).unpack()
gi._glib.GError: GDBus.Error:org.freedesktop.DBus.Error.NoReply: Message recipient disconnected from message bus without replying
You can ignore the traceback at the end, it's caused by the example code being very simple, which includes simplified server shutdown.
5) you can also notice the server shutting down in the first shell as the client told it to quit over DBUS
How is this useful for my application ?
You can use the D-Bus Inspector & Visual D-Bus applications (both available from Jolla Store) to explore available DBUS APIs and/or to test your own DBUS API.
How do I integrate this with my Python application ?
You need to include the python3-gobject package (available from official repositories) as a dependency of your application.
Then just include pydbus (which is a pure-Python library) with your application code in the same manner as any other third party Python library.
(Jolla developers said they preffer developers bindling pydbus with their applications rather than providing it from the official repositories as well.)
Current issues
Feedback, correction & tutorial enhancements welcome! :)