如何在python的扩展类中使用dbus导出方法,继承方法?

时间:2012-12-07 14:32:27

标签: python ubuntu pygtk dbus

我有一个顶级类和扩展顶级类的类,但几乎所有来自子类的方法都来自顶级类(使用继承,没有重新实现),所以我没有子类中的方法,我怎么能用dbus为每个子类导出它们(每个子类的名称作为dbus路径的一部分)?

我将展示一个澄清的示例代码,我的类结构是:

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

我的dbus接口是com.example.MyInterface,我想使用:com.example.MyInterface.WindowOne访问每个子类,并且对于每个子类,我想访问这些方法,包括从main继承的方法类,如com.example.MyInterface.WindowOne.showcom.example.MyInterface.WindowOne.destroy

在此代码中,我使用'Window'类扩展子类'WindowOne','Window'中的方法show()destroy()未在'WindowOne'中重新实现,但在这段代码中我把方法show()解释为问题,我得到代码的方式是这样,我在子类中重新声明了方法show(),但这看起来很糟糕。 / p>

最大的问题可能是:有一些方法可以使用装饰器:@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()

1 个答案:

答案 0 :(得分:0)

经过一些实验,我发现了一些必不可少的东西,我以前在文档中找不到:导出方法的路径不需要具有相同的导出对象路径! Clarifing:If该方法未在子类(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中调用,因为它重新实现了顶级方法。

相关问题