I want to make an application that will be using GDBus, but the API reference is too difficult (which function to start with? What is the best way? Am I doing it right?..) and the code examples I could find online are written in C. I saw a similar thread but it is for PyDBus and what I want is use of GDBus in Python.
I’m stuck at handling signal connection, this is my progress :
import gi
from gi.repository import Gio, GLib, GObject
# Changed from Alarm to Printer for simplicity
class Printer(Gio.DBusInterfaceSkeleton):
def __init__(self, **kwargs):
Gio.DBusInterfaceSkeleton.__init__(self)
self.msg = "Hello"
def say_something(self):
print(self.msg)
def on_handle_configure(printer, invocation, seconds):
printer.say_something()
def on_name_acquired(conn, name):
printer = Printer()
Printer.connect("handle-configure", on_handle_configure, None) # => TypeError
Printer.export(conn, "/com/movk/printer")
Gio.bus_own_name(Gio.BusType.SESSION,
"com.movk.printer",
Gio.BusNameOwnerFlags.NONE,
None,
on_name_acquired,
None)
GLib.MainLoop().run()
The error is :
File "service.py", line 22, in on_name_acquired
Printer.connect("handle-configure", on_handle_configure, None)
TypeError: descriptor 'connect' requires a 'gi._gi.GObject' object but received a 'str'
File "service.py", line 22, in on_name_acquired
Printer.connect("handle-configure", on_handle_configure, None)
TypeError: descriptor 'connect' requires a 'gi._gi.GObject' object but received a 'str'
Ahhh its confusing. There are two things, Gio.DBusInterfaceSkeleton, which has export method but not add_interface and Gio.DBusObjectSkeleton which has your add_interface method but not export. Which one to use? How to use? Can you give an example?
I believe you should create an InterfaceSkeleton, add the methods you want to publish there, then add the interface to an ObjectSkeleton and export that.
There is a lot to get your head around the first time! My advice is to experiment, and also to look at other similar projects even if they are written in a different languages
If there’s a segmentation fault, you can use gdb to find out what happened. Use gdb --args python3 ... to run your program, type run to start execution, then backtrace to see where it crashed.
It’s reasonably common that PyGI code can trigger segfaults because it can call directly into the C code without checking that parameters you passed make sense, so if you pass an integer when the C code expects a string for example it’ll lead to a crash.
Running it with service.py already running, I got this:
$ python3 client.py 13
Error: g-dbus-error-quark: GDBus.Error:org.freedesktop.DBus.Error.UnknownMethod: No such interface “org.gtk.GDBus.TestInterface” on object at path /org/Gtk/GDBus/TestInterface (19)