def extractObjectDefinition(self, objDefJson): if 'objectId_' in objDefJson or 'objectDefinition_' in objDefJson: return self.convertObjectArgs(objDefJson) if 'type' not in objDefJson or 'args' not in objDefJson: raise MalformedMessageException("Malformed object definition given") objType = objDefJson['type'] objectArgs = objDefJson['args'] objectArgs = self.convertObjectArgs(objectArgs) if objType not in AllObjectClassesToExpose.classMap: raise InvalidObjectDefinitionException("Unknown object type") try: objectCls = AllObjectClassesToExpose.classMap[objType] result = objectCls(objectArgs) if not ComputedGraph.isLocation(result): result.__dict__['objectDefinition_'] = objDefJson return result except Exceptions.SubscribableWebObjectsException as e: raise InvalidObjectDefinitionException(e.message)
def extractObjectDefinition(self, objDefJson): if "objectId_" in objDefJson or "objectDefinition_" in objDefJson: return self.convertObjectArgs(objDefJson) if "type" not in objDefJson or "args" not in objDefJson: raise MalformedMessageException("Malformed object definition given") objType = objDefJson["type"] objectArgs = objDefJson["args"] objectArgs = self.convertObjectArgs(objectArgs) if objType not in AllObjectClassesToExpose.classMap: raise InvalidObjectDefinitionException("Unknown object type") try: objectCls = AllObjectClassesToExpose.classMap[objType] result = objectCls(objectArgs) if not ComputedGraph.isLocation(result): result.__dict__["objectDefinition_"] = objDefJson return result except Exceptions.SubscribableWebObjectsException as e: raise InvalidObjectDefinitionException(e.message)
def convertResponseToJson(self, jsonCandidate): try: 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) if jsonCandidate is None: return jsonCandidate if isinstance(jsonCandidate, (int,str,unicode,float,bool,long)): return jsonCandidate if isinstance(jsonCandidate, (list, tuple)): return [self.convertResponseToJson(r) for r in jsonCandidate] if isinstance(jsonCandidate, dict): newDict = {} for k,v in jsonCandidate.iteritems(): if not isinstance(k,str): raiseException() newDict[k] = self.convertResponseToJson(v) return newDict objDefPopulated = False try: if ComputedGraph.isLocation(jsonCandidate): if jsonCandidate in self.objectToIdCache_: objDef = { 'objectId_': self.lookupObjectId(jsonCandidate) } else: objDef = { "objectId_": self.lookupObjectId(jsonCandidate), "objectDefinition_": { 'type': AllObjectClassesToExpose.typenameFromType( ComputedGraph.getLocationTypeFromLocation(jsonCandidate) ), 'args': jsonCandidate.__reduce__()[1][0] } } objDefPopulated = True else: objDef = jsonCandidate.objectDefinition_ objDefPopulated = True except AttributeError: objDefPopulated = False if objDefPopulated: return self.convertResponseToJson(objDef) raiseException() except: logging.error("%s of type %s is not valid json.", jsonCandidate, type(jsonCandidate)) raise
def convertResponseToJson(self, jsonCandidate): try: 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) if jsonCandidate is None: return jsonCandidate if isinstance(jsonCandidate, (int, str, unicode, float, bool, long)): return jsonCandidate if isinstance(jsonCandidate, (list, tuple)): return [self.convertResponseToJson(r) for r in jsonCandidate] if isinstance(jsonCandidate, dict): newDict = {} for k, v in jsonCandidate.iteritems(): if not isinstance(k, str): raiseException() newDict[k] = self.convertResponseToJson(v) return newDict objDefPopulated = False try: if ComputedGraph.isLocation(jsonCandidate): if jsonCandidate in self.objectToIdCache_: objDef = {"objectId_": self.lookupObjectId(jsonCandidate)} else: objDef = { "objectId_": self.lookupObjectId(jsonCandidate), "objectDefinition_": { "type": AllObjectClassesToExpose.typenameFromType( ComputedGraph.getLocationTypeFromLocation(jsonCandidate) ), "args": jsonCandidate.__reduce__()[1][0], }, } objDefPopulated = True else: objDef = jsonCandidate.objectDefinition_ objDefPopulated = True except AttributeError: objDefPopulated = False if objDefPopulated: return self.convertResponseToJson(objDef) raiseException() except: logging.error("%s of type %s is not valid json.", jsonCandidate, type(jsonCandidate)) raise
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 validateComputedValueArgs(args): """check that every argument to a ComputedValue is either an ImplValContainer or another ComputedValue""" if not args: return False if not isinstance(args, tuple): return False for ix, a in enumerate(args): if ComputedGraph.isLocation(a): if not issubclass(a.__location_class__, ComputedValue): logging.error("Failed validation of args[%s].__location_class__ = '%s'", ix, a.__location_class__) return False else: if not isinstance(a, (ImplValContainer_, long, int, str, bool)): logging.error("Failed validation of args[%s].__class__ = '%s'", ix, a.__class__) return False return True
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 isComputedValue(o): return ComputedGraph.isLocation(o) and issubclass(o.__location_class__, ComputedValue)
def getObjectClass(o): if ComputedGraph.isLocation(o): return ComputedGraph.getLocationTypeFromLocation(o) return o.__class__