簡體   English   中英

如何在python的擴展類中使用dbus導出方法,繼承的方法?

[英]How export methods with dbus in a extended class in python, inherited methods?

我有一個頂層類和一個擴展頂層類的類,但是子類的幾乎所有方法都來自頂層類(使用繼承,而無需重新實現),所以子類中沒有這些方法,我怎么能為每個子類用dbus導出它們(每個子類的名稱作為dbus路徑的一部分)?

我將顯示一個示例代碼進行澄清,我的類結構為:

Window (main class)
  |--WindowOne (child class)
  |--WindowTwo
  |--WindowThree

我的dbus接口是com.example.MyInterface ,我想使用com.example.MyInterface.WindowOne訪問每個子類,對於每個子類,我都希望訪問方法,包括從主類繼承的方法,例如com.example.MyInterface.WindowOne.showcom.example.MyInterface.WindowOne.destroy

在這段代碼中,我用“ Window”類擴展了子類“ WindowOne”,“ Window”中的show()destroy()方法沒有在“ WindowOne”中重新實現,但是在這段代碼中,我將方法show()來解釋問題,我讓代碼起作用的方式是這樣,我在子類中重新聲明了方法show() ,但這似乎很糟糕。

最大的問題可能是:有某種使用裝飾器的方法: @dbus.service.method('com.example.MyInterface.WindowOne')用於類(在這種情況下為子類)?

測試源代碼:

# interface imports
from gi.repository import Gtk

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

# Main window class
class Window(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)

    def show(self):
        self.window.show_all()

    def destroy(self):
        Gtk.main_quit()


# Child window class
class WindowOne(Window):
    def __init__(self, gladeFilePath):
        Window.__init__(self, gladeFilePath, "WindowOne")

    @dbus.service.method('com.example.MyInterface.WindowOne')
    def show(self):
        self.window.show_all()


if __name__ == "__main__":
    DBusGMainLoop(set_as_default=True)

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

    Gtk.main()

經過一些試驗,我意識到一些必不可少的東西,這些是我以前在文檔中找不到的: 導出方法的路徑不必具有與導出對象相同的路徑! 澄清:如果未在子類( WindowOne )中重新實現該方法,則不需要使用@dbus.service.method('com.example.MyInterface.WindowOne')在子類中導出該方法,例如,我只需要使用以下方法導出主類( Window )中的方法: @dbus.service.method('com.example.MyInterface.Window')

因此,在導出頂級Window的方法時,我只需要使用固定路徑,請參見下面的固定代碼。

# interface imports
from gi.repository import Gtk

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

# Main window class
class Window(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')
    def show(self):
        self.window.show_all()

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

    @dbus.service.method('com.example.MyInterface.Window')
    def update(self, data):
        # top class 'update' method


# Child window class
class WindowOne(Window):
    def __init__(self, gladeFilePath):
        Window.__init__(self, gladeFilePath, "WindowOne")

    @dbus.service.method('com.example.MyInterface.WindowOne')
    def update(self, data):
        # reimplementation of top class 'update' method


if __name__ == "__main__":
    DBusGMainLoop(set_as_default=True)

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

    Gtk.main()

在調用總線方法的代碼中,我僅使用如下代碼:

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')

show方法在頂級類Window調用,但在作為子類的對象WindowOne中執行。

並且方法update在子類WindowOne被調用,因為它正在重新實現頂級方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM