如何在python的扩展类中使用dbus导出方法,继承的方法? [英] How export methods with dbus in a extended class in python, inherited methods?
问题描述
我有一个顶级类和一些扩展顶级类的类,但是子类中的几乎所有方法都来自顶级类(使用继承,无需重新实现),所以子类中没有这些方法,如何为每个子类(每个子类的名称作为dbus路径的一部分)用dbus导出它们?
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 don't have the methods in the child classes, how could I export them with dbus for each child class (with the name of each child class as part of dbus path)?
我将显示一个示例代码来说明,我的课程结构是:
I will show a example code for clarify, my class structure is:
Window (main class)
|--WindowOne (child class)
|--WindowTwo
|--WindowThree
我的dbus界面是 com.example.MyInterface
,我想使用以下方法访问每个子类: com.example.MyInterface.WindowOne
,并针对每个孩子我想访问这些方法,包括从主类继承的方法,例如 com.example.MyInterface.WindowOne.show
和 com。 example.MyInterface.WindowOne.destroy
。
My interface for dbus is com.example.MyInterface
and I would like to access each child class using: com.example.MyInterface.WindowOne
, and for each child class I would like to access the methods, inclusive the inherited methods from the main class, like com.example.MyInterface.WindowOne.show
and com.example.MyInterface.WindowOne.destroy
.
在此代码中,我将子类'WindowOne'扩展为'Window'类,方法是 show()
和 destroy()
在 Window中没有在 WindowOne中重新实现,但是在这段代码中,我放入了方法 show()
来解释问题,即获得此方法的方法。工作的代码是这样,我在子类中重新声明了方法 show()
,但这似乎很糟糕。
In this code, I extend the child class 'WindowOne' with the 'Window' class, the methods show()
and destroy()
in the 'Window' are not re-implemented in 'WindowOne', but in this code I put the method show()
to explain the problem, the way I get the code to work was this, I re-declare the method show()
in child class, but this seems bad.
最大的问题可能是:有某种使用装饰器的方法: @ dbus.service.method('com.example.MyInterface.WindowOne')
用于类(
The big question maybe is: There is some way to use the decorator: @dbus.service.method('com.example.MyInterface.WindowOne')
for classes (child classes in this case)?
测试源代码:
# 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()
推荐答案
经过一番试验,我意识到一些必不可少的东西,但我找不到文档中的e:导出方法的路径不需要与导出对象的路径相同!说明:如果未在子类中重新实现该方法( WindowOne
),我不需要使用 @ dbus.service.method('com.example.MyInterface.WindowOne')
,例如,我只需要使用以下方法在主类( Window
)中导出该方法: @ dbus.service.method('com。 example.MyInterface.Window')
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')
因此,在导出顶级类<$ c $的方法时,我只需要使用固定路径c> 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 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()
在调用总线方法的代码中,我只使用
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')
显示方法
在顶级类 Window
中被调用,但在作为子项的对象 WindowOne
中执行
The method show
is called in the top class Window
, but is executed in the object WindowOne
that is a child class.
然后在子类 WindowOne $中调用
update
方法c $ c>,因为它正在重新实现顶级方法。
And the method update
is called in the child class WindowOne
, because it is reimplementing the top class method.
这篇关于如何在python的扩展类中使用dbus导出方法,继承的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!