<s> import weakref <EOL> from dispatch import saferef <EOL> WEAKREF_TYPES = ( weakref . ReferenceType , saferef . BoundMethodWeakref ) <EOL> def _make_id ( target ) : <EOL> if hasattr ( target , '<STR_LIT>' ) : <EOL> return ( id ( target . im_self ) , id ( target . im_func ) ) <EOL> return id ( target ) <EOL> class Signal ( object ) : <EOL> """<STR_LIT>""" <EOL> _debugging = False <EOL> def __init__ ( self , providing_args = None ) : <EOL> """<STR_LIT>""" <EOL> self . receivers = [ ] <EOL> if providing_args is None : <EOL> providing_args = [ ] <EOL> self . providing_args = set ( providing_args ) <EOL> def connect ( self , receiver , sender = None , weak = True , dispatch_uid = None ) : <EOL> """<STR_LIT>""" <EOL> if self . _debugging : <EOL> import inspect <EOL> assert callable ( receiver ) , "<STR_LIT>" <EOL> try : <EOL> argspec = inspect . getargspec ( receiver ) <EOL> except TypeError : <EOL> try : <EOL> argspec = inspect . getargspec ( receiver . __call__ ) <EOL> except ( TypeError , AttributeError ) : <EOL> argspec = None <EOL> if argspec : <EOL> assert argspec [ <NUM_LIT:2> ] is not None , "<STR_LIT>" <EOL> if dispatch_uid : <EOL> lookup_key = ( dispatch_uid , _make_id ( sender ) ) <EOL> else : <EOL> lookup_key = ( _make_id ( receiver ) , _make_id ( sender ) ) <EOL> if weak : <EOL> receiver = saferef . safeRef ( receiver , onDelete = self . _remove_receiver ) <EOL> for r_key , _ in self . receivers : <EOL> if r_key == lookup_key : <EOL> break <EOL> else : <EOL> self . receivers . append ( ( lookup_key , receiver ) ) <EOL> def disconnect ( self , receiver = None , sender = None , weak = True , dispatch_uid = None ) : <EOL> """<STR_LIT>""" <EOL> if dispatch_uid : <EOL> lookup_key = ( dispatch_uid , _make_id ( sender ) ) <EOL> else : <EOL> lookup_key = ( _make_id ( receiver ) , _make_id ( sender ) ) <EOL> for index in range ( len ( self . receivers ) ) : <EOL> ( r_key , _ ) = self . receivers [ index ] <EOL> if r_key == lookup_key : <EOL> del self . receivers [ index ] <EOL> break <EOL> def send ( self , sender , ** named ) : <EOL> """<STR_LIT>""" <EOL> responses = [ ] <EOL> if not self . receivers : <EOL> return responses <EOL> for receiver in self . _live_receivers ( _make_id ( sender ) ) : <EOL> response = receiver ( signal = self , sender = sender , ** named ) <EOL> responses . append ( ( receiver , response ) ) <EOL> return responses <EOL> def send_robust ( self , sender , ** named ) : <EOL> """<STR_LIT>""" <EOL> responses = [ ] <EOL> if not self . receivers : <EOL> return responses <EOL> for receiver in self . _live_receivers ( _make_id ( sender ) ) : <EOL> try : <EOL> response = receiver ( signal = self , sender = sender , ** named ) <EOL> except Exception as err : <EOL> responses . append ( ( receiver , err ) ) <EOL> else : <EOL> responses . append ( ( receiver , response ) ) <EOL> return responses <EOL> def _live_receivers ( self , senderkey ) : <EOL> """<STR_LIT>""" <EOL> none_senderkey = _make_id ( None ) <EOL> receivers = [ ] <EOL> for ( receiverkey , r_senderkey ) , receiver in self . receivers : <EOL> if r_senderkey == none_senderkey or r_senderkey == senderkey : <EOL> if isinstance ( receiver , WEAKREF_TYPES ) : <EOL> receiver = receiver ( ) <EOL> if receiver is not None : <EOL> receivers . append ( receiver ) <EOL> else : <EOL> receivers . append ( receiver ) <EOL> return receivers <EOL> def _remove_receiver ( self , receiver ) : <EOL> """<STR_LIT>""" <EOL> to_remove = [ ] <EOL> for key , connected_receiver in self . receivers : <EOL> if connected_receiver == receiver : <EOL> to_remove . append ( key ) <EOL> for key in to_remove : <EOL> for idx , ( r_key , _ ) in enumerate ( self . receivers ) : <EOL> if r_key == key : <EOL> del self . receivers [ idx ] </s>
