<s> from rx . disposables import Disposable , SingleAssignmentDisposable <EOL> from . scheduler import Scheduler <EOL> class CatchScheduler ( Scheduler ) : <EOL> def __init__ ( self , scheduler , handler ) : <EOL> self . _scheduler = scheduler <EOL> self . _handler = handler <EOL> self . _recursive_original = None <EOL> self . _recursive_wrapper = None <EOL> super ( CatchScheduler , self ) . __init__ ( ) <EOL> def local_now ( self ) : <EOL> return self . _scheduler . now ( ) <EOL> def schedule_now ( self , state , action ) : <EOL> """<STR_LIT>""" <EOL> return self . _scheduler . scheduleWithState ( state , self . _wrap ( action ) ) <EOL> def schedule_relative ( self , duetime , action , state = None ) : <EOL> """<STR_LIT>""" <EOL> return self . _scheduler . schedule_relative ( duetime , self . _wrap ( action ) , <EOL> state = state ) <EOL> def schedule_absolute ( self , duetime , action , state = None ) : <EOL> """<STR_LIT>""" <EOL> return self . _scheduler . schedule_absolute ( duetime , self . _wrap ( action ) , <EOL> state = state ) <EOL> def _clone ( self , scheduler ) : <EOL> return CatchScheduler ( scheduler , self . _handler ) <EOL> def _wrap ( self , action ) : <EOL> parent = self <EOL> def wrapped_action ( self , state ) : <EOL> try : <EOL> return action ( parent . _get_recursive_wrapper ( self ) , state ) <EOL> except Exception as ex : <EOL> if not parent . _handler ( ex ) : <EOL> raise Exception ( ex ) <EOL> return Disposable . empty ( ) <EOL> return wrapped_action <EOL> def _get_recursive_wrapper ( self , scheduler ) : <EOL> if self . _recursive_original != scheduler : <EOL> self . _recursive_original = scheduler <EOL> wrapper = self . _clone ( scheduler ) <EOL> wrapper . _recursive_original = scheduler <EOL> wrapper . _recursive_wrapper = wrapper <EOL> self . _recursive_wrapper = wrapper <EOL> return self . _recursive_wrapper <EOL> def schedule_periodic ( self , period , action , state = None ) : <EOL> d = SingleAssignmentDisposable ( ) <EOL> failed = [ False ] <EOL> def periodic_action ( periodic_state ) : <EOL> if failed [ <NUM_LIT:0> ] : <EOL> return None <EOL> try : <EOL> return action ( periodic_state ) <EOL> except Exception as ex : <EOL> failed [ <NUM_LIT:0> ] = True <EOL> if not self . _handler ( ex ) : <EOL> raise Exception ( ex ) <EOL> d . dispose ( ) <EOL> return None <EOL> d . disposable = self . _scheduler . schedule_periodic ( periodic_action , <EOL> period , state ) <EOL> return d </s>
