0

トップクラスとトップクラスを拡張するクラスがありますが、子クラスのほとんどすべてのメソッドがトップクラスからのものであるため(継承を使用し、再実装せずに)、子クラスにメソッドがありません。どうすればよいですか?各子クラスのdbusを使用して(dbusパスの一部として各子クラスの名前を使用して)それらをエクスポートしますか?

明確にするためのサンプルコードを示します。私のクラス構造は次のとおりです。

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

このコードでは、子クラス「WindowOne」を「Window」クラスで拡張します。メソッドshow()destroy()「Window」は「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()
4

1 に答える 1

0

いくつかの実験の結果、以前はドキュメントで見つけられなかった重要なことがわかりました。エクスポートされたメソッドのパスは、エクスポートされたオブジェクトのパスと同じである必要はありません。明確化:メソッドが子クラス(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

于 2012-12-28T13:05:20.587 に答える