2012-12-07 3 views
0

나는 상위 클래스를 확장 상위 클래스와 클래스를 가지고 있지만, 자식 클래스의 거의 모든 방법은 (재 구현하지 않고 상속을 사용하여) 최고 수준에서, 그래서 자식 클래스의 메소드가없는, 어떻게 각 하위 클래스 (dbus 경로의 일부로 각 하위 클래스의 이름 포함)에 대해 dbus로 내보낼 수 있습니까?확장 클래스에서 dbus를 사용하여 내보내는 방법은 Python에서 상속 된 메서드입니까?

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

DBUS에 대한 나의 인터페이스는 com.example.MyInterface 내가 사용하여 각 자식 클래스 접근하고 싶습니다 : 명확히하기 위해

나는 예제 코드를 표시합니다, 내 수업 구조는 com.example.MyInterface.WindowOne를하고, 각 하위 클래스 I에 대한 com.example.MyInterface.WindowOne.showcom.example.MyInterface.WindowOne.destroy과 같은 기본 클래스의 상속 된 메서드를 비롯하여 메서드에 액세스하려고합니다. 이 코드에서

, 나는 '창'의 방법 show()destroy()가 'WindowOne'에서 다시 구현되지는 '창'클래스와 자식 클래스의 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() 
+0

나는 장식과 DBUS 너무 잘 모르겠지만, 왜 당신은'__init__' 기능을 장식과에 공급되는'name'을 사용하지 않는? – deinonychusaur

+0

은 아마 당신이 무엇을 의미하는지 이해하지 않는,하지만 난에 __init__''에 줄을 변경하려고 '. com.example.MyInterface''self.busName = dbus.service.BusName (+ self.name, 버스 = dbus.SessionBus())',이 버스는 이름 버스를 바꾼다. 내 보낸 메소드는 com.example.MyInterface.WindowOne와 같은 완전한 이름을 가진 적절한 데코레이터를 가질 필요가있다. self.name은 내부에서 작동하지 않는다. 데코레이터 : ( – Msum

+0

나는 전에 깨닫지 못했던 또 다른 문제 인 com.example.MyInterface를 알아 냈습니다.WindowOne'은이 클래스의 한 객체가 인스턴스화 된 경우에만 사용할 수 있습니다. 그러나이 작업은 기본 창에서 발생하기 때문에 다른 창은 여전히 ​​dbus에서 사용할 수 없습니다. 나는 더 나은 해결책으로 생각할 것이다. 어떤 도움이라도 환영받을 것이다. 감사. – Msum

답변

0

, 나는이 문서에서 이전에 발견하지 않는 것이 뭔가 중요한 실현 : 수출 방법에 대한 경로가 동일한 경로를 할 필요가 없습니다 내 보낸 개체의! Clarifing : 방법은 하위 클래스 (WindowOne), 내가 예를 들어, 난 그냥 메인 클래스 (Window)의 방법을 내보낼 필요가 @dbus.service.method('com.example.MyInterface.WindowOne')를 사용하여 자식 클래스에 내보낼 필요가 사용하지 않는에서 다시 구현되어 있지 않은 경우 : @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') 

상위 클래스 Window에라고 show 방법 만있는 객체 WindowOne에서 실행 아이 클래스.

은 최상위 클래스 메소드를 다시 구현하기 때문에 하위 클래스 WindowOne에서 호출됩니다.

관련 문제