Package gavo :: Package base :: Module observer
[frames] | no frames]

Source Code for Module gavo.base.observer

 1  """ 
 2  Observers are objects listening to EventDispatchers. 
 3   
 4  They are mostly used as bases for UIs in the context of the DC. 
 5  """ 
 6   
 7  #c Copyright 2008-2019, the GAVO project 
 8  #c 
 9  #c This program is free software, covered by the GNU GPL.  See the 
10  #c COPYING file in the source distribution. 
11   
12   
13 -def listensTo(*args):
14 """is a decorator to make a method listen to a set of events. 15 16 It receives one or more event names. 17 """ 18 def deco(meth): 19 meth.listensTo = args 20 return meth
21 return deco 22 23
24 -class ObserverBase(object):
25 """is a base class for observers. 26 27 Observers have methods listening to certain events. Use the listen 28 decorator above to make the connections. The actual event subscriptions 29 are done in the constructor. 30 31 The signature of the listeners always is:: 32 33 listener(dispatcher, arg) -> ignored 34 35 dispatcher is the EventDispatcher instance propagating the event. It 36 has lots of useful attributes explained in base.event's notifyXXX docstrings. 37 38 You can listen to anything that has a notify method in the EventDispatcher. 39 """
40 - def __init__(self, dispatcher):
41 self.dispatcher = dispatcher 42 for name in dir(self): 43 att = getattr(self, name) 44 if hasattr(att, "listensTo"): 45 for ev in att.listensTo: 46 dispatcher.subscribe(ev, att)
47