如何在继承方法的python中的扩展类中导出带有dbus的方法



我有一个top类和扩展top类的类,但几乎所有来自子类的方法都来自top类(使用继承,没有重新实现),所以我没有子类中的方法,我如何将它们与每个子类的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(),但这似乎很糟糕。

最大的问题可能是:有什么方法可以将decorator:@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')在子类中导出它,例如,我只需要使用:@dbus.service.method('com.example.MyInterface.Window')在主类(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中被调用,因为它正在重新实现顶级方法。

最新更新