[how-to] Using DBUS from Python on Sailfish OS
asked 2018-03-20 01:53:44 +0200
This post is a wiki. Anyone with karma >75 is welcome to improve it.
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:
- Sailfish OS device with up to date Salfish OS install (2.1.3+)
- being able to install packages from official repos
- being able to run 2 concurrent shell sessions (2 SSH connections, screen, tmux, etc.)
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 ?
- the server part of the example can be used for providing a DBUS API from PYthon applications to other Sailfish OS applications (for example a DBUS API for a maping application to focus on a set of coordinates, etc.)
- the client part of the example can be used by Python applications to connect to existing Sailfish OS DBUS APIs (both the system & various applications provide DBUS APIs)
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
- apparently not Harbour compatible yet (""No OS version and packages satisfy package requirements: Requirement python3-gobject not available") but I have not checked for this myself
Feedback, correction & tutorial enhancements welcome! :)
Fantastic @MartinK, thanks for sharing!
lakutalo ( 2018-03-20 09:12:25 +0200 )edit