예제 #1
0
    def pyforaTupleToTuple(self):
        if self.valueIVC is None:
            return None

        result = PyforaObjectConverter.PyforaObjectConverter().unwrapPyforaTupleToTuple(self.valueIVC)
        assert isinstance(result, tuple)
        return result
예제 #2
0
    def pyforaDictToDictOfAssignedVarsToProxyValues(self):
        if self.valueIVC is None:
            return None

        assert not self.isException, "We should not allow exceptions to be thrown here. Instead we should " +\
            " be wrapping the code in try/catch and returning data that contains any updated variables after the exception."

        result = PyforaObjectConverter.PyforaObjectConverter()\
                    .unwrapPyforaDictToDictOfAssignedVars(self.valueIVC)
        assert isinstance(result, dict)
        return result
예제 #3
0
    def args(self):
        converter = PyforaObjectConverter.PyforaObjectConverter()
        def unwrapArg(argId):
            if isinstance(argId, int):
                return converter.getIvcFromObjectId(argId)
            else:
                return argId

        implVals = tuple(unwrapArg(arg) for arg in self.argIds)

        return implVals[:1] + (ForaValue.FORAValue.symbol_Call.implVal_,) + implVals[1:]
예제 #4
0
def validateObjectIds(ids):
    converter = PyforaObjectConverter.PyforaObjectConverter()
    return all([converter.hasObjectId(i) for i in ids if isinstance(i, int)])
예제 #5
0
    def getResultAsJson(self, *args):
        """If we are over the complexity limit, None, else the result encoded as json"""
        if self.computedValue.isFailure:
            return None

        if self.computedValue.valueIVC is None:
            return None

        value = self.computedValue.valueIVC

        if self.computedValue.isException:
            if value.isTuple():
                #the first element is the exception. The second element is the stacktrace and variables.
                value = value[0]

        c = PyforaObjectConverter.PyforaObjectConverter()

        try:
            def extractVectorContents(vectorIVC):
                if len(vectorIVC) == 0:
                    return {'listContents': []}

                #if this is an unpaged vector we can handle it without callback
                vdm = ComputedValueGateway.getGateway().vdm
                if vdm.vectorDataIsLoaded(vectorIVC, 0, len(vectorIVC)) and vectorIVC.isVectorEntirelyUnpaged():
                    #see if it's a string. This is the only way to be holding a Vector of char
                    if vectorIVC.isVectorOfChar():
                        res = vdm.extractVectorContentsAsNumpyArray(vectorIVC, 0, len(vectorIVC))
                        assert res is not None
                        return {'string': res.tostring()}

                    #see if it's simple enough to transmit as numpy data
                    if len(vectorIVC.getVectorElementsJOR()) == 1 and len(vectorIVC) > 1:
                        firstElement = vdm.extractVectorItem(vectorIVC, 0)

                        if isOfSimpleType(firstElement):
                            res = vdm.extractVectorContentsAsNumpyArray(vectorIVC, 0, len(vectorIVC))

                            if res is not None:
                                assert len(res) == len(vectorIVC)
                                return {'contentsAsNumpyArray': res}

                    #see if we can extract the data as a regular pythonlist
                    res = vdm.extractVectorContentsAsPythonArray(vectorIVC, 0, len(vectorIVC)) 
                    assert res is not None
                    return {'listContents': res}

                vec = ComputedValue.ComputedValueVector(vectorImplVal=vectorIVC)
                vecSlice = vec.entireSlice

                res = None
                preventPythonArrayExtraction = False

                #see if it's a string. This is the only way to be holding a Vector of char
                if vectorIVC.isVectorOfChar():
                    res = vecSlice.extractVectorDataAsNumpyArray()
                    if res is not None:
                        res = {'string': res.tostring()}

                #see if it's simple enough to transmit as numpy data
                if res is None and len(vectorIVC.getVectorElementsJOR()) == 1 and len(vectorIVC) > 1:
                    res = vecSlice.extractVectorDataAsNumpyArray()

                    if res is not None:
                        firstElement = vecSlice.extractVectorItemAsIVC(0)
                        if firstElement is None:
                            #note we can't import this at the top of the file because this file gets imported
                            #during the build process, which doesn't have pyfora installed.
                            import pyfora.Exceptions as Exceptions
                            raise Exceptions.ForaToPythonConversionError(
                                "Shouldn't be possible to download data as numpy, and then not get the first value"
                                )

                        if isOfSimpleType(firstElement):
                            res = {'contentsAsNumpyArray': res}
                        else:
                            res = None
                    else:
                        if not vecSlice.vdmThinksIsLoaded():
                            #there's a race condition where the data could be loaded between now and
                            #the call to 'extractVectorDataAsPythonArray'. This prevents it.
                            preventPythonArrayExtraction = True

                #see if we can extract the data as a regular pythonlist
                if not preventPythonArrayExtraction and res is None:
                    res = vecSlice.extractVectorDataAsPythonArray()
                    if res is not None:
                        res = {'listContents': res}

                if res is None:
                    vecSlice.increaseRequestCount()
                    return None

                return res

            try:
                import pyfora.BinaryObjectRegistry as BinaryObjectRegistry
                stream = BinaryObjectRegistry.BinaryObjectRegistry()

                root_id, needsLoading = c.transformPyforaImplval(
                    value,
                    stream,
                    extractVectorContents,
                    self.maxBytecount
                    )

                if needsLoading:
                    return None

                result_to_send = {'data': base64.b64encode(stream.str()), 'root_id': root_id}

            except Exception as e:
                import pyfora
                if self.computedValue.isException and isinstance(e, pyfora.ForaToPythonConversionError):
                    return {
                        'result': {
                            "untranslatableException": str(ForaValue.FORAValue(self.computedValue.valueIVC))
                            },
                        'isException': True,
                        'trace': self.computedValue.exceptionCodeLocationsAsJson
                        }
                elif isinstance(e, pyfora.ForaToPythonConversionError):
                    return {
                        'foraToPythonConversionError': e.message
                        }
                else:
                    raise

            if self.computedValue.isException:
                return {
                    'result': result_to_send,
                    'isException': True,
                    'trace': self.computedValue.exceptionCodeLocationsAsJson
                    }
            else:
                return {
                    'result': result_to_send,
                    'isException': False
                    }

        except PyforaToJsonTransformer.HaltTransformationException:
            if self.computedValue.isException:
                return {
                    'maxBytesExceeded': True,
                    'isException': True,
                    'trace': self.computedValue.exceptionCodeLocationsAsJson
                    }
            else:
                return {'maxBytesExceeded': True, 'isException': False}
예제 #6
0
    def result(self):
        """If we are over the complexity limit, None, else the result encoded as json"""
        if self.computedValue.isFailure:
            return None

        if self.computedValue.valueIVC is None:
            return None

        value = self.computedValue.valueIVC

        if self.computedValue.isException:
            if value.isTuple():
                #the first element is the exception. The second element is the stacktrace and variables.
                value = value[0]

        c = PyforaObjectConverter.PyforaObjectConverter()

        #ask the objectConverter to convert this python object to something
        #we can send back to the server as json
        transformer = PyforaToJsonTransformer.PyforaToJsonTransformer(
            self.maxBytecount)

        try:
            vectorSlicesNeedingLoad = []

            def extractVectorContents(vectorIVC):
                if len(vectorIVC) == 0:
                    return []
                vec = ComputedValue.ComputedValueVector(
                    vectorImplVal=vectorIVC)
                vecSlice = vec.entireSlice

                if vectorIVC.isVectorOfChar():
                    res = vecSlice.extractVectorDataAsNumpyArray()
                    if res is not None:
                        res = res.tostring()
                else:
                    res = vecSlice.extractVectorDataAsPythonArray()

                if res is None:
                    vectorSlicesNeedingLoad.append(vecSlice)
                    vecSlice.increaseRequestCount()
                    return None

                return res

            try:
                res = c.transformPyforaImplval(value, transformer,
                                               extractVectorContents)
            except Exception as e:
                import pyfora
                if self.computedValue.isException and isinstance(
                        e, pyfora.ForaToPythonConversionError):
                    return {
                        'result': {
                            "untranslatableException":
                            str(
                                ForaValue.FORAValue(
                                    self.computedValue.valueIVC))
                        },
                        'isException': True,
                        'trace':
                        self.computedValue.exceptionCodeLocationsAsJson
                    }
                elif isinstance(e, pyfora.ForaToPythonConversionError):
                    return {'foraToPythonConversionError': e.message}
                else:
                    raise

            if transformer.anyListsThatNeedLoading:
                return None
            else:
                if self.computedValue.isException:
                    return {
                        'result': res,
                        'isException': True,
                        'trace':
                        self.computedValue.exceptionCodeLocationsAsJson
                    }
                else:
                    return {'result': res, 'isException': False}

        except PyforaToJsonTransformer.HaltTransformationException:
            if self.computedValue.isException:
                return {
                    'maxBytesExceeded': True,
                    'isException': True,
                    'trace': self.computedValue.exceptionCodeLocationsAsJson
                }
            else:
                return {'maxBytesExceeded': True, 'isException': False}
예제 #7
0
    def getResultAsJson(self, *args):
        """If we are over the complexity limit, None, else the result encoded as json"""
        if self.computedValue.isFailure:
            return None

        if self.computedValue.valueIVC is None:
            return None

        value = self.computedValue.valueIVC

        if self.computedValue.isException:
            if value.isTuple():
                #the first element is the exception. The second element is the stacktrace and variables.
                value = value[0]

        c = PyforaObjectConverter.PyforaObjectConverter()

        #ask the objectConverter to convert this python object to something
        #we can send back to the server as json
        transformer = PyforaToJsonTransformer.PyforaToJsonTransformer(
            self.maxBytecount)

        try:

            def extractVectorContents(vectorIVC):
                if len(vectorIVC) == 0:
                    return {'listContents': []}
                vec = ComputedValue.ComputedValueVector(
                    vectorImplVal=vectorIVC)
                vecSlice = vec.entireSlice

                res = None
                preventPythonArrayExtraction = False

                #see if it's a string. This is the only way to be holding a Vector of char
                if vectorIVC.isVectorOfChar():
                    res = vecSlice.extractVectorDataAsNumpyArray()
                    if res is not None:
                        res = {'string': res.tostring()}

                #see if it's simple enough to transmit as numpy data
                if res is None and len(vectorIVC.getVectorElementsJOR()
                                       ) == 1 and len(vectorIVC) > 1:
                    res = vecSlice.extractVectorDataAsNumpyArrayInChunks()

                    if res is not None:
                        firstElement = vecSlice.extractVectorItemAsIVC(0)
                        if firstElement is None:
                            #note we can't import this at the top of the file because this file gets imported
                            #during the build process, which doesn't have pyfora installed.
                            import pyfora.Exceptions as Exceptions
                            raise Exceptions.ForaToPythonConversionError(
                                "Shouldn't be possible to download data as numpy, and then not get the first value"
                            )

                        res = {
                            'firstElement': firstElement,
                            'contentsAsNumpyArrays': res
                        }
                    else:
                        if not vecSlice.vdmThinksIsLoaded():
                            #there's a race condition where the data could be loaded between now and
                            #the call to 'extractVectorDataAsPythonArray'. This prevents it.
                            preventPythonArrayExtraction = True

                #see if we can extract the data as a regular pythonlist
                if not preventPythonArrayExtraction and res is None:
                    res = vecSlice.extractVectorDataAsPythonArray()
                    if res is not None:
                        res = {'listContents': res}

                if res is None:
                    vecSlice.increaseRequestCount()
                    return None

                return res

            try:
                res = c.transformPyforaImplval(value, transformer,
                                               extractVectorContents)
            except Exception as e:
                import pyfora
                if self.computedValue.isException and isinstance(
                        e, pyfora.ForaToPythonConversionError):
                    return {
                        'result': {
                            "untranslatableException":
                            str(
                                ForaValue.FORAValue(
                                    self.computedValue.valueIVC))
                        },
                        'isException': True,
                        'trace':
                        self.computedValue.exceptionCodeLocationsAsJson
                    }
                elif isinstance(e, pyfora.ForaToPythonConversionError):
                    return {'foraToPythonConversionError': e.message}
                else:
                    raise

            if transformer.anyListsThatNeedLoading:
                return None
            else:
                if self.computedValue.isException:
                    return {
                        'result': res,
                        'isException': True,
                        'trace':
                        self.computedValue.exceptionCodeLocationsAsJson
                    }
                else:
                    return {'result': res, 'isException': False}

        except PyforaToJsonTransformer.HaltTransformationException:
            if self.computedValue.isException:
                return {
                    'maxBytesExceeded': True,
                    'isException': True,
                    'trace': self.computedValue.exceptionCodeLocationsAsJson
                }
            else:
                return {'maxBytesExceeded': True, 'isException': False}