<s> from calvin . utilities import calvinuuid <EOL> from calvin . utilities import calvinlogger <EOL> _log = calvinlogger . get_logger ( __name__ ) <EOL> class CalvinCB ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , func , * args , ** kwargs ) : <EOL> super ( CalvinCB , self ) . __init__ ( ) <EOL> self . id = calvinuuid . uuid ( "<STR_LIT>" ) <EOL> self . func = func <EOL> self . args = list ( args ) <EOL> self . kwargs = kwargs <EOL> try : <EOL> self . name = self . func . __name__ <EOL> except : <EOL> self . name = self . func . name if hasattr ( self . func , '<STR_LIT:name>' ) else "<STR_LIT>" <EOL> def args_append ( self , * args ) : <EOL> """<STR_LIT>""" <EOL> self . args . append ( * args ) <EOL> def kwargs_update ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> self . kwargs . update ( kwargs ) <EOL> def __call__ ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return self . func ( * ( self . args + list ( args ) ) , ** dict ( self . kwargs , ** kwargs ) ) <EOL> except : <EOL> _log . exception ( "<STR_LIT>" % ( <EOL> self . func , self . func . __name__ , ( self . args + list ( args ) ) , dict ( self . kwargs , ** kwargs ) ) ) <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" + self . name + "<STR_LIT>" % ( self . args , self . kwargs ) <EOL> class CalvinCBGroup ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , funcs = None ) : <EOL> super ( CalvinCBGroup , self ) . __init__ ( ) <EOL> self . id = calvinuuid . uuid ( "<STR_LIT>" ) <EOL> self . funcs = funcs if funcs else [ ] <EOL> def func_append ( self , func ) : <EOL> """<STR_LIT>""" <EOL> self . funcs . append ( func ) <EOL> def __call__ ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> reply = { } <EOL> for f in self . funcs : <EOL> reply [ f . id ] = f ( * args , ** kwargs ) <EOL> return reply <EOL> class CalvinCBClass ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , callbacks = None , callback_valid_names = None ) : <EOL> self . __callbacks = { } <EOL> self . __callback_valid_names = callback_valid_names <EOL> if not callbacks : <EOL> return <EOL> for name , cbs in callbacks . iteritems ( ) : <EOL> if self . __callback_valid_names is None or name in self . __callback_valid_names : <EOL> self . __callbacks [ name ] = dict ( [ ( cb . id , cb ) for cb in cbs ] ) <EOL> def callback_valid_names ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __callback_valid_names if self . __callback_valid_names else self . __callbacks . keys ( ) <EOL> def callback_register ( self , name , cb ) : <EOL> """<STR_LIT>""" <EOL> if self . __callback_valid_names is None or name in self . __callback_valid_names : <EOL> if name not in self . __callbacks : <EOL> self . __callbacks [ name ] = { } <EOL> self . __callbacks [ name ] [ cb . id ] = cb <EOL> def callback_unregister ( self , _id ) : <EOL> """<STR_LIT>""" <EOL> for k , v in self . __callbacks . iteritems ( ) : <EOL> if _id in v : <EOL> self . __callbacks [ k ] . pop ( _id ) <EOL> if not self . __callbacks [ k ] : <EOL> del self . __callbacks [ k ] <EOL> break <EOL> def _callback_execute ( self , name , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> reply = { } <EOL> if name not in self . __callbacks : <EOL> _log . debug ( "<STR_LIT>" % name ) <EOL> return reply <EOL> local_copy = self . __callbacks [ name ] . copy ( ) <EOL> for cb in local_copy . itervalues ( ) : <EOL> try : <EOL> reply [ cb . id ] = cb ( * args , ** kwargs ) <EOL> except : <EOL> _log . exception ( "<STR_LIT>" % ( name , cb , args , kwargs ) ) <EOL> return reply <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> def fname ( arg1 , arg2 , kwarg1 ) : <EOL> """<STR_LIT>""" <EOL> print "<STR_LIT>" , arg1 , "<STR_LIT:/>" , arg2 , "<STR_LIT:/>" , kwarg1 <EOL> return True <EOL> def fname2 ( arg2 , kwarg1 ) : <EOL> """<STR_LIT>""" <EOL> print "<STR_LIT>" , arg2 , "<STR_LIT:/>" , kwarg1 <EOL> return True <EOL> def fname3 ( arg2 , kwarg1 ) : <EOL> """<STR_LIT>""" <EOL> raise Exception ( "<STR_LIT>" ) <EOL> return True <EOL> a = CalvinCB ( fname , <NUM_LIT:1> ) <EOL> a ( <NUM_LIT:10> , kwarg1 = <NUM_LIT:2> ) <EOL> a ( <NUM_LIT:20> , kwarg1 = <NUM_LIT:3> ) <EOL> print "<STR_LIT>" <EOL> b = CalvinCBGroup ( [ CalvinCB ( fname , <NUM_LIT> ) ] ) <EOL> b . func_append ( CalvinCB ( fname , <NUM_LIT:2> ) ) <EOL> b . func_append ( CalvinCB ( fname3 ) ) <EOL> b . func_append ( CalvinCB ( fname2 ) ) <EOL> b ( <NUM_LIT:50> , kwarg1 = <NUM_LIT:10> ) <EOL> class TestingCB ( CalvinCBClass ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , arg , callbacks = None ) : <EOL> super ( TestingCB , self ) . __init__ ( callbacks , [ "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> self . arg = arg <EOL> def internal ( self ) : <EOL> """<STR_LIT>""" <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" , self . _callback_execute ( "<STR_LIT>" , <NUM_LIT:100> , kwarg1 = <NUM_LIT:12> ) <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" , self . _callback_execute ( "<STR_LIT>" , <NUM_LIT> , kwarg1 = <NUM_LIT> ) <EOL> t = TestingCB ( <NUM_LIT:1> , callbacks = { '<STR_LIT>' : [ a ] , '<STR_LIT>' : [ b ] } ) <EOL> t . callback_register ( '<STR_LIT>' , CalvinCB ( fname2 ) ) <EOL> t . callback_unregister ( a . id ) <EOL> print t . callback_valid_names ( ) <EOL> t . internal ( ) </s>
