<s> """<STR_LIT>""" <EOL> EVENTS = [ ] <EOL> CALLBACKS = { } <EOL> def declare ( nevents ) : <EOL> """<STR_LIT>""" <EOL> for evt in nevents : <EOL> if evt not in EVENTS : <EOL> EVENTS . append ( evt ) <EOL> def register ( event , callback ) : <EOL> """<STR_LIT>""" <EOL> if event not in CALLBACKS . keys ( ) : <EOL> CALLBACKS [ event ] = { } <EOL> fullname = "<STR_LIT>" % ( callback . __module__ , callback . __name__ ) <EOL> if fullname not in CALLBACKS [ event ] : <EOL> CALLBACKS [ event ] [ fullname ] = callback <EOL> return <NUM_LIT:1> <EOL> class observe ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * evtnames , ** kwargs ) : <EOL> self . evtnames = evtnames <EOL> def __call__ ( self , f ) : <EOL> for evt in self . evtnames : <EOL> register ( evt , f ) <EOL> return f <EOL> def unregister ( event , callback ) : <EOL> """<STR_LIT>""" <EOL> if event not in EVENTS : <EOL> return False <EOL> if event not in CALLBACKS : <EOL> return False <EOL> fullname = "<STR_LIT>" % ( callback . __module__ , callback . __name__ ) <EOL> try : <EOL> del CALLBACKS [ event ] [ fullname ] <EOL> except KeyError : <EOL> pass <EOL> def raiseEvent ( event , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if event not in EVENTS or event not in CALLBACKS : <EOL> return <NUM_LIT:0> <EOL> for callback in CALLBACKS [ event ] . values ( ) : <EOL> callback ( * args , ** kwargs ) <EOL> return <NUM_LIT:1> <EOL> def raiseQueryEvent ( event , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> result = [ ] <EOL> if event not in EVENTS or event not in CALLBACKS : <EOL> return result <EOL> for callback in CALLBACKS [ event ] . values ( ) : <EOL> tmp = callback ( * args , ** kwargs ) <EOL> if tmp is None : <EOL> continue <EOL> result += tmp <EOL> return result <EOL> def raiseDictEvent ( event , * args ) : <EOL> """<STR_LIT>""" <EOL> result = { } <EOL> if event not in EVENTS or event not in CALLBACKS : <EOL> return result <EOL> for callback in CALLBACKS [ event ] . values ( ) : <EOL> tmp = callback ( * args ) <EOL> if tmp is None : <EOL> continue <EOL> for k , v in tmp . iteritems ( ) : <EOL> result [ k ] = v <EOL> return result </s>
