Skip to content Skip to sidebar Skip to footer

How Export Methods With Dbus In A Extended Class In Python, Inherited Methods?

I have a top class and classes that extend the top class, but almost all methods from the child classes are from the top class (using inheritance, without reimplementation), so I d

Solution 1:

After some experimentation, I realize something essential, that I don't find before in documentation: The path for exported methods don't need to have the same path of exported object! Clarifing: If the method is not reimplemented in the child class (WindowOne), I don't need to export it in the child class using @dbus.service.method('com.example.MyInterface.WindowOne') , for example, I just need to export the method in the main class (Window) using: @dbus.service.method('com.example.MyInterface.Window')

So I just need to use a fixed path when export the method of the top class Window, see in the fixed code below.

# interface importsfrom gi.repository import Gtk

# dbus imports  import dbus
import dbus.service
from dbus.mainloop.glib import DBusGMainLoop

# Main window classclassWindow(dbus.service.Object):
    def__init__(self, gladeFilePath, name):
        # ... inicialization
        self.name = name
        self.busName = dbus.service.BusName('com.example.MyInterface.', bus=dbus.SessionBus())
        dbus.service.Object.__init__(self, self.busName, '/com/example/MyInterface/' + self.name)

    @dbus.service.method('com.example.MyInterface.Window')defshow(self):
        self.window.show_all()

    @dbus.service.method('com.example.MyInterface.Window')defdestroy(self):
        Gtk.main_quit()

    @dbus.service.method('com.example.MyInterface.Window')defupdate(self, data):
        # top class 'update' method# Child window classclassWindowOne(Window):
    def__init__(self, gladeFilePath):
        Window.__init__(self, gladeFilePath, "WindowOne")

    @dbus.service.method('com.example.MyInterface.WindowOne')defupdate(self, data):
        # reimplementation of top class 'update' methodif __name__ == "__main__":
    DBusGMainLoop(set_as_default=True)

    gladeFilePath = "/etc/interface.glade"
    windowOne = WindowOne(gladeFilePath)

    Gtk.main()

In the code for call the bus method, I just use like below:

bus = dbus.SessionBus()
dbusWindowOne = bus.get_object('com.example.MyInterface', '/com/example/MyInterface/WindowOne')
showWindowOne = dbusWindowOne.get_dbus_method('show', 'com.example.MyInterface.Window')
updateWindowOne = dbusWindowOne.get_dbus_method('update', 'com.example.MyInterface.WindowOne')

The method show is called in the top class Window, but is executed in the object WindowOne that is a child class.

And the method update is called in the child class WindowOne, because it is reimplementing the top class method.

Post a Comment for "How Export Methods With Dbus In A Extended Class In Python, Inherited Methods?"