<s> from __future__ import print_function <EOL> import abc <EOL> from gevent import getcurrent , spawn_later <EOL> from spinoff . actor . events import Events , DeadLetter <EOL> from spinoff . actor . uri import Uri <EOL> from spinoff . actor . misc import TempActor <EOL> from spinoff . actor . context import get_context , _get_cell <EOL> from spinoff . util . pattern_matching import ANY , IN , Matcher <EOL> from spinoff . util . logging import dbg <EOL> class _BaseRef ( object ) : <EOL> """<STR_LIT>""" <EOL> __metaclass__ = abc . ABCMeta <EOL> @ abc . abstractproperty <EOL> def is_local ( self ) : <EOL> raise NotImplementedError <EOL> @ abc . abstractproperty <EOL> def is_stopped ( self ) : <EOL> raise NotImplementedError <EOL> @ abc . abstractproperty <EOL> def uri ( self ) : <EOL> raise NotImplementedError <EOL> def __div__ ( self , next ) : <EOL> """<STR_LIT>""" <EOL> if self . is_local and self . _cell : <EOL> return self . _cell . lookup_ref ( next ) <EOL> else : <EOL> return Ref ( cell = None , uri = self . uri / next , is_local = self . is_local , <EOL> node = None if self . is_local else self . node ) <EOL> def __lshift__ ( self , message ) : <EOL> """<STR_LIT>""" <EOL> self . send ( message ) <EOL> return self <EOL> def ask ( self , msg , timeout = None ) : <EOL> tmp , d = TempActor . make ( ) <EOL> self . send ( msg , _sender = tmp ) <EOL> return d . get ( timeout = timeout ) <EOL> def forward ( self , msg ) : <EOL> sender = get_context ( ) . sender <EOL> assert sender <EOL> self . send ( msg , _sender = sender ) <EOL> def send_later ( self , delay , message , _sender = None ) : <EOL> spawn_later ( delay , self . send , message , _sender = _sender or get_context ( ) . ref ) <EOL> def stop ( self ) : <EOL> """<STR_LIT>""" <EOL> self . send ( '<STR_LIT>' ) <EOL> def kill ( self ) : <EOL> """<STR_LIT>""" <EOL> self . send ( '<STR_LIT>' ) <EOL> class Ref ( _BaseRef ) : <EOL> """<STR_LIT>""" <EOL> _cell = None <EOL> is_local = True <EOL> uri = None <EOL> node = None <EOL> def __init__ ( self , cell , uri , node , is_local = True ) : <EOL> assert is_local or not cell <EOL> assert uri is None or isinstance ( uri , Uri ) <EOL> self . _cell = cell <EOL> self . uri = uri <EOL> self . node = node <EOL> self . is_local = is_local <EOL> def send ( self , message , _sender = None ) : <EOL> """<STR_LIT>""" <EOL> if not _sender : <EOL> context = get_context ( ) <EOL> if context : <EOL> _sender = context . ref <EOL> if self . _cell : <EOL> if not self . _cell . stopped : <EOL> self . _cell . receive ( message , _sender ) <EOL> return <EOL> else : <EOL> self . _cell = None <EOL> if not self . is_local : <EOL> if self . uri . node != self . node . nid : <EOL> self . node . send_message ( message , remote_ref = self , sender = _sender ) <EOL> else : <EOL> self . _cell = self . node . guardian . lookup_cell ( self . uri ) <EOL> self . is_local = True <EOL> self . _cell . receive ( message , _sender ) <EOL> else : <EOL> if self . node and self . node . guardian : <EOL> cell = self . node . guardian . lookup_cell ( self . uri ) <EOL> if cell : <EOL> cell . receive ( message , _sender ) <EOL> return <EOL> if ( '<STR_LIT>' , ANY ) == message : <EOL> message [ <NUM_LIT:1> ] . send ( ( '<STR_LIT>' , self ) , _sender = self ) <EOL> elif ( message == ( '<STR_LIT>' , ANY ) or message == ( '<STR_LIT>' , ANY ) or message == ( '<STR_LIT>' , ANY ) or <EOL> message == '<STR_LIT>' or message == '<STR_LIT>' or message == '<STR_LIT>' ) : <EOL> pass <EOL> else : <EOL> Events . log ( DeadLetter ( self , message , _sender ) ) <EOL> @ property <EOL> def is_stopped ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . is_local and not self . _cell <EOL> def join ( self ) : <EOL> cell = _get_cell ( ) <EOL> cell . watch ( self ) <EOL> cell . get ( ( '<STR_LIT>' , self ) ) <EOL> def __eq__ ( self , other ) : <EOL> """<STR_LIT>""" <EOL> return ( isinstance ( other , Ref ) and self . uri == other . uri <EOL> or isinstance ( other , Matcher ) and other == self ) <EOL> def __ne__ ( self , other ) : <EOL> return not ( self == other ) <EOL> def __hash__ ( self ) : <EOL> return hash ( self . uri ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( str ( self . uri ) , ) <EOL> def __getstate__ ( self ) : <EOL> return str ( self . uri ) <EOL> def __setstate__ ( self , uri ) : <EOL> if isinstance ( uri , tuple ) : <EOL> self . is_local = False <EOL> uri , self . node = uri <EOL> self . uri = Uri . parse ( uri ) </s>
