トップクラスとトップクラスを拡張するクラスがありますが、子クラスのほとんどすべてのメソッドがトップクラスからのものであるため(継承を使用し、再実装せずに)、子クラスにメソッドがありません。どうすればよいですか?各子クラスの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()