def generatePythonWrapperForClass(lineWriter, className, classObj): lineWriter("") lineWriter("class %s(WebObjectBase):" % className) lineWriter = addSpaces(lineWriter) lineWriter("def __init__(self, sessionState, inArgs=None):") lineWriter(' super(%s, self).__init__(sessionState, inArgs)' % className) lineWriter(" self.objectDefinition_.update({") lineWriter(' "type": "%s",' % className) lineWriter(' })') lineWriter("") for memberName, memberDef in getClassMember(classObj).iteritems(): if Decorators.isPropertyToExpose(memberDef): if Decorators.propertyHasSetter(memberDef): lineWriter("def set_%s(self, value, callbacks):" % memberName) lineWriter(" self.set('%s', value, callbacks)" % memberName) lineWriter('') lineWriter("def get_%s(self, callbacks):" % memberName) lineWriter(" self.get('%s', callbacks)" % memberName) lineWriter('') lineWriter("def subscribe_%s(self, callbacks):" % memberName) lineWriter(" self.subscribe('%s', callbacks)" % memberName) lineWriter('') if Decorators.isFunctionToExpose(memberDef): lineWriter("def %s(self, args, callbacks):" % memberName) lineWriter(" self.invoke('%s', args, callbacks)" % memberName) lineWriter('')
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 getFunctionForExecuteMessage(self, jsonMessage, objectToRead): if "field" not in jsonMessage: raise MalformedMessageException("missing 'field'") field = jsonMessage["field"] if not isinstance(field, str): raise MalformedMessageException("fieldname was not a string") field = intern(field) try: funDef = getattr(getObjectClass(objectToRead), field) except: raise InvalidFieldException() funDef = getattr(getObjectClass(objectToRead), field) if not Decorators.isFunctionToExpose(funDef): raise InvalidFunctionException() if Decorators.functionExpectsCallback(funDef): if Decorators.functionExpectsExpandedArgs(funDef): def caller(x, callback, arg): if isinstance(arg, list): return getattr(x, field)(callback, *arg) else: return getattr(x, field)(callback, **arg) return caller else: return lambda x, callback, *args: getattr(x, field)(callback, *args) else: if Decorators.functionExpectsExpandedArgs(funDef): def caller(x, callback, arg): try: if isinstance(arg, list): callback(getattr(x, field)(*arg)) else: callback(getattr(x, field)(**arg)) except Exception as e: callback(e) return caller else: def caller(x, callback, *args): try: callback(getattr(x, field)(*args)) except Exception as e: callback(e) return caller
def getFunctionForExecuteMessage(self, jsonMessage, objectToRead): if 'field' not in jsonMessage: raise MalformedMessageException("missing 'field'") field = jsonMessage['field'] if not isinstance(field, str): raise MalformedMessageException("fieldname was not a string") field = intern(field) try: funDef = getattr(getObjectClass(objectToRead), field) except: raise InvalidFieldException() funDef = getattr(getObjectClass(objectToRead), field) if not Decorators.isFunctionToExpose(funDef): raise InvalidFunctionException() if Decorators.functionExpectsCallback(funDef): if Decorators.functionExpectsExpandedArgs(funDef): def caller(x, callback, arg): if isinstance(arg, list): return getattr(x,field)(callback, *arg) else: return getattr(x,field)(callback, **arg) return caller else: return lambda x, callback, *args: getattr(x, field)(callback, *args) else: if Decorators.functionExpectsExpandedArgs(funDef): def caller(x, callback, arg): try: if isinstance(arg, list): callback(getattr(x,field)(*arg)) else: callback(getattr(x,field)(**arg)) except Exception as e: callback(e) return caller else: def caller(x, callback, *args): try: callback(getattr(x, field)(*args)) except Exception as e: callback(e) return caller
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 getFieldExtractorForReadMessage(self, jsonMessage, objectToRead): if 'field' not in jsonMessage: raise MalformedMessageException("missing 'field'") field = jsonMessage['field'] if not isinstance(field, str): raise MalformedMessageException("fieldname not a string") field = intern(field) try: fieldDef = getattr(getObjectClass(objectToRead), field) except: raise InvalidFieldException() if not Decorators.isPropertyToExpose(fieldDef): raise InvalidFieldException() def getter(x): return getattr(x, field) return getter
def getFieldExtractorForReadMessage(self, jsonMessage, objectToRead): if "field" not in jsonMessage: raise MalformedMessageException("missing 'field'") field = jsonMessage["field"] if not isinstance(field, str): raise MalformedMessageException("fieldname not a string") field = intern(field) try: fieldDef = getattr(getObjectClass(objectToRead), field) except: raise InvalidFieldException() if not Decorators.isPropertyToExpose(fieldDef): raise InvalidFieldException() def getter(x): return getattr(x, field) return getter
def generateCoffeeWrapperForClass(lineWriter, className, classObj): lineWriter("") lineWriter("class %s" % className) lineWriter = addSpaces(lineWriter) lineWriter("constructor: (inArgs) -> ") lineWriter(" @objectDefinition_ = ") lineWriter(' type: "%s"' % className) lineWriter(' args: if inArgs? then inArgs else {}') lineWriter("") #create a wrapper function around our toJSON function that sets a module-level flag #this prevents users from accidentally allocating objectIDs and then not sending them over #the wire to the server lineWriter("toJSONWireFormat: () => ") lineWriter(" isConvertingToWireFormat = true") lineWriter(" data = JSON.parse(JSON.stringify(@))") lineWriter(" isConvertingToWireFormat = false") lineWriter(" data") #create a 'toJSON' function that allows the object to be serialized to JSON #This function gets run whenever we call 'JSON.stringify' or 'socket.emit' lineWriter("toJSON: () => ") lineWriter(" if not useObjectIds or not isConvertingToWireFormat") lineWriter(" return {objectDefinition_: @objectDefinition_}") lineWriter(" if usableObjectId(@objectId_)") lineWriter(" return {objectId_: @objectId_}") lineWriter(" @objectId_ = allocateObjectId()") lineWriter(" {objectDefinition_: @objectDefinition_, objectId_: @objectId_}") lineWriter("") for memberName, memberDef in getClassMember(classObj).iteritems(): if Decorators.isPropertyToExpose(memberDef): if Decorators.propertyHasSetter(memberDef): lineWriter("set_%s: (value, callbacks) -> " % memberName) lineWriter(" req = ") lineWriter(" objectDefinition: @toJSONWireFormat()") lineWriter(" messageType: 'Assign'") lineWriter(" field: '%s'" % memberName) lineWriter(" value: value") lineWriter(" jsonInterface.request req, (response) -> ") lineWriter(" if response.responseType == 'OK'") lineWriter(" callbacks.onSuccess()") lineWriter(" else") lineWriter(" if callbacks.onFailure? then callbacks.onFailure (response)") lineWriter(" updateObjectIdThreshold()") lineWriter("get_%s: (callbacks) -> " % memberName) lineWriter(" req = ") lineWriter(" objectDefinition: @toJSONWireFormat()") lineWriter(" messageType: 'Read'") lineWriter(" field: '%s'" % memberName) lineWriter(" jsonInterface.request req, (response) -> ") lineWriter(" if response.responseType == 'ReadResponse'") lineWriter(" callbacks.onSuccess incomingObjectCache.convertJsonToObject(response.value)") lineWriter(" else") lineWriter(" if callbacks.onFailure? then callbacks.onFailure (response)") lineWriter(" updateObjectIdThreshold()") lineWriter("subscribe_%s: (callbacks) -> " % memberName) lineWriter(" req = ") lineWriter(" objectDefinition: @toJSONWireFormat()") lineWriter(" messageType: 'Subscribe'") lineWriter(" field: '%s'" % memberName) lineWriter(" jsonInterface.request req, (response) -> ") lineWriter(" if response.responseType == 'SubscribeResponse'") lineWriter(" callbacks.onSuccess incomingObjectCache.convertJsonToObject(response.value)") lineWriter(" else if response.responseType == 'ValueChanged'") lineWriter(" callbacks.onChanged incomingObjectCache.convertJsonToObject(response.value)") lineWriter(" else") lineWriter(" if callbacks.onFailure? then callbacks.onFailure (response)") lineWriter(" updateObjectIdThreshold()") if Decorators.isFunctionToExpose(memberDef): lineWriter("%s: (args, callbacks) -> " % memberName) lineWriter(" req = ") lineWriter(" objectDefinition: @toJSONWireFormat()") lineWriter(" messageType: 'Execute'") lineWriter(" args: args") lineWriter(" field: '%s'" % memberName) lineWriter(" jsonInterface.request req, (response) -> ") lineWriter(" if response.responseType == 'ExecutionResult'") lineWriter(" callbacks.onSuccess incomingObjectCache.convertJsonToObject(response.result)") lineWriter(" else") lineWriter(" if callbacks.onFailure? then callbacks.onFailure (response)") lineWriter(" updateObjectIdThreshold()")
def generateCoffeeWrapperForClass(lineWriter, className, classObj): lineWriter("") lineWriter("class %s" % className) lineWriter = addSpaces(lineWriter) lineWriter("constructor: (inArgs) -> ") lineWriter(" @objectDefinition_ = ") lineWriter(' type: "%s"' % className) lineWriter(' args: if inArgs? then inArgs else {}') lineWriter("") #create a wrapper function around our toJSON function that sets a module-level flag #this prevents users from accidentally allocating objectIDs and then not sending them over #the wire to the server lineWriter("toJSONWireFormat: () => ") lineWriter(" isConvertingToWireFormat = true") lineWriter(" data = JSON.parse(JSON.stringify(@))") lineWriter(" isConvertingToWireFormat = false") lineWriter(" data") #create a 'toJSON' function that allows the object to be serialized to JSON #This function gets run whenever we call 'JSON.stringify' or 'socket.emit' lineWriter("toJSON: () => ") lineWriter(" if not useObjectIds or not isConvertingToWireFormat") lineWriter(" return {objectDefinition_: @objectDefinition_}") lineWriter(" if usableObjectId(@objectId_)") lineWriter(" return {objectId_: @objectId_}") lineWriter(" @objectId_ = allocateObjectId()") lineWriter( " {objectDefinition_: @objectDefinition_, objectId_: @objectId_}") lineWriter("") for memberName, memberDef in getClassMember(classObj).iteritems(): if Decorators.isPropertyToExpose(memberDef): if Decorators.propertyHasSetter(memberDef): lineWriter("set_%s: (value, callbacks) -> " % memberName) lineWriter(" req = ") lineWriter(" objectDefinition: @toJSONWireFormat()") lineWriter(" messageType: 'Assign'") lineWriter(" field: '%s'" % memberName) lineWriter(" value: value") lineWriter(" jsonInterface.request req, (response) -> ") lineWriter(" if response.responseType == 'OK'") lineWriter(" callbacks.onSuccess()") lineWriter(" else") lineWriter( " if callbacks.onFailure? then callbacks.onFailure (response)" ) lineWriter(" updateObjectIdThreshold()") lineWriter("get_%s: (callbacks) -> " % memberName) lineWriter(" req = ") lineWriter(" objectDefinition: @toJSONWireFormat()") lineWriter(" messageType: 'Read'") lineWriter(" field: '%s'" % memberName) lineWriter(" jsonInterface.request req, (response) -> ") lineWriter(" if response.responseType == 'ReadResponse'") lineWriter( " callbacks.onSuccess incomingObjectCache.convertJsonToObject(response.value)" ) lineWriter(" else") lineWriter( " if callbacks.onFailure? then callbacks.onFailure (response)" ) lineWriter(" updateObjectIdThreshold()") lineWriter("subscribe_%s: (callbacks) -> " % memberName) lineWriter(" req = ") lineWriter(" objectDefinition: @toJSONWireFormat()") lineWriter(" messageType: 'Subscribe'") lineWriter(" field: '%s'" % memberName) lineWriter(" jsonInterface.request req, (response) -> ") lineWriter( " if response.responseType == 'SubscribeResponse'") lineWriter( " callbacks.onSuccess incomingObjectCache.convertJsonToObject(response.value)" ) lineWriter( " else if response.responseType == 'ValueChanged'") lineWriter( " callbacks.onChanged incomingObjectCache.convertJsonToObject(response.value)" ) lineWriter(" else") lineWriter( " if callbacks.onFailure? then callbacks.onFailure (response)" ) lineWriter(" updateObjectIdThreshold()") if Decorators.isFunctionToExpose(memberDef): lineWriter("%s: (args, callbacks) -> " % memberName) lineWriter(" req = ") lineWriter(" objectDefinition: @toJSONWireFormat()") lineWriter(" messageType: 'Execute'") lineWriter(" args: args") lineWriter(" field: '%s'" % memberName) lineWriter(" jsonInterface.request req, (response) -> ") lineWriter(" if response.responseType == 'ExecutionResult'") lineWriter( " callbacks.onSuccess incomingObjectCache.convertJsonToObject(response.result)" ) lineWriter(" else") lineWriter( " if callbacks.onFailure? then callbacks.onFailure (response)" ) lineWriter(" updateObjectIdThreshold()")