Search code examples
python-3.xlinuxdbus

How to choose and use a python3 dbus library to replace a dbus-send call


Using ubuntu 20.10

I want to write a python script to process the output of this shell command:

dbus-send --print-reply --dest=org.gnome.SessionManager /org/gnome/SessionManager org.gnome.SessionManager.GetInhibitors

I don't know anything about coding dbus. The first place to start was I hoped a good python library.

I learn this about the apparently canonical library: "dbus-python is a legacy API, built with a deprecated dbus-glib library, and involving a lot of type-guessing (despite "explicit is better than implicit" and "resist the temptation to guess") (from https://wiki.python.org/moin/DbusExamples)

The library https://pypi.org/project/dbus-next/ promises a pure python implementation: "Zero dependencies and pure Python 3." It is not mentioned on the wiki page mentioned above, but it looks like a healthy project. However it seems that the pure python approach requires me to use the asyncio version. I think this is overkill for my needs.

I tried using dasbus but I can't install the necessary libraries, at least not in a virtual env. Despite installing the system package python3-gi, I get errors "no module named gi" and trying to install PyGObject fails because "no package 'cairo' found" ... and ERROR: failed building wheel for pycairo.

So at this point, I have a library which is old and not recommended, a library with dependency difficulties and a library which seems to force me to use asyncio.

I now understand why the php script I am trying to re-write in python executed a shell command and dealt with the ugly output.

On top of that, I think I have worked out these points:

I need the SessionBus

The "path" is "/org/gnome/SessionManager" The "interface" is org.gnome.SessionManager I want to "call" the "member" GetInhibitors

I find async a bit of overkill since this is a shell script but if this is only way I can avoid a C dependency, I will deal with it.

Which library should I use?


Solution

  • pydbus is a very capable library that allows me to build things quickly and simply although the repo isn't necessarily that active.

    I've only used dbus-next library a few times and it does have various *_sync methods if you don't want to do things asynchronously.