<s> import types <EOL> import traceback <EOL> from PythonQt import QtCore , QtGui <EOL> from director . timercallback import TimerCallback <EOL> from director . simpletimer import SimpleTimer <EOL> from director import callbacks <EOL> class AsyncTaskQueue ( object ) : <EOL> QUEUE_STARTED_SIGNAL = '<STR_LIT>' <EOL> QUEUE_STOPPED_SIGNAL = '<STR_LIT>' <EOL> TASK_STARTED_SIGNAL = '<STR_LIT>' <EOL> TASK_ENDED_SIGNAL = '<STR_LIT>' <EOL> TASK_PAUSED_SIGNAL = '<STR_LIT>' <EOL> TASK_FAILED_SIGNAL = '<STR_LIT>' <EOL> TASK_EXCEPTION_SIGNAL = '<STR_LIT>' <EOL> class PauseException ( Exception ) : <EOL> pass <EOL> class FailException ( Exception ) : <EOL> pass <EOL> def __init__ ( self ) : <EOL> self . tasks = [ ] <EOL> self . generators = [ ] <EOL> self . timer = TimerCallback ( targetFps = <NUM_LIT:10> ) <EOL> self . timer . callback = self . callbackLoop <EOL> self . callbacks = callbacks . CallbackRegistry ( [ self . QUEUE_STARTED_SIGNAL , <EOL> self . QUEUE_STOPPED_SIGNAL , <EOL> self . TASK_STARTED_SIGNAL , <EOL> self . TASK_ENDED_SIGNAL , <EOL> self . TASK_PAUSED_SIGNAL , <EOL> self . TASK_FAILED_SIGNAL , <EOL> self . TASK_EXCEPTION_SIGNAL ] ) <EOL> self . currentTask = None <EOL> self . isRunning = False <EOL> def reset ( self ) : <EOL> assert not self . isRunning <EOL> assert not self . generators <EOL> self . tasks = [ ] <EOL> def start ( self ) : <EOL> self . isRunning = True <EOL> self . callbacks . process ( self . QUEUE_STARTED_SIGNAL , self ) <EOL> self . timer . start ( ) <EOL> def stop ( self ) : <EOL> self . isRunning = False <EOL> self . currentTask = None <EOL> self . generators = [ ] <EOL> self . timer . stop ( ) <EOL> self . callbacks . process ( self . QUEUE_STOPPED_SIGNAL , self ) <EOL> def wrapGenerator ( self , generator ) : <EOL> def generatorWrapper ( ) : <EOL> return generator <EOL> return generatorWrapper <EOL> def addTask ( self , task ) : <EOL> if isinstance ( task , types . GeneratorType ) : <EOL> task = self . wrapGenerator ( task ) <EOL> assert hasattr ( task , '<STR_LIT>' ) <EOL> self . tasks . append ( task ) <EOL> def callbackLoop ( self ) : <EOL> try : <EOL> for i in xrange ( <NUM_LIT:10> ) : <EOL> self . doWork ( ) <EOL> if not self . tasks : <EOL> self . stop ( ) <EOL> except AsyncTaskQueue . PauseException : <EOL> assert self . currentTask <EOL> self . callbacks . process ( self . TASK_PAUSED_SIGNAL , self , self . currentTask ) <EOL> self . stop ( ) <EOL> except AsyncTaskQueue . FailException : <EOL> assert self . currentTask <EOL> self . callbacks . process ( self . TASK_FAILED_SIGNAL , self , self . currentTask ) <EOL> self . stop ( ) <EOL> except : <EOL> assert self . currentTask <EOL> self . callbacks . process ( self . TASK_EXCEPTION_SIGNAL , self , self . currentTask ) <EOL> self . stop ( ) <EOL> raise <EOL> return self . isRunning <EOL> def popTask ( self ) : <EOL> assert not self . isRunning <EOL> assert not self . currentTask <EOL> if self . tasks : <EOL> self . tasks . pop ( <NUM_LIT:0> ) <EOL> def completePreviousTask ( self ) : <EOL> assert self . currentTask <EOL> self . tasks . remove ( self . currentTask ) <EOL> self . callbacks . process ( self . TASK_ENDED_SIGNAL , self , self . currentTask ) <EOL> self . currentTask = None <EOL> def startNextTask ( self ) : <EOL> self . currentTask = self . tasks [ <NUM_LIT:0> ] <EOL> self . callbacks . process ( self . TASK_STARTED_SIGNAL , self , self . currentTask ) <EOL> result = self . currentTask ( ) <EOL> if isinstance ( result , types . GeneratorType ) : <EOL> self . generators . insert ( <NUM_LIT:0> , result ) <EOL> def doWork ( self ) : <EOL> if self . generators : <EOL> self . handleGenerator ( self . generators [ <NUM_LIT:0> ] ) <EOL> else : <EOL> if self . currentTask : <EOL> self . completePreviousTask ( ) <EOL> if self . tasks : <EOL> self . startNextTask ( ) <EOL> def handleGenerator ( self , generator ) : <EOL> try : <EOL> result = generator . next ( ) <EOL> except StopIteration : <EOL> self . generators . remove ( generator ) <EOL> else : <EOL> if isinstance ( result , types . GeneratorType ) : <EOL> self . generators . insert ( <NUM_LIT:0> , result ) <EOL> def connectQueueStarted ( self , func ) : <EOL> return self . callbacks . connect ( self . QUEUE_STARTED_SIGNAL , func ) <EOL> def disconnectQueueStarted ( self , callbackId ) : <EOL> self . callbacks . disconnect ( callbackId ) <EOL> def connectQueueStopped ( self , func ) : <EOL> return self . callbacks . connect ( self . QUEUE_STOPPED_SIGNAL , func ) <EOL> def disconnectQueueStopped ( self , callbackId ) : <EOL> self . callbacks . disconnect ( callbackId ) <EOL> def connectTaskStarted ( self , func ) : <EOL> return self . callbacks . connect ( self . TASK_STARTED_SIGNAL , func ) <EOL> def disconnectTaskStarted ( self , callbackId ) : <EOL> self . callbacks . disconnect ( callbackId ) <EOL> def connectTaskEnded ( self , func ) : <EOL> return self . callbacks . connect ( self . TASK_ENDED_SIGNAL , func ) <EOL> def disconnectTaskEnded ( self , callbackId ) : <EOL> self . callbacks . disconnect ( callbackId ) <EOL> def connectTaskPaused ( self , func ) : <EOL> return self . callbacks . connect ( self . TASK_PAUSED_SIGNAL , func ) <EOL> def disconnectTaskPaused ( self , callbackId ) : <EOL> self . callbacks . disconnect ( callbackId ) <EOL> def connectTaskFailed ( self , func ) : <EOL> return self . callbacks . connect ( self . TASK_FAILED_SIGNAL , func ) <EOL> def disconnectTaskFailed ( self , callbackId ) : <EOL> self . callbacks . disconnect ( callbackId ) <EOL> def connectTaskException ( self , func ) : <EOL> return self . callbacks . connect ( self . TASK_EXCEPTION_SIGNAL , func ) <EOL> def disconnectTaskException ( self , callbackId ) : <EOL> self . callbacks . disconnect ( callbackId ) <EOL> class AsyncTask ( object ) : <EOL> def __init__ ( self ) : <EOL> pass <EOL> def getStatus ( self ) : <EOL> return '<STR_LIT:none>' <EOL> def __call__ ( self ) : <EOL> pass <EOL> class PrintTask ( AsyncTask ) : <EOL> def __init__ ( self , message ) : <EOL> self . message = message <EOL> def __call__ ( self ) : <EOL> print self . message <EOL> class UserPromptTask ( AsyncTask ) : <EOL> promptsEnabled = True <EOL> def __init__ ( self , message , force = False , testingValue = None ) : <EOL> self . message = message <EOL> self . force = force <EOL> self . testingValue = testingValue <EOL> def showDialog ( self ) : <EOL> self . d = QtGui . QDialog ( ) <EOL> buttons = QtGui . QDialogButtonBox ( ) <EOL> buttons . addButton ( '<STR_LIT>' , QtGui . QDialogButtonBox . AcceptRole ) <EOL> buttons . addButton ( '<STR_LIT>' , QtGui . QDialogButtonBox . RejectRole ) <EOL> buttons . connect ( '<STR_LIT>' , self . d . accept ) <EOL> buttons . connect ( '<STR_LIT>' , self . d . reject ) <EOL> l = QtGui . QVBoxLayout ( self . d ) <EOL> l . addWidget ( QtGui . QLabel ( self . message ) ) <EOL> l . addWidget ( buttons ) <EOL> self . d . setAttribute ( QtCore . Qt . WA_QuitOnClose , False ) <EOL> self . d . show ( ) <EOL> self . d . raise_ ( ) <EOL> self . d . connect ( '<STR_LIT>' , self . onYes ) <EOL> self . d . connect ( '<STR_LIT>' , self . onNo ) <EOL> def onYes ( self ) : <EOL> self . result = True <EOL> def onNo ( self ) : <EOL> self . result = False <EOL> def __call__ ( self ) : <EOL> if not self . promptsEnabled and not self . force : <EOL> return <EOL> self . showDialog ( ) <EOL> self . result = None <EOL> if self . testingValue is not None : <EOL> self . d . close ( ) <EOL> self . result = self . testingValue <EOL> while self . result is None : <EOL> yield <EOL> if not self . result : <EOL> raise AsyncTaskQueue . PauseException ( ) <EOL> class DelayTask ( AsyncTask ) : <EOL> def __init__ ( self , delayTimeInSeconds ) : <EOL> self . delayTimeInSeconds = delayTimeInSeconds <EOL> def __call__ ( self ) : <EOL> t = SimpleTimer ( ) <EOL> while t . elapsed ( ) < self . delayTimeInSeconds : <EOL> yield <EOL> class PauseTask ( AsyncTask ) : <EOL> def __init__ ( self ) : <EOL> pass <EOL> def __call__ ( self ) : <EOL> raise AsyncTaskQueue . PauseException ( ) <EOL> class QuitTask ( AsyncTask ) : <EOL> def __init__ ( self ) : <EOL> pass <EOL> def __call__ ( self ) : <EOL> QtCore . QCoreApplication . instance ( ) . quit ( ) </s>
