def construct(json): if (not isinstance(json, dict) or 'type' not in json or 'args' not in json): raise Exceptions.SubscribableWebObjectsException("Invalid ObjectDefinition: %s", json) if json['type'] not in classMap: raise Exceptions.SubscribableWebObjectsException("Unknown class type: %s", json['type']) return json['type'](json['args'])
def handleAssignMessage(self, jsonMessage, objectToRead): if "field" not in jsonMessage: raise MalformedMessageException("incoming message missing 'field' field: " + str(jsonMessage)) if "value" not in jsonMessage: raise MalformedMessageException("incoming message missing 'value' field: " + str(jsonMessage)) field = jsonMessage["field"] if not isinstance(field, str): raise MalformedMessageException("incoming 'field' not a string: " + str(jsonMessage)) field = intern(field) try: fieldDef = getattr(getObjectClass(objectToRead), field) except: raise InvalidFieldException() fieldDef = getattr(getObjectClass(objectToRead), field) if not Decorators.isPropertyToExpose(fieldDef): raise InvalidFieldException() if not Decorators.propertyHasSetter(fieldDef): return InvalidFieldException() try: setter = Decorators.getSetter(field, fieldDef) setter(objectToRead, jsonMessage["value"]) return [{"messageId": jsonMessage["messageId"], "responseType": "OK"}] except Exception as e: return [unexpectedExceptionJson(jsonMessage, Exceptions.wrapException(e).message)]
def handleExecuteMessage(self, jsonMessage, objectToExecuteOn): funImplementation = self.getFunctionForExecuteMessage(jsonMessage, objectToExecuteOn) def callback(result): if isinstance(result, Exception): def creator(): return unexpectedExceptionJson(jsonMessage, Exceptions.wrapException(result).message) self.addResultToQueue(creator) else: def creator(): try: return { "messageId": jsonMessage["messageId"], "responseType": "ExecutionResult", "result": self.outgoingObjectCache.convertResponseToJson(result), } except Exception as e: return unexpectedExceptionJson(jsonMessage, Exceptions.wrapException(e).message) self.addResultToQueue(creator) try: objectArgs = self.convertObjectArgs(jsonMessage["args"]) funImplementation(objectToExecuteOn, callback, objectArgs) return [] except Exception as e: return [unexpectedExceptionJson(jsonMessage, Exceptions.wrapException(e).message)]
def handleJsonMessage(self, incomingJsonMessage): if not isinstance(incomingJsonMessage, dict): raise MalformedMessageException("Incoming message was not a dictionary: %s" % incomingJsonMessage) if not "messageId" in incomingJsonMessage: raise MalformedMessageException("Invalid incoming message id: %s" % incomingJsonMessage) if incomingJsonMessage["messageId"] != self.expectedMessageId: raise MalformedMessageException( "Invalid incoming message id: expected %s, but got %s. %s" % (self.expectedMessageId, incomingJsonMessage["messageId"], incomingJsonMessage) ) try: self.expectedMessageId += 1 if incomingJsonMessage["messageType"] not in self.messageTypeHandlers: raise MalformedMessageException("Invalid incoming messageType") if not "objectDefinition" in incomingJsonMessage: raise MalformedMessageException("No object definition given") if incomingJsonMessage["messageType"] != "ServerFlushObjectIdsBelow": obj = self.extractObjectDefinition(incomingJsonMessage["objectDefinition"]) else: obj = None return self.messageTypeHandlers[incomingJsonMessage["messageType"]](incomingJsonMessage, obj) except MalformedMessageException: raise except Exception as e: return [unexpectedExceptionJson(incomingJsonMessage, Exceptions.wrapException(e).message)]
def handleExecuteMessage(self, jsonMessage, objectToExecuteOn): funImplementation = self.getFunctionForExecuteMessage(jsonMessage, objectToExecuteOn) def callback(result): if isinstance(result, Exception): def creator(): return unexpectedExceptionJson(jsonMessage, Exceptions.wrapException(result).message) self.addResultToQueue(creator) else: def creator(): try: return { "messageId": jsonMessage["messageId"], "responseType": "ExecutionResult", "result": self.outgoingObjectCache.convertResponseToJson(result) } except Exception as e: return unexpectedExceptionJson(jsonMessage, Exceptions.wrapException(e).message) self.addResultToQueue(creator) try: objectArgs = self.convertObjectArgs(jsonMessage['args']) funImplementation(objectToExecuteOn, callback, objectArgs) return [] except Exception as e: return [unexpectedExceptionJson(jsonMessage, Exceptions.wrapException(e).message)]
def handleJsonMessage(self, incomingJsonMessage): if not isinstance(incomingJsonMessage, dict): raise MalformedMessageException( "Incoming message was not a dictionary: %s" % incomingJsonMessage) if not 'messageId' in incomingJsonMessage: raise MalformedMessageException( "Invalid incoming message id: %s" % incomingJsonMessage) if incomingJsonMessage['messageId'] != self.expectedMessageId: raise MalformedMessageException( "Invalid incoming message id: expected %s, but got %s. %s" % (self.expectedMessageId, incomingJsonMessage['messageId'], incomingJsonMessage) ) try: self.expectedMessageId += 1 if incomingJsonMessage['messageType'] not in self.messageTypeHandlers: raise MalformedMessageException("Invalid incoming messageType") if not 'objectDefinition' in incomingJsonMessage: raise MalformedMessageException("No object definition given") if incomingJsonMessage["messageType"] != "ServerFlushObjectIdsBelow": obj = self.extractObjectDefinition(incomingJsonMessage['objectDefinition']) else: obj = None return self.messageTypeHandlers[incomingJsonMessage["messageType"]](incomingJsonMessage, obj) except MalformedMessageException: raise except Exception as e: return [unexpectedExceptionJson(incomingJsonMessage, Exceptions.wrapException(e).message)]
def creator(): try: return { "messageId": jsonMessage["messageId"], "responseType": "ExecutionResult", "result": self.outgoingObjectCache.convertResponseToJson(result), } except Exception as e: return unexpectedExceptionJson(jsonMessage, Exceptions.wrapException(e).message)
def writeToS3(self, bucketname, keyname): """Trigger a task writing this dataset to amazon S3. Returns a WriteToS3Task object. """ if not isinstance(bucketname, str): raise Exceptions.SubscribableWebObjectsException("Expected bucketname to be a string") if not isinstance(keyname, str): raise Exceptions.SubscribableWebObjectsException("Expected keyname to be a string") if self.valueIVC is None: return None task = WriteToS3Task.WriteToS3Task(computedValue=self, bucketname=bucketname, keyname=keyname) task.trigger() return task
def creator(): try: return { "messageId": jsonMessage["messageId"], "responseType": "ExecutionResult", "result": self.outgoingObjectCache.convertResponseToJson(result) } except Exception as e: return unexpectedExceptionJson(jsonMessage, Exceptions.wrapException(e).message)
def convert(self, objectId, serializedBinaryObjectDefinition): import pyfora.Exceptions as PyforaExceptions import pyfora.BinaryObjectRegistryDeserializer as BinaryObjectRegistryDeserializer result = [None] def onConverted(r): result[0] = r t0 = time.time() BinaryObjectRegistryDeserializer.deserializeFromString( base64.b64decode(serializedBinaryObjectDefinition), objectRegistry_[0], convertJsonToObject ) logging.info("Updated object registry in %s seconds.", time.time() - t0) t0 = time.time() try: converter_[0].convert(objectId, objectRegistry_[0], onConverted) except Exception as e: logging.error("Converter raised an exception: %s", traceback.format_exc()) raise Exceptions.InternalError("Unable to convert objectId %s" % objectId) logging.info("Converted to fora in %s seconds", time.time() - t0) assert result[0] is not None if isinstance(result[0], PyforaExceptions.PythonToForaConversionError): return {'isException': True, 'message': result[0].message, 'trace': result[0].trace} if isinstance(result[0], Exception): raise Exceptions.SubscribableWebObjectsException(result[0].message) objectIdToIvc_[objectId] = result[0] return {'objectId': objectId}
def convert(self, objectId, objectIdToObjectDefinition): import pyfora.TypeDescription as TypeDescription import pyfora.Exceptions as PyforaExceptions result = [None] def onConverted(r): result[0] = r t0 = time.time() objectRegistry_[0].objectIdToObjectDefinition.update({ int(k): TypeDescription.deserialize(v) for k, v in objectIdToObjectDefinition.iteritems() }) logging.info("Updated object registry in %s seconds.", time.time() - t0) t0 = time.time() try: converter_[0].convert(objectId, objectRegistry_[0], onConverted) except Exception as e: logging.error("Converter raised an exception: %s", traceback.format_exc()) raise Exceptions.InternalError("Unable to convert objectId %s" % objectId) logging.info("Converted to fora in %s seconds", time.time() - t0) assert result[0] is not None if isinstance(result[0], PyforaExceptions.PythonToForaConversionError): return {'isException': True, 'message': result[0].message, 'trace': result[0].trace} if isinstance(result[0], Exception): raise Exceptions.SubscribableWebObjectsException(result[0].message) objectIdToIvc_[objectId] = result[0] return {'objectId': objectId}
def handleReadMessage(self, jsonMessage, objectToRead): fieldFun = self.getFieldExtractorForReadMessage(jsonMessage, objectToRead) try: return [{ "messageId": jsonMessage["messageId"], "responseType": "ReadResponse", "value": self.outgoingObjectCache.convertResponseToJson(fieldFun(objectToRead)) }] except Exception as e: try: objectType = objectToRead.__location_class__ if ComputedGraph.isLocation(objectToRead) else str(objectToRead) logging.info("converting %s of type %s", fieldFun(objectToRead), objectType) except: pass return [unexpectedExceptionJson(jsonMessage, Exceptions.wrapException(e).message)]
def recomputeSubscription_(self, subscriptionId): if subscriptionId not in self.subscriptionGetters: return try: newValue = self.subscriptionGetters[subscriptionId]() except Exception as e: e = Exceptions.wrapException(e) newValue = e if subscriptionId not in self.subscriptionValues: self.subscriptionValues[subscriptionId] = newValue return existingValue = self.subscriptionValues[subscriptionId] if existingValue != newValue: self.changedSubscriptions.add(subscriptionId) self.subscriptionValues[subscriptionId] = newValue
def handleAssignMessage(self, jsonMessage, objectToRead): if 'field' not in jsonMessage: raise MalformedMessageException( "incoming message missing 'field' field: " + str(jsonMessage) ) if 'value' not in jsonMessage: raise MalformedMessageException( "incoming message missing 'value' field: " + str(jsonMessage) ) field = jsonMessage['field'] if not isinstance(field, str): raise MalformedMessageException( "incoming 'field' not a string: " + str(jsonMessage)) field = intern(field) try: fieldDef = getattr(getObjectClass(objectToRead), field) except: raise InvalidFieldException() fieldDef = getattr(getObjectClass(objectToRead), field) if not Decorators.isPropertyToExpose(fieldDef): raise InvalidFieldException() if not Decorators.propertyHasSetter(fieldDef): return InvalidFieldException() try: setter = Decorators.getSetter(field, fieldDef) setter(objectToRead, jsonMessage['value']) return [{ "messageId": jsonMessage["messageId"], "responseType": "OK" }] except Exception as e: return [unexpectedExceptionJson(jsonMessage, Exceptions.wrapException(e).message)]
def handleReadMessage(self, jsonMessage, objectToRead): fieldFun = self.getFieldExtractorForReadMessage(jsonMessage, objectToRead) try: return [ { "messageId": jsonMessage["messageId"], "responseType": "ReadResponse", "value": self.outgoingObjectCache.convertResponseToJson(fieldFun(objectToRead)), } ] except Exception as e: try: objectType = ( objectToRead.__location_class__ if ComputedGraph.isLocation(objectToRead) else str(objectToRead) ) logging.info("converting %s of type %s", fieldFun(objectToRead), objectType) except: pass return [unexpectedExceptionJson(jsonMessage, Exceptions.wrapException(e).message)]
def aFunctionThrowingASpecificException(self, jsonArgs): raise Exceptions.SubscribableWebObjectsException( "swo exception: function call")
def aValueThrowingASpecificException(self): raise Exceptions.SubscribableWebObjectsException( "swo exception: getter")
def setAValueThrowingASpecificException(self, value): raise Exceptions.SubscribableWebObjectsException( "swo exception: setter")
def creator(): return unexpectedExceptionJson(jsonMessage, Exceptions.wrapException(result).message)
def raiseException(): guid = uuid.uuid4() logging.error("%s of type %s is not valid json. guid = %s", jsonCandidate, type(jsonCandidate), guid) raise Exceptions.SubscribableWebObjectsException("result was not valid json. Guid = %s" % guid)