<s> """<STR_LIT>""" <EOL> from twisted . internet import protocol , reactor , defer <EOL> from twisted . internet import error as tw_error <EOL> from twisted . protocols import basic <EOL> import socket <EOL> import logging <EOL> import time <EOL> from starpy import error <EOL> log = logging . getLogger ( '<STR_LIT>' ) <EOL> FAILURE_CODE = - <NUM_LIT:1> <EOL> class FastAGIProtocol ( basic . LineOnlyReceiver ) : <EOL> """<STR_LIT>""" <EOL> readingVariables = False <EOL> lostConnectionDeferred = None <EOL> delimiter = '<STR_LIT:\n>' <EOL> def __init__ ( self , * args , ** named ) : <EOL> """<STR_LIT>""" <EOL> self . messageCache = [ ] <EOL> self . variables = { } <EOL> self . pendingMessages = [ ] <EOL> def connectionMade ( self ) : <EOL> """<STR_LIT>""" <EOL> log . info ( "<STR_LIT>" ) <EOL> self . readingVariables = True <EOL> def connectionLost ( self , reason ) : <EOL> """<STR_LIT>""" <EOL> log . info ( """<STR_LIT>""" ) <EOL> try : <EOL> for df in self . pendingMessages : <EOL> df . errback ( tw_error . ConnectionDone ( <EOL> "<STR_LIT>" ) ) <EOL> finally : <EOL> if self . lostConnectionDeferred : <EOL> self . lostConnectionDeferred . errback ( reason ) <EOL> del self . pendingMessages [ : ] <EOL> def onClose ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . lostConnectionDeferred : <EOL> self . lostConnectionDeferred = defer . Deferred ( ) <EOL> return self . lostConnectionDeferred <EOL> def lineReceived ( self , line ) : <EOL> """<STR_LIT>""" <EOL> log . debug ( '<STR_LIT>' , line ) <EOL> if self . readingVariables : <EOL> if not line . strip ( ) : <EOL> self . readingVariables = False <EOL> self . factory . mainFunction ( self ) <EOL> else : <EOL> try : <EOL> key , value = line . split ( '<STR_LIT::>' , <NUM_LIT:1> ) <EOL> value = value [ <NUM_LIT:1> : ] . rstrip ( '<STR_LIT:\n>' ) . rstrip ( '<STR_LIT:\r>' ) <EOL> except ValueError , err : <EOL> log . error ( """<STR_LIT>""" , line ) <EOL> else : <EOL> self . variables [ key . lower ( ) ] = value <EOL> log . debug ( """<STR_LIT>""" , key , value ) <EOL> else : <EOL> try : <EOL> df = self . pendingMessages . pop ( <NUM_LIT:0> ) <EOL> except IndexError , err : <EOL> log . warn ( "<STR_LIT>" , line ) <EOL> else : <EOL> if line . startswith ( '<STR_LIT>' ) : <EOL> line = line [ <NUM_LIT:4> : ] <EOL> if line . lower ( ) . startswith ( '<STR_LIT>' ) : <EOL> line = line [ <NUM_LIT:7> : ] <EOL> df . callback ( line ) <EOL> else : <EOL> try : <EOL> errCode , line = line . split ( '<STR_LIT:U+0020>' , <NUM_LIT:1> ) <EOL> errCode = int ( errCode ) <EOL> except ValueError , err : <EOL> errCode = <NUM_LIT> <EOL> df . errback ( error . AGICommandFailure ( errCode , line ) ) <EOL> def sendCommand ( self , commandString ) : <EOL> """<STR_LIT>""" <EOL> log . info ( "<STR_LIT>" , commandString ) <EOL> commandString = commandString . rstrip ( '<STR_LIT:\n>' ) . rstrip ( '<STR_LIT:\r>' ) <EOL> df = defer . Deferred ( ) <EOL> self . pendingMessages . append ( df ) <EOL> self . sendLine ( commandString ) <EOL> return df <EOL> def checkFailure ( self , result , failure = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> resultInt , line = result . split ( '<STR_LIT:U+0020>' , <NUM_LIT:1> ) <EOL> except ValueError , err : <EOL> resultInt = result <EOL> if resultInt . strip ( ) == failure : <EOL> raise error . AGICommandFailure ( FAILURE_CODE , result ) <EOL> return result <EOL> def resultAsInt ( self , result ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return int ( result . strip ( ) ) <EOL> except ValueError , err : <EOL> raise error . AGICommandFailure ( FAILURE_CODE , result ) <EOL> def secondResultItem ( self , result ) : <EOL> """<STR_LIT>""" <EOL> return result . split ( '<STR_LIT:U+0020>' , <NUM_LIT:1> ) [ <NUM_LIT:1> ] <EOL> def resultPlusTimeoutFlag ( self , resultLine ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> digits , timeout = resultLine . split ( '<STR_LIT:U+0020>' , <NUM_LIT:1> ) <EOL> return digits . strip ( ) , True <EOL> except ValueError , err : <EOL> return resultLine . strip ( ) , False <EOL> def dateAsSeconds ( self , date ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( date , '<STR_LIT>' ) : <EOL> date = time . mktime ( date . timetuple ( ) ) <EOL> elif isinstance ( date , time . struct_time ) : <EOL> date = time . mktime ( date ) <EOL> return date <EOL> def onRecordingComplete ( self , resultLine ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> digit , exitType , endposStuff = resultLine . split ( '<STR_LIT:U+0020>' , <NUM_LIT:2> ) <EOL> except ValueError , err : <EOL> pass <EOL> else : <EOL> digit = int ( digit ) <EOL> exitType = exitType . strip ( '<STR_LIT>' ) <EOL> endposStuff = endposStuff . strip ( ) <EOL> if endposStuff . startswith ( '<STR_LIT>' ) : <EOL> endpos = int ( endposStuff [ <NUM_LIT:7> : ] . strip ( ) ) <EOL> return digit , exitType , endpos <EOL> raise ValueError ( "<STR_LIT>" % <EOL> resultLine ) <EOL> def onStreamingComplete ( self , resultLine , skipMS = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> digit , endposStuff = resultLine . split ( '<STR_LIT:U+0020>' , <NUM_LIT:1> ) <EOL> except ValueError , err : <EOL> pass <EOL> else : <EOL> digit = int ( digit ) <EOL> endposStuff = endposStuff . strip ( ) <EOL> if endposStuff . startswith ( '<STR_LIT>' ) : <EOL> endpos = int ( endposStuff [ <NUM_LIT:7> : ] . strip ( ) ) <EOL> if endpos == skipMS : <EOL> raise error . AGICommandFailure ( FAILURE_CODE , <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % ( endpos , digit ) ) <EOL> return digit , endpos <EOL> raise ValueError ( "<STR_LIT>" % <EOL> resultLine ) <EOL> def jumpOnError ( self , reason , difference = <NUM_LIT:100> , forErrors = None ) : <EOL> """<STR_LIT>""" <EOL> if forErrors : <EOL> if not isinstance ( forErrors , ( tuple , list ) ) : <EOL> forErrors = ( forErrors , ) <EOL> reason . trap ( * forErrors ) <EOL> sequence = InSequence ( ) <EOL> sequence . append ( self . setContext , self . variables [ '<STR_LIT>' ] ) <EOL> sequence . append ( self . setExtension , self . variables [ '<STR_LIT>' ] ) <EOL> sequence . append ( self . setPriority , int ( self . variables [ '<STR_LIT>' ] ) <EOL> + difference ) <EOL> sequence . append ( self . finish ) <EOL> return sequence ( ) <EOL> def finish ( self ) : <EOL> """<STR_LIT>""" <EOL> self . transport . loseConnection ( ) <EOL> def answer ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . sendCommand ( "<STR_LIT>" ) . addCallback ( <EOL> self . checkFailure <EOL> ) . addCallback ( self . resultAsInt ) <EOL> def channelStatus ( self , channel = None ) : <EOL> """<STR_LIT>""" <EOL> if channel : <EOL> command = '<STR_LIT>' % ( channel ) <EOL> else : <EOL> command = "<STR_LIT>" <EOL> return self . sendCommand ( command ) . addCallback ( <EOL> self . checkFailure , <EOL> ) . addCallback ( self . resultAsInt ) <EOL> def onControlStreamFileComplete ( self , resultLine ) : <EOL> """<STR_LIT>""" <EOL> parts = resultLine . split ( '<STR_LIT:U+0020>' , <NUM_LIT:1> ) <EOL> result = int ( parts [ <NUM_LIT:0> ] ) <EOL> endpos = None <EOL> if len ( parts ) == <NUM_LIT:2> : <EOL> endposStuff = parts [ <NUM_LIT:1> ] . strip ( ) <EOL> if endposStuff . startswith ( '<STR_LIT>' ) : <EOL> endpos = int ( endposStuff [ <NUM_LIT:7> : ] ) <EOL> else : <EOL> log . error ( "<STR_LIT>" , <EOL> resultLine ) <EOL> return result , endpos <EOL> def controlStreamFile ( <EOL> self , filename , escapeDigits , <EOL> skipMS = <NUM_LIT:0> , ffChar = '<STR_LIT:*>' , rewChar = '<STR_LIT:#>' , pauseChar = None , <EOL> ) : <EOL> """<STR_LIT>""" <EOL> command = '<STR_LIT>' % ( <EOL> filename , escapeDigits , skipMS , ffChar , rewChar <EOL> ) <EOL> if pauseChar : <EOL> command += '<STR_LIT>' % ( pauseChar ) <EOL> return self . sendCommand ( command ) . addCallback ( self . checkFailure ) . addCallback ( self . onControlStreamFileComplete ) <EOL> def databaseDel ( self , family , key ) : <EOL> """<STR_LIT>""" <EOL> command = '<STR_LIT>' % ( family , key ) <EOL> return self . sendCommand ( command ) . addCallback ( <EOL> self . checkFailure , failure = '<STR_LIT:0>' , <EOL> ) . addCallback ( self . resultAsInt ) <EOL> def databaseDeltree ( self , family , keyTree = None ) : <EOL> """<STR_LIT>""" <EOL> command = '<STR_LIT>' % ( family , ) <EOL> if keyTree : <EOL> command += '<STR_LIT>' % ( keytree , ) <EOL> return self . sendCommand ( command ) . addCallback ( <EOL> self . checkFailure , failure = '<STR_LIT:0>' , <EOL> ) . addCallback ( self . resultAsInt ) <EOL> def databaseGet ( self , family , key ) : <EOL> """<STR_LIT>""" <EOL> command = '<STR_LIT>' % ( family , key ) <EOL> def returnValue ( resultLine ) : <EOL> return resultLine [ <NUM_LIT:1> : - <NUM_LIT:1> ] <EOL> return self . sendCommand ( command ) . addCallback ( <EOL> self . checkFailure , failure = '<STR_LIT:0>' , <EOL> ) . addCallback ( self . secondResultItem ) . addCallback ( returnValue ) <EOL> def databaseSet ( self , family , key , value ) : <EOL> """<STR_LIT>""" <EOL> command = '<STR_LIT>' % ( family , key , value ) <EOL> return self . sendCommand ( command ) . addCallback ( <EOL> self . checkFailure , failure = '<STR_LIT:0>' , <EOL> ) . addCallback ( self . resultAsInt ) <EOL> databasePut = databaseSet <EOL> def execute ( self , application , * options , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> command = '''<STR_LIT>''' % ( application ) <EOL> if options : <EOL> if kwargs . pop ( '<STR_LIT>' , False ) is True : <EOL> delimiter = "<STR_LIT:U+002C>" <EOL> else : <EOL> delimiter = "<STR_LIT:|>" <EOL> command += '<STR_LIT>' % ( <EOL> delimiter . join ( [ <EOL> str ( x ) for x in options <EOL> ] ) <EOL> ) <EOL> return self . sendCommand ( command ) . addCallback ( <EOL> self . checkFailure , failure = '<STR_LIT>' , <EOL> ) <EOL> def getData ( self , filename , timeout = <NUM_LIT> , maxDigits = None ) : <EOL> """<STR_LIT>""" <EOL> timeout *= <NUM_LIT:1000> <EOL> command = '''<STR_LIT>''' % ( filename , timeout ) <EOL> if maxDigits is not None : <EOL> command = '<STR_LIT:U+0020>' . join ( [ command , str ( maxDigits ) ] ) <EOL> return self . sendCommand ( command ) . addCallback ( <EOL> self . checkFailure , failure = '<STR_LIT>' , <EOL> ) . addCallback ( self . resultPlusTimeoutFlag ) <EOL> def getOption ( self , filename , escapeDigits , timeout = None ) : <EOL> """<STR_LIT>""" <EOL> command = '''<STR_LIT>''' % ( filename , escapeDigits ) <EOL> if timeout is not None : <EOL> timeout *= <NUM_LIT:1000> <EOL> command += '<STR_LIT>' % ( timeout , ) <EOL> def charFirst ( ( c , position ) ) : <EOL> if not c : <EOL> c = '<STR_LIT>' <EOL> else : <EOL> c = chr ( c ) <EOL> return c , position <EOL> return self . sendCommand ( command ) . addCallback ( <EOL> self . checkFailure , <EOL> ) . addCallback ( <EOL> self . onStreamingComplete <EOL> ) . addCallback ( charFirst ) <EOL> def getVariable ( self , variable ) : <EOL> """<STR_LIT>""" <EOL> def stripBrackets ( value ) : <EOL> return value . strip ( ) [ <NUM_LIT:1> : - <NUM_LIT:1> ] <EOL> command = '''<STR_LIT>''' % ( variable , ) <EOL> return self . sendCommand ( command ) . addCallback ( <EOL> self . checkFailure , failure = '<STR_LIT:0>' , <EOL> ) . addCallback ( self . secondResultItem ) . addCallback ( stripBrackets ) <EOL> def hangup ( self , channel = None ) : <EOL> """<STR_LIT>""" <EOL> command = "<STR_LIT>" <EOL> if channel is not None : <EOL> command += '<STR_LIT>' % ( channel ) <EOL> return self . sendCommand ( command ) . addCallback ( <EOL> self . checkFailure , failure = '<STR_LIT>' , <EOL> ) . addCallback ( self . resultAsInt ) <EOL> def noop ( self , message = None ) : <EOL> """<STR_LIT>""" <EOL> command = "<STR_LIT>" <EOL> if message is not None : <EOL> command += '<STR_LIT>' % message <EOL> return self . sendCommand ( command ) . addCallback ( <EOL> self . checkFailure , failure = '<STR_LIT>' , <EOL> ) . addCallback ( self . resultAsInt ) <EOL> def playback ( self , filename , doAnswer = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> option = { - <NUM_LIT:1> : '<STR_LIT>' , <NUM_LIT:0> : '<STR_LIT>' , <NUM_LIT:1> : '<STR_LIT>' } [ doAnswer ] <EOL> except KeyError : <EOL> raise TypeError ( "<STR_LIT>" <EOL> "<STR_LIT>" % doAnswer ) <EOL> command = '<STR_LIT>' % ( filename , ) <EOL> if option : <EOL> command += '<STR_LIT>' % ( option , ) <EOL> return self . execute ( command ) . addCallback ( <EOL> self . checkFailure , failure = '<STR_LIT>' , <EOL> ) . addCallback ( self . resultAsInt ) <EOL> def receiveChar ( self , timeout = None ) : <EOL> """<STR_LIT>""" <EOL> command = '''<STR_LIT>''' <EOL> if timeout is not None : <EOL> timeout *= <NUM_LIT:1000> <EOL> command += '<STR_LIT>' % ( timeout , ) <EOL> return self . sendCommand ( command ) . addCallback ( <EOL> self . checkFailure , failure = '<STR_LIT>' , <EOL> ) . addCallback ( self . resultPlusTimeoutFlag ) <EOL> def receiveText ( self , timeout = None ) : <EOL> """<STR_LIT>""" <EOL> command = '''<STR_LIT>''' <EOL> if timeout is not None : <EOL> timeout *= <NUM_LIT:1000> <EOL> command += '<STR_LIT>' % ( timeout , ) <EOL> return self . sendCommand ( command ) . addCallback ( <EOL> self . checkFailure , failure = '<STR_LIT>' , <EOL> ) <EOL> def recordFile ( <EOL> self , filename , format , escapeDigits , timeout = - <NUM_LIT:1> , <EOL> offsetSamples = None , beep = True , silence = None , <EOL> ) : <EOL> """<STR_LIT>""" <EOL> timeout *= <NUM_LIT:1000> <EOL> command = '''<STR_LIT>''' % ( <EOL> filename , format , escapeDigits , timeout , <EOL> ) <EOL> if offsetSamples is not None : <EOL> command += '<STR_LIT>' % ( offsetSamples , ) <EOL> if beep : <EOL> command += '<STR_LIT>' <EOL> if silence is not None : <EOL> command += '<STR_LIT>' % ( silence , ) <EOL> def onResult ( resultLine ) : <EOL> value , type , endpos = resultLine . split ( '<STR_LIT:U+0020>' ) <EOL> type = type . strip ( ) [ <NUM_LIT:1> : - <NUM_LIT:1> ] <EOL> endpos = int ( endpos . split ( '<STR_LIT:=>' ) [ <NUM_LIT:1> ] ) <EOL> return ( value , type , endpos ) <EOL> return self . sendCommand ( command ) . addCallback ( <EOL> self . onRecordingComplete <EOL> ) <EOL> def sayXXX ( self , baseCommand , value , escapeDigits = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> command = '<STR_LIT>' % ( baseCommand , value , escapeDigits or '<STR_LIT>' ) <EOL> return self . sendCommand ( command ) . addCallback ( <EOL> self . checkFailure , failure = '<STR_LIT>' , <EOL> ) . addCallback ( self . resultAsInt ) <EOL> def sayAlpha ( self , string , escapeDigits = None ) : <EOL> """<STR_LIT>""" <EOL> string = "<STR_LIT>" . join ( [ x for x in string if x . isalnum ( ) ] ) <EOL> return self . sayXXX ( '<STR_LIT>' , string , escapeDigits ) <EOL> def sayDate ( self , date , escapeDigits = None ) : <EOL> """<STR_LIT>""" <EOL> return self . sayXXX ( '<STR_LIT>' , self . dateAsSeconds ( date ) , escapeDigits ) <EOL> def sayDigits ( self , number , escapeDigits = None ) : <EOL> """<STR_LIT>""" <EOL> number = "<STR_LIT>" . join ( [ x for x in str ( number ) if x . isdigit ( ) ] ) <EOL> return self . sayXXX ( '<STR_LIT>' , number , escapeDigits ) <EOL> def sayNumber ( self , number , escapeDigits = None ) : <EOL> """<STR_LIT>""" <EOL> number = "<STR_LIT>" . join ( [ x for x in str ( number ) if x . isdigit ( ) ] ) <EOL> return self . sayXXX ( '<STR_LIT>' , number , escapeDigits ) <EOL> def sayPhonetic ( self , string , escapeDigits = None ) : <EOL> """<STR_LIT>""" <EOL> string = "<STR_LIT>" . join ( [ x for x in string if x . isalnum ( ) ] ) <EOL> return self . sayXXX ( '<STR_LIT>' , string , escapeDigits ) <EOL> def sayTime ( self , time , escapeDigits = None ) : <EOL> """<STR_LIT>""" <EOL> return self . sayXXX ( '<STR_LIT>' , self . dateAsSeconds ( time ) , escapeDigits ) <EOL> def sayDateTime ( self , time , escapeDigits = '<STR_LIT>' , format = None , timezone = None ) : <EOL> """<STR_LIT>""" <EOL> command = '<STR_LIT>' % ( self . dateAsSeconds ( time ) , <EOL> escapeDigits ) <EOL> if format is not None : <EOL> command += '<STR_LIT>' % ( format , ) <EOL> if timezone is not None : <EOL> command += '<STR_LIT>' % ( timezone , ) <EOL> return self . sendCommand ( command ) . addCallback ( <EOL> self . checkFailure , failure = '<STR_LIT>' , <EOL> ) . addCallback ( self . resultAsInt ) <EOL> def sendImage ( self , filename ) : <EOL> """<STR_LIT>""" <EOL> command = '<STR_LIT>' % ( filename , ) <EOL> return self . sendCommand ( command ) . addCallback ( <EOL> self . checkFailure , failure = '<STR_LIT>' , <EOL> ) . addCallback ( self . resultAsInt ) <EOL> def sendText ( self , text ) : <EOL> """<STR_LIT>""" <EOL> command = "<STR_LIT>" % ( text ) <EOL> return self . sendCommand ( command ) . addCallback ( <EOL> self . checkFailure , failure = '<STR_LIT>' , <EOL> ) . addCallback ( self . resultAsInt ) <EOL> def setAutoHangup ( self , time ) : <EOL> """<STR_LIT>""" <EOL> command = """<STR_LIT>""" % ( time , ) <EOL> return self . sendCommand ( command ) . addCallback ( <EOL> self . checkFailure , failure = '<STR_LIT>' , <EOL> ) . addCallback ( self . resultAsInt ) <EOL> def setCallerID ( self , number ) : <EOL> """<STR_LIT>""" <EOL> command = "<STR_LIT>" % ( number ) <EOL> return self . sendCommand ( command ) . addCallback ( self . resultAsInt ) <EOL> def setContext ( self , context ) : <EOL> """<STR_LIT>""" <EOL> command = """<STR_LIT>""" % ( context , ) <EOL> return self . sendCommand ( command ) . addCallback ( self . resultAsInt ) <EOL> def setExtension ( self , extension ) : <EOL> """<STR_LIT>""" <EOL> command = """<STR_LIT>""" % ( extension , ) <EOL> return self . sendCommand ( command ) . addCallback ( self . resultAsInt ) <EOL> def setMusic ( self , on = True , musicClass = None ) : <EOL> """<STR_LIT>""" <EOL> command = """<STR_LIT>""" % ( [ '<STR_LIT>' , '<STR_LIT>' ] [ on ] , ) <EOL> if musicClass is not None : <EOL> command += "<STR_LIT>" % ( musicClass , ) <EOL> return self . sendCommand ( command ) . addCallback ( self . resultAsInt ) <EOL> def setPriority ( self , priority ) : <EOL> """<STR_LIT>""" <EOL> command = """<STR_LIT>""" % ( priority , ) <EOL> return self . sendCommand ( command ) . addCallback ( self . resultAsInt ) <EOL> def setVariable ( self , variable , value ) : <EOL> """<STR_LIT>""" <EOL> value = '''<STR_LIT>''' % ( str ( value ) . replace ( '<STR_LIT:">' , '<STR_LIT>' ) , ) <EOL> command = '<STR_LIT>' % ( variable , value ) <EOL> return self . sendCommand ( command ) . addCallback ( self . resultAsInt ) <EOL> def streamFile ( self , filename , escapeDigits = "<STR_LIT>" , offset = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> command = '<STR_LIT>' % ( filename , escapeDigits ) <EOL> if offset is not None : <EOL> command += '<STR_LIT>' % ( offset ) <EOL> return self . sendCommand ( command ) . addCallback ( <EOL> self . checkFailure , failure = '<STR_LIT>' , <EOL> ) . addCallback ( self . onStreamingComplete , skipMS = offset ) <EOL> def tddMode ( self , on = True ) : <EOL> """<STR_LIT>""" <EOL> if on is True : <EOL> on = '<STR_LIT>' <EOL> elif on is False : <EOL> on = '<STR_LIT>' <EOL> elif on is None : <EOL> on = '<STR_LIT>' <EOL> command = '<STR_LIT>' % ( on , ) <EOL> return self . sendCommand ( command ) . addCallback ( <EOL> self . checkFailure , failure = '<STR_LIT>' , <EOL> ) . addCallback ( <EOL> self . checkFailure , failure = '<STR_LIT:0>' , <EOL> ) . addCallback ( <EOL> self . resultAsInt , <EOL> ) <EOL> def verbose ( self , message , level = None ) : <EOL> """<STR_LIT>""" <EOL> command = '<STR_LIT>' % ( message , ) <EOL> if level is not None : <EOL> command += '<STR_LIT>' % ( level ) <EOL> return self . sendCommand ( command ) . addCallback ( <EOL> self . resultAsInt , <EOL> ) <EOL> def waitForDigit ( self , timeout ) : <EOL> """<STR_LIT>""" <EOL> timeout *= <NUM_LIT:1000> <EOL> command = "<STR_LIT>" % ( timeout , ) <EOL> return self . sendCommand ( command ) . addCallback ( <EOL> self . checkFailure , failure = '<STR_LIT>' , <EOL> ) . addCallback ( <EOL> self . resultAsInt , <EOL> ) <EOL> def wait ( self , duration ) : <EOL> """<STR_LIT>""" <EOL> df = defer . Deferred ( ) <EOL> reactor . callLater ( duration , df . callback , <NUM_LIT:0> ) <EOL> return df <EOL> class InSequence ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . actions = [ ] <EOL> self . results = [ ] <EOL> self . finalDF = None <EOL> def append ( self , function , * args , ** named ) : <EOL> """<STR_LIT>""" <EOL> self . actions . append ( ( function , args , named ) ) <EOL> def __call__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _doSequence ( ) <EOL> def _doSequence ( self ) : <EOL> """<STR_LIT>""" <EOL> finalDF = defer . Deferred ( ) <EOL> self . onActionSuccess ( None , finalDF = finalDF ) <EOL> return finalDF <EOL> def recordResult ( self , result ) : <EOL> """<STR_LIT>""" <EOL> self . results . append ( result ) <EOL> return result <EOL> def onActionSuccess ( self , result , finalDF ) : <EOL> """<STR_LIT>""" <EOL> log . debug ( '<STR_LIT>' , result ) <EOL> if self . actions : <EOL> action = self . actions . pop ( <NUM_LIT:0> ) <EOL> log . debug ( '<STR_LIT>' , action ) <EOL> df = defer . maybeDeferred ( action [ <NUM_LIT:0> ] , * action [ <NUM_LIT:1> ] , ** action [ <NUM_LIT:2> ] ) <EOL> df . addCallback ( self . recordResult ) <EOL> df . addCallback ( self . onActionSuccess , finalDF = finalDF ) <EOL> df . addErrback ( self . onActionFailure , finalDF = finalDF ) <EOL> return df <EOL> else : <EOL> finalDF . callback ( self . results ) <EOL> def onActionFailure ( self , reason , finalDF ) : <EOL> """<STR_LIT>""" <EOL> log . debug ( '<STR_LIT>' ) <EOL> reason . results = self . results <EOL> finalDF . errback ( reason ) <EOL> class FastAGIFactory ( protocol . Factory ) : <EOL> """<STR_LIT>""" <EOL> protocol = FastAGIProtocol <EOL> def __init__ ( self , mainFunction ) : <EOL> """<STR_LIT>""" <EOL> self . mainFunction = mainFunction </s>
