<s> """<STR_LIT>""" <EOL> from __future__ import division , absolute_import <EOL> __metaclass__ = type <EOL> import sys <EOL> import time <EOL> import warnings <EOL> from zope . interface import implementer <EOL> from twisted . python import log <EOL> from twisted . python import reflect <EOL> from twisted . python . deprecate import _getDeprecationWarningString <EOL> from twisted . python . failure import Failure <EOL> from twisted . python . versions import Version <EOL> from twisted . internet import base , defer <EOL> from twisted . internet . interfaces import IReactorTime <EOL> from twisted . internet . error import ReactorNotRunning <EOL> class LoopingCall : <EOL> """<STR_LIT>""" <EOL> call = None <EOL> running = False <EOL> _deferred = None <EOL> interval = None <EOL> _runAtStart = False <EOL> starttime = None <EOL> def __init__ ( self , f , * a , ** kw ) : <EOL> self . f = f <EOL> self . a = a <EOL> self . kw = kw <EOL> from twisted . internet import reactor <EOL> self . clock = reactor <EOL> @ property <EOL> def deferred ( self ) : <EOL> """<STR_LIT>""" <EOL> warningString = _getDeprecationWarningString ( <EOL> "<STR_LIT>" , <EOL> Version ( "<STR_LIT>" , <NUM_LIT:16> , <NUM_LIT:0> , <NUM_LIT:0> ) , <EOL> replacement = '<STR_LIT>' ) <EOL> warnings . warn ( warningString , DeprecationWarning , stacklevel = <NUM_LIT:2> ) <EOL> return self . _deferred <EOL> def withCount ( cls , countCallable ) : <EOL> """<STR_LIT>""" <EOL> def counter ( ) : <EOL> now = self . clock . seconds ( ) <EOL> if self . interval == <NUM_LIT:0> : <EOL> self . _realLastTime = now <EOL> return countCallable ( <NUM_LIT:1> ) <EOL> lastTime = self . _realLastTime <EOL> if lastTime is None : <EOL> lastTime = self . starttime <EOL> if self . _runAtStart : <EOL> lastTime -= self . interval <EOL> lastInterval = self . _intervalOf ( lastTime ) <EOL> thisInterval = self . _intervalOf ( now ) <EOL> count = thisInterval - lastInterval <EOL> if count > <NUM_LIT:0> : <EOL> self . _realLastTime = now <EOL> return countCallable ( count ) <EOL> self = cls ( counter ) <EOL> self . _realLastTime = None <EOL> return self <EOL> withCount = classmethod ( withCount ) <EOL> def _intervalOf ( self , t ) : <EOL> """<STR_LIT>""" <EOL> elapsedTime = t - self . starttime <EOL> intervalNum = int ( elapsedTime / self . interval ) <EOL> return intervalNum <EOL> def start ( self , interval , now = True ) : <EOL> """<STR_LIT>""" <EOL> assert not self . running , ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if interval < <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> self . running = True <EOL> deferred = self . _deferred = defer . Deferred ( ) <EOL> self . starttime = self . clock . seconds ( ) <EOL> self . interval = interval <EOL> self . _runAtStart = now <EOL> if now : <EOL> self ( ) <EOL> else : <EOL> self . _scheduleFrom ( self . starttime ) <EOL> return deferred <EOL> def stop ( self ) : <EOL> """<STR_LIT>""" <EOL> assert self . running , ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> self . running = False <EOL> if self . call is not None : <EOL> self . call . cancel ( ) <EOL> self . call = None <EOL> d , self . _deferred = self . _deferred , None <EOL> d . callback ( self ) <EOL> def reset ( self ) : <EOL> """<STR_LIT>""" <EOL> assert self . running , ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if self . call is not None : <EOL> self . call . cancel ( ) <EOL> self . call = None <EOL> self . starttime = self . clock . seconds ( ) <EOL> self . _scheduleFrom ( self . starttime ) <EOL> def __call__ ( self ) : <EOL> def cb ( result ) : <EOL> if self . running : <EOL> self . _scheduleFrom ( self . clock . seconds ( ) ) <EOL> else : <EOL> d , self . _deferred = self . _deferred , None <EOL> d . callback ( self ) <EOL> def eb ( failure ) : <EOL> self . running = False <EOL> d , self . _deferred = self . _deferred , None <EOL> d . errback ( failure ) <EOL> self . call = None <EOL> d = defer . maybeDeferred ( self . f , * self . a , ** self . kw ) <EOL> d . addCallback ( cb ) <EOL> d . addErrback ( eb ) <EOL> def _scheduleFrom ( self , when ) : <EOL> """<STR_LIT>""" <EOL> def howLong ( ) : <EOL> if self . interval == <NUM_LIT:0> : <EOL> return <NUM_LIT:0> <EOL> runningFor = when - self . starttime <EOL> untilNextInterval = self . interval - ( runningFor % self . interval ) <EOL> if when == when + untilNextInterval : <EOL> return self . interval <EOL> return untilNextInterval <EOL> self . call = self . clock . callLater ( howLong ( ) , self ) <EOL> def __repr__ ( self ) : <EOL> if hasattr ( self . f , '<STR_LIT>' ) : <EOL> func = self . f . __qualname__ <EOL> elif hasattr ( self . f , '<STR_LIT>' ) : <EOL> func = self . f . __name__ <EOL> if hasattr ( self . f , '<STR_LIT>' ) : <EOL> func = self . f . im_class . __name__ + '<STR_LIT:.>' + func <EOL> else : <EOL> func = reflect . safe_repr ( self . f ) <EOL> return '<STR_LIT>' % ( <EOL> self . interval , func , reflect . safe_repr ( self . a ) , <EOL> reflect . safe_repr ( self . kw ) ) <EOL> class SchedulerError ( Exception ) : <EOL> """<STR_LIT>""" <EOL> class SchedulerStopped ( SchedulerError ) : <EOL> """<STR_LIT>""" <EOL> class TaskFinished ( SchedulerError ) : <EOL> """<STR_LIT>""" <EOL> class TaskDone ( TaskFinished ) : <EOL> """<STR_LIT>""" <EOL> class TaskStopped ( TaskFinished ) : <EOL> """<STR_LIT>""" <EOL> class TaskFailed ( TaskFinished ) : <EOL> """<STR_LIT>""" <EOL> class NotPaused ( SchedulerError ) : <EOL> """<STR_LIT>""" <EOL> class _Timer ( object ) : <EOL> MAX_SLICE = <NUM_LIT> <EOL> def __init__ ( self ) : <EOL> self . end = time . time ( ) + self . MAX_SLICE <EOL> def __call__ ( self ) : <EOL> return time . time ( ) >= self . end <EOL> _EPSILON = <NUM_LIT> <EOL> def _defaultScheduler ( x ) : <EOL> from twisted . internet import reactor <EOL> return reactor . callLater ( _EPSILON , x ) <EOL> class CooperativeTask ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , iterator , cooperator ) : <EOL> """<STR_LIT>""" <EOL> self . _iterator = iterator <EOL> self . _cooperator = cooperator <EOL> self . _deferreds = [ ] <EOL> self . _pauseCount = <NUM_LIT:0> <EOL> self . _completionState = None <EOL> self . _completionResult = None <EOL> cooperator . _addTask ( self ) <EOL> def whenDone ( self ) : <EOL> """<STR_LIT>""" <EOL> d = defer . Deferred ( ) <EOL> if self . _completionState is None : <EOL> self . _deferreds . append ( d ) <EOL> else : <EOL> d . callback ( self . _completionResult ) <EOL> return d <EOL> def pause ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _checkFinish ( ) <EOL> self . _pauseCount += <NUM_LIT:1> <EOL> if self . _pauseCount == <NUM_LIT:1> : <EOL> self . _cooperator . _removeTask ( self ) <EOL> def resume ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _pauseCount == <NUM_LIT:0> : <EOL> raise NotPaused ( ) <EOL> self . _pauseCount -= <NUM_LIT:1> <EOL> if self . _pauseCount == <NUM_LIT:0> and self . _completionState is None : <EOL> self . _cooperator . _addTask ( self ) <EOL> def _completeWith ( self , completionState , deferredResult ) : <EOL> """<STR_LIT>""" <EOL> self . _completionState = completionState <EOL> self . _completionResult = deferredResult <EOL> if not self . _pauseCount : <EOL> self . _cooperator . _removeTask ( self ) <EOL> for d in self . _deferreds : <EOL> d . callback ( deferredResult ) <EOL> def stop ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _checkFinish ( ) <EOL> self . _completeWith ( TaskStopped ( ) , Failure ( TaskStopped ( ) ) ) <EOL> def _checkFinish ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _completionState is not None : <EOL> raise self . _completionState <EOL> def _oneWorkUnit ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> result = next ( self . _iterator ) <EOL> except StopIteration : <EOL> self . _completeWith ( TaskDone ( ) , self . _iterator ) <EOL> except : <EOL> self . _completeWith ( TaskFailed ( ) , Failure ( ) ) <EOL> else : <EOL> if isinstance ( result , defer . Deferred ) : <EOL> self . pause ( ) <EOL> def failLater ( f ) : <EOL> self . _completeWith ( TaskFailed ( ) , f ) <EOL> result . addCallbacks ( lambda result : self . resume ( ) , <EOL> failLater ) <EOL> class Cooperator ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , <EOL> terminationPredicateFactory = _Timer , <EOL> scheduler = _defaultScheduler , <EOL> started = True ) : <EOL> """<STR_LIT>""" <EOL> self . _tasks = [ ] <EOL> self . _metarator = iter ( ( ) ) <EOL> self . _terminationPredicateFactory = terminationPredicateFactory <EOL> self . _scheduler = scheduler <EOL> self . _delayedCall = None <EOL> self . _stopped = False <EOL> self . _started = started <EOL> def coiterate ( self , iterator , doneDeferred = None ) : <EOL> """<STR_LIT>""" <EOL> if doneDeferred is None : <EOL> doneDeferred = defer . Deferred ( ) <EOL> CooperativeTask ( iterator , self ) . whenDone ( ) . chainDeferred ( doneDeferred ) <EOL> return doneDeferred <EOL> def cooperate ( self , iterator ) : <EOL> """<STR_LIT>""" <EOL> return CooperativeTask ( iterator , self ) <EOL> def _addTask ( self , task ) : <EOL> """<STR_LIT>""" <EOL> if self . _stopped : <EOL> self . _tasks . append ( task ) <EOL> task . _completeWith ( SchedulerStopped ( ) , Failure ( SchedulerStopped ( ) ) ) <EOL> else : <EOL> self . _tasks . append ( task ) <EOL> self . _reschedule ( ) <EOL> def _removeTask ( self , task ) : <EOL> """<STR_LIT>""" <EOL> self . _tasks . remove ( task ) <EOL> if not self . _tasks and self . _delayedCall : <EOL> self . _delayedCall . cancel ( ) <EOL> self . _delayedCall = None <EOL> def _tasksWhileNotStopped ( self ) : <EOL> """<STR_LIT>""" <EOL> terminator = self . _terminationPredicateFactory ( ) <EOL> while self . _tasks : <EOL> for t in self . _metarator : <EOL> yield t <EOL> if terminator ( ) : <EOL> return <EOL> self . _metarator = iter ( self . _tasks ) <EOL> def _tick ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _delayedCall = None <EOL> for taskObj in self . _tasksWhileNotStopped ( ) : <EOL> taskObj . _oneWorkUnit ( ) <EOL> self . _reschedule ( ) <EOL> _mustScheduleOnStart = False <EOL> def _reschedule ( self ) : <EOL> if not self . _started : <EOL> self . _mustScheduleOnStart = True <EOL> return <EOL> if self . _delayedCall is None and self . _tasks : <EOL> self . _delayedCall = self . _scheduler ( self . _tick ) <EOL> def start ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _stopped = False <EOL> self . _started = True <EOL> if self . _mustScheduleOnStart : <EOL> del self . _mustScheduleOnStart <EOL> self . _reschedule ( ) <EOL> def stop ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _stopped = True <EOL> for taskObj in self . _tasks : <EOL> taskObj . _completeWith ( SchedulerStopped ( ) , <EOL> Failure ( SchedulerStopped ( ) ) ) <EOL> self . _tasks = [ ] <EOL> if self . _delayedCall is not None : <EOL> self . _delayedCall . cancel ( ) <EOL> self . _delayedCall = None <EOL> @ property <EOL> def running ( self ) : <EOL> """<STR_LIT>""" <EOL> return ( self . _started and not self . _stopped ) <EOL> _theCooperator = Cooperator ( ) <EOL> def coiterate ( iterator ) : <EOL> """<STR_LIT>""" <EOL> return _theCooperator . coiterate ( iterator ) <EOL> def cooperate ( iterator ) : <EOL> """<STR_LIT>""" <EOL> return _theCooperator . cooperate ( iterator ) <EOL> @ implementer ( IReactorTime ) <EOL> class Clock : <EOL> """<STR_LIT>""" <EOL> rightNow = <NUM_LIT:0.0> <EOL> def __init__ ( self ) : <EOL> self . calls = [ ] <EOL> def seconds ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . rightNow <EOL> def _sortCalls ( self ) : <EOL> """<STR_LIT>""" <EOL> self . calls . sort ( key = lambda a : a . getTime ( ) ) <EOL> def callLater ( self , when , what , * a , ** kw ) : <EOL> """<STR_LIT>""" <EOL> dc = base . DelayedCall ( self . seconds ( ) + when , <EOL> what , a , kw , <EOL> self . calls . remove , <EOL> lambda c : None , <EOL> self . seconds ) <EOL> self . calls . append ( dc ) <EOL> self . _sortCalls ( ) <EOL> return dc <EOL> def getDelayedCalls ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . calls <EOL> def advance ( self , amount ) : <EOL> """<STR_LIT>""" <EOL> self . rightNow += amount <EOL> self . _sortCalls ( ) <EOL> while self . calls and self . calls [ <NUM_LIT:0> ] . getTime ( ) <= self . seconds ( ) : <EOL> call = self . calls . pop ( <NUM_LIT:0> ) <EOL> call . called = <NUM_LIT:1> <EOL> call . func ( * call . args , ** call . kw ) <EOL> self . _sortCalls ( ) <EOL> def pump ( self , timings ) : <EOL> """<STR_LIT>""" <EOL> for amount in timings : <EOL> self . advance ( amount ) <EOL> def deferLater ( clock , delay , callable , * args , ** kw ) : <EOL> """<STR_LIT>""" <EOL> def deferLaterCancel ( deferred ) : <EOL> delayedCall . cancel ( ) <EOL> d = defer . Deferred ( deferLaterCancel ) <EOL> d . addCallback ( lambda ignored : callable ( * args , ** kw ) ) <EOL> delayedCall = clock . callLater ( delay , d . callback , None ) <EOL> return d <EOL> def react ( main , argv = ( ) , _reactor = None ) : <EOL> """<STR_LIT>""" <EOL> if _reactor is None : <EOL> from twisted . internet import reactor as _reactor <EOL> finished = main ( _reactor , * argv ) <EOL> codes = [ <NUM_LIT:0> ] <EOL> stopping = [ ] <EOL> _reactor . addSystemEventTrigger ( '<STR_LIT>' , '<STR_LIT>' , stopping . append , True ) <EOL> def stop ( result , stopReactor ) : <EOL> if stopReactor : <EOL> try : <EOL> _reactor . stop ( ) <EOL> except ReactorNotRunning : <EOL> pass <EOL> if isinstance ( result , Failure ) : <EOL> if result . check ( SystemExit ) is not None : <EOL> code = result . value . code <EOL> else : <EOL> log . err ( result , "<STR_LIT>" ) <EOL> code = <NUM_LIT:1> <EOL> codes [ <NUM_LIT:0> ] = code <EOL> def cbFinish ( result ) : <EOL> if stopping : <EOL> stop ( result , False ) <EOL> else : <EOL> _reactor . callWhenRunning ( stop , result , True ) <EOL> finished . addBoth ( cbFinish ) <EOL> _reactor . run ( ) <EOL> sys . exit ( codes [ <NUM_LIT:0> ] ) <EOL> __all__ = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] </s>
