<s> """<STR_LIT>""" <EOL> import pyfora . Exceptions as Exceptions <EOL> import pyfora . Executor as Executor <EOL> import pyfora . ObjectConverter as ObjectConverter <EOL> import pyfora . RemotePythonObject as RemotePythonObject <EOL> import pyfora . SocketIoJsonInterface as SocketIoJsonInterface <EOL> import pyfora . ModuleDirectoryStructure as ModuleDirectoryStructure <EOL> import pyfora <EOL> import os <EOL> class Connection ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , webObjectFactory , converter ) : <EOL> self . objectConverter = converter <EOL> self . webObjectFactory = webObjectFactory <EOL> self . closed = False <EOL> def triggerS3DatasetExport ( self , <EOL> valueAsString , <EOL> bucketname , <EOL> keyname , <EOL> onCompletedCallback ) : <EOL> if not isinstance ( valueAsString , RemotePythonObject . ComputedRemotePythonObject ) : <EOL> onCompletedCallback ( <EOL> Exceptions . PyforaError ( <EOL> "<STR_LIT>" <EOL> ) <EOL> ) <EOL> return <EOL> import pyfora . SubscribableWebObjects as SubscribableWebObjects <EOL> if not isinstance ( valueAsString . computedValue , SubscribableWebObjects . PyforaComputedValue ) : <EOL> onCompletedCallback ( <EOL> Exceptions . PyforaError ( <EOL> "<STR_LIT>" <EOL> ) <EOL> ) <EOL> return <EOL> computedValue = valueAsString . computedValue <EOL> computedValueToCalculate = self . webObjectFactory . ComputedValueForMember ( <EOL> { <EOL> '<STR_LIT>' : computedValue , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } ) <EOL> def onFailure ( err ) : <EOL> if not self . closed : <EOL> onCompletedCallback ( Exceptions . PyforaError ( err [ '<STR_LIT:message>' ] ) ) <EOL> def isFinishedChanged ( isFinished ) : <EOL> if not self . closed and isFinished : <EOL> self . triggerS3DatasetExportOnFinishedCalculation ( <EOL> computedValueToCalculate , <EOL> bucketname , <EOL> keyname , <EOL> onCompletedCallback <EOL> ) <EOL> def subscribeToFinished ( result ) : <EOL> computedValueToCalculate . subscribe_isFinished ( { <EOL> '<STR_LIT>' : isFinishedChanged , <EOL> '<STR_LIT>' : onFailure , <EOL> '<STR_LIT>' : isFinishedChanged <EOL> } ) <EOL> computedValueToCalculate . increaseRequestCount ( <EOL> { } , <EOL> { '<STR_LIT>' : subscribeToFinished , '<STR_LIT>' : onFailure } <EOL> ) <EOL> def triggerS3DatasetExportOnFinishedCalculation ( self , <EOL> computedValue , <EOL> bucketname , <EOL> keyname , <EOL> onCompletedCallback ) : <EOL> def onSuccess ( writeToS3TaskObject ) : <EOL> self . subscribeToWriteToS3TaskResultAndCallCallback ( writeToS3TaskObject , <EOL> onCompletedCallback ) <EOL> def onFailure ( err ) : <EOL> onCompletedCallback ( Exceptions . PyforaError ( err [ '<STR_LIT:message>' ] ) ) <EOL> computedValue . writeToS3 ( <EOL> { '<STR_LIT>' : bucketname , '<STR_LIT>' : keyname } , <EOL> { '<STR_LIT>' : onSuccess , '<STR_LIT>' : onFailure } <EOL> ) <EOL> def subscribeToWriteToS3TaskResultAndCallCallback ( self , <EOL> writeToS3TaskObject , <EOL> onCompletedCallback ) : <EOL> def onSuccess ( result ) : <EOL> if not self . closed and result is not None : <EOL> if result [ '<STR_LIT:success>' ] : <EOL> onCompletedCallback ( None ) <EOL> else : <EOL> onCompletedCallback ( Exceptions . PyforaError ( result [ '<STR_LIT:message>' ] ) ) <EOL> def onFailure ( err ) : <EOL> onCompletedCallback ( Exceptions . PyforaError ( err [ '<STR_LIT:message>' ] ) ) <EOL> writeToS3TaskObject . subscribe_successOrError ( { <EOL> '<STR_LIT>' : onSuccess , <EOL> '<STR_LIT>' : onSuccess , <EOL> '<STR_LIT>' : onFailure <EOL> } ) <EOL> def convertObject ( self , objectId , objectRegistry , callback ) : <EOL> def wrapper ( * args , ** kwargs ) : <EOL> if not self . closed : <EOL> callback ( * args , ** kwargs ) <EOL> self . objectConverter . convert ( objectId , objectRegistry , wrapper ) <EOL> def createComputation ( self , fn , args , onCreatedCallback ) : <EOL> """<STR_LIT>""" <EOL> assert isinstance ( fn , RemotePythonObject . RemotePythonObject ) <EOL> assert all ( [ isinstance ( arg , RemotePythonObject . RemotePythonObject ) for arg in args ] ) <EOL> computedValue = self . webObjectFactory . PyforaComputedValue ( { <EOL> '<STR_LIT>' : ( fn . _pyforaComputedValueArg ( ) , ) + tuple ( <EOL> arg . _pyforaComputedValueArg ( ) for arg in args <EOL> ) <EOL> } ) <EOL> def onFailure ( err ) : <EOL> if not self . closed : <EOL> onCreatedCallback ( Exceptions . PyforaError ( err ) ) <EOL> def onSuccess ( computationId ) : <EOL> if not self . closed : <EOL> onCreatedCallback ( computedValue ) <EOL> def onChanged ( computationId ) : <EOL> pass <EOL> computedValue . subscribe_submittedComputationId ( { <EOL> '<STR_LIT>' : onSuccess , <EOL> '<STR_LIT>' : onFailure , <EOL> '<STR_LIT>' : onChanged <EOL> } ) <EOL> def prioritizeComputation ( self , <EOL> computedValue , <EOL> onPrioritizedCallback , <EOL> onCompletedCallback , <EOL> onFailedCallback ) : <EOL> """<STR_LIT>""" <EOL> def onFailure ( err ) : <EOL> if not self . closed : <EOL> onPrioritizedCallback ( Exceptions . PyforaError ( err ) ) <EOL> def onSuccess ( result ) : <EOL> if not self . closed : <EOL> onPrioritizedCallback ( None ) <EOL> self . _subscribeToComputationStatus ( computedValue , <EOL> onCompletedCallback , <EOL> onFailedCallback ) <EOL> computedValue . increaseRequestCount ( { } , { <EOL> '<STR_LIT>' : onSuccess , <EOL> '<STR_LIT>' : onFailure <EOL> } ) <EOL> def triggerCompilationOnComputation ( self , computedValue , onCompleted ) : <EOL> """<STR_LIT>""" <EOL> def onFailure ( err ) : <EOL> onCompleted ( ) <EOL> def onSuccess ( result ) : <EOL> onCompleted ( ) <EOL> computedValue . triggerCompilation ( { } , { <EOL> '<STR_LIT>' : onSuccess , <EOL> '<STR_LIT>' : onFailure <EOL> } ) <EOL> @ staticmethod <EOL> def cancelComputation ( computedValue ) : <EOL> """<STR_LIT>""" <EOL> def completed ( _ ) : <EOL> pass <EOL> computedValue . cancel ( { } , { <EOL> '<STR_LIT>' : completed , <EOL> '<STR_LIT>' : completed <EOL> } ) <EOL> def expandComputedValueToDictOfAssignedVarsToProxyValues ( self , computedValue , onExpanded ) : <EOL> """<STR_LIT>""" <EOL> def onResult ( result ) : <EOL> if result is not None and not self . closed : <EOL> onExpanded ( result ) <EOL> def onFailure ( result ) : <EOL> if isinstance ( result , Exception ) : <EOL> onExpanded ( result ) <EOL> else : <EOL> onExpanded ( <EOL> Exceptions . PyforaError ( <EOL> "<STR_LIT>" + str ( result ) <EOL> ) <EOL> ) <EOL> computedValue . increaseRequestCount ( <EOL> { } , <EOL> { '<STR_LIT>' : lambda * args : None , '<STR_LIT>' : lambda * args : None } <EOL> ) <EOL> computedValue . subscribe_pyforaDictToAssignedVarsToComputedValues ( { <EOL> '<STR_LIT>' : onResult , <EOL> '<STR_LIT>' : onFailure , <EOL> '<STR_LIT>' : onResult <EOL> } ) <EOL> def expandComputedValueToTupleOfProxies ( self , computedValue , onExpanded ) : <EOL> def onResult ( result ) : <EOL> if result is not None and not self . closed : <EOL> onExpanded ( result ) <EOL> def onFailure ( result ) : <EOL> if isinstance ( result , Exception ) : <EOL> onExpanded ( result ) <EOL> else : <EOL> onExpanded ( <EOL> Exceptions . PyforaError ( <EOL> "<STR_LIT>" + str ( result ) <EOL> ) <EOL> ) <EOL> computedValue . increaseRequestCount ( <EOL> { } , <EOL> { '<STR_LIT>' : lambda * args : None , '<STR_LIT>' : lambda * args : None } <EOL> ) <EOL> computedValue . subscribe_pyforaTupleToTupleOfComputedValues ( { <EOL> '<STR_LIT>' : onResult , <EOL> '<STR_LIT>' : onFailure , <EOL> '<STR_LIT>' : onResult <EOL> } ) <EOL> def _subscribeToComputationStatus ( self , computedValue , onCompletedCallback , onFailedCallback ) : <EOL> def statusChanged ( jsonStatus ) : <EOL> if not self . closed : <EOL> if jsonStatus is not None : <EOL> if jsonStatus [ '<STR_LIT:status>' ] == '<STR_LIT>' : <EOL> onFailedCallback ( Exceptions . PyforaError ( jsonStatus [ '<STR_LIT:message>' ] ) ) <EOL> else : <EOL> onCompletedCallback ( jsonStatus ) <EOL> def onFailure ( err ) : <EOL> if not self . closed : <EOL> onFailedCallback ( Exceptions . PyforaError ( err ) ) <EOL> computedValue . subscribe_jsonStatusRepresentation ( { <EOL> '<STR_LIT>' : statusChanged , <EOL> '<STR_LIT>' : onFailure , <EOL> '<STR_LIT>' : statusChanged <EOL> } ) <EOL> def downloadComputation ( self , computedValue , onResultCallback , maxBytecount = None ) : <EOL> """<STR_LIT>""" <EOL> def onFailure ( err ) : <EOL> if not self . closed : <EOL> onResultCallback ( Exceptions . PyforaError ( err [ '<STR_LIT:message>' ] ) ) <EOL> def resultChanged ( jsonStatus ) : <EOL> if not self . closed and jsonStatus is not None : <EOL> onResultCallback ( jsonStatus ) <EOL> computedValue . increaseRequestCount ( <EOL> { } , <EOL> { '<STR_LIT>' : lambda * args : None , '<STR_LIT>' : lambda * args : None } <EOL> ) <EOL> def resultStatusChanged ( populated ) : <EOL> if not self . closed and populated : <EOL> resultComputer . getResultAsJson ( { } , { <EOL> '<STR_LIT>' : resultChanged , <EOL> '<STR_LIT>' : onFailure <EOL> } ) <EOL> resultComputer = self . webObjectFactory . PyforaResultAsJson ( <EOL> { '<STR_LIT>' : computedValue , '<STR_LIT>' : maxBytecount } <EOL> ) <EOL> resultComputer . subscribe_resultIsPopulated ( { <EOL> '<STR_LIT>' : resultStatusChanged , <EOL> '<STR_LIT>' : onFailure , <EOL> '<STR_LIT>' : resultStatusChanged <EOL> } ) <EOL> def close ( self ) : <EOL> self . closed = True <EOL> self . webObjectFactory . getJsonInterface ( ) . close ( ) <EOL> def createObjectConverter ( webObjectFactory ) : <EOL> path = os . path . join ( os . path . abspath ( os . path . split ( pyfora . __file__ ) [ <NUM_LIT:0> ] ) , "<STR_LIT>" ) <EOL> moduleTree = ModuleDirectoryStructure . ModuleDirectoryStructure . read ( path , "<STR_LIT>" , "<STR_LIT>" ) <EOL> return ObjectConverter . ObjectConverter ( webObjectFactory , moduleTree . toJson ( ) ) <EOL> def connect ( url , timeout = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> socketIoInterface = SocketIoJsonInterface . SocketIoJsonInterface ( <EOL> url , <EOL> '<STR_LIT>' <EOL> ) <EOL> socketIoInterface . connect ( timeout = timeout ) <EOL> return connectGivenSocketIo ( socketIoInterface ) <EOL> def connectGivenSocketIo ( socketIoInterface ) : <EOL> import pyfora . SubscribableWebObjects as SubscribableWebObjects <EOL> webObjectFactory = SubscribableWebObjects . WebObjectFactory ( socketIoInterface ) <EOL> return Executor . Executor ( Connection ( webObjectFactory , createObjectConverter ( webObjectFactory ) ) ) </s>
