Module Import alias identifier Import alias identifier FunctionDef arguments args NameParam defaults body Return Lambda arguments args NameParam defaults Call NameLoad BinOpAdd Str NameLoad decorator_list FunctionDef arguments args NameParam defaults body If CompareIs NameLoad NameLoad body Return Dict Assign NameStore Dict For NameStore Call NameLoad Call AttributeLoad NameLoad attr body If CompareIsNot NameLoad NameLoad body For TupleStore NameStore NameStore Call AttributeLoad AttributeLoad NameLoad attr attr body Assign SubscriptStore NameLoad Index NameLoad NameLoad Return NameLoad decorator_list Assign NameStore Str FunctionDef arguments args NameParam NameParam NameParam defaults body Expr Call NameLoad Str Expr Call NameLoad BinOpMod Str NameLoad Assign NameStore Call NameLoad NameLoad Expr Call NameLoad Str Expr Call NameLoad BinOpMod Str NameLoad Expr Call NameLoad Str Expr Call NameLoad BinOpMod Str NameLoad Expr Call NameLoad Str Expr Call NameLoad Str For TupleStore NameStore NameStore Call AttributeLoad Call NameLoad NameLoad attr body If Call AttributeLoad NameLoad attr NameLoad body If Call AttributeLoad NameLoad attr NameLoad body Expr Call NameLoad BinOpMod Str NameLoad Expr Call NameLoad BinOpMod Str NameLoad Expr Call NameLoad Str Expr Call NameLoad BinOpMod Str NameLoad Expr Call NameLoad BinOpMod Str NameLoad Expr Call NameLoad Str Expr Call NameLoad BinOpMod Str NameLoad Expr Call NameLoad BinOpMod Str NameLoad Expr Call NameLoad Str If Call AttributeLoad NameLoad attr NameLoad body Expr Call NameLoad BinOpMod Str NameLoad Expr Call NameLoad BinOpMod Str NameLoad Expr Call NameLoad Str decorator_list FunctionDef arguments args NameParam defaults body Expr Str Assign NameStore Call AttributeLoad NameLoad attr Assign NameStore Lambda arguments args NameParam defaults Call AttributeLoad NameLoad attr BinOpAdd Call AttributeLoad BinOpAdd Str NameLoad attr Str Expr Call AttributeLoad NameLoad attr BinOpAdd NameLoad Str For TupleStore NameStore NameStore Call AttributeLoad NameLoad attr body Expr Call NameLoad Lambda arguments args NameParam defaults Call AttributeLoad NameLoad attr BinOpAdd Call AttributeLoad NameLoad attr Str NameLoad NameLoad Expr Call AttributeLoad NameLoad attr Str For TupleStore NameStore NameStore Call AttributeLoad NameLoad attr body Expr Call AttributeLoad NameLoad attr BinOpMod Str TupleLoad NameLoad NameLoad Expr Call AttributeLoad NameLoad attr Str Return Call AttributeLoad NameLoad attr decorator_list If CompareEq NameLoad Str body Import alias identifier Print Call NameLoad AttributeLoad NameLoad attr
N/A N/A ufora.BackendGateway.SubscribableWebObjects.Decorators Decorators N/A cStringIO StringIO addSpaces N/A N/A lineWriter N/A N/A N/A N/A N/A N/A s N/A N/A lineWriter N/A s N/A getClassMember N/A N/A cls N/A N/A N/A N/A cls object N/A N/A N/A N/A prior N/A N/A c N/A reversed N/A N/A cls mro N/A N/A N/A c object N/A N/A N/A x val N/A N/A N/A c __dict__ iteritems N/A N/A N/A prior N/A x val N/A prior N/A N/A headerText 
"NOTE: THIS IS GENERATED CODE. DO NOT EDIT THIS FILE OR CHECK IT INTO SOURCE CONTROL!"

import json

class WebObjectBase(object):
    def __init__(self, sessionState, inArgs=None):
        self.objectId_ = None
        self.sessionState = sessionState
        self.objectDefinition_ = {
            "args": inArgs if inArgs is not None else {}
        }

    def toJSONWireFormat(self):
        self.sessionState.isConvertingToWireFormat = True
        memoizedJson = self.toMemoizedJSON()
        def encoder(obj):
            if isinstance(obj, WebObjectBase):
                return obj.toMemoizedJSON()
            return obj

        data = json.loads(json.dumps(memoizedJson, default=encoder))
        self.sessionState.isConvertingToWireFormat = False
        return data

    def toMemoizedJSON(self):
        if not self.sessionState.useObjectIds or not self.sessionState.isConvertingToWireFormat:
            return {"objectDefinition_": self.objectDefinition_}
        if self.sessionState.usableObjectId(self.objectId_):
            return {"objectId_": self.objectId_}
        self.objectId_ = self.sessionState.allocateObjectId()
        return {"objectDefinition_": self.objectDefinition_, "objectId_": self.objectId_}

    def toJSON(self, objectId):
        return {"objectDefinition_": self.objectDefinition_, "objectId_": objectId}

    def get(self, field, callbacks):
        req = {
            'objectDefinition': self.toJSONWireFormat(),
            'messageType': 'Read',
            'field': field
            }
        def responseCallback(response, callbacks):
            if response['responseType'] == 'ReadResponse':
                self.triggerSuccessCallback(
                    callbacks,
                    self.sessionState.convertJsonToObject(response['value'])
                    )
            else:
                self.triggerFailureCallback(callbacks, response)
        self.sessionState.jsonInterface.send(req, lambda response: responseCallback(response, callbacks))
        self.sessionState.updateObjectIdThreshold()

    def subscribe(self, field, callbacks):
        req = {
            'objectDefinition': self.toJSONWireFormat(),
            'messageType': 'Subscribe',
            'field': field
            }
        def responseCallback(response, callbacks):
            if response['responseType'] == 'SubscribeResponse':
                self.triggerSuccessCallback(
                    callbacks,
                    self.sessionState.convertJsonToObject(response['value'])
                    )
            elif response['responseType'] == 'ValueChanged':
                self.triggerChangedCallback(
                    callbacks,
                    self.sessionState.convertJsonToObject(response['value'])
                    )
            else:
                self.triggerFailureCallback(callbacks, response)
        self.sessionState.jsonInterface.send(req, lambda response: responseCallback(response, callbacks))
        self.sessionState.updateObjectIdThreshold()

    def set(self, attributeName, value, callbacks):
        req = {
            'objectDefinition': self.toJSONWireFormat(),
            'messageType': 'Assign',
            'field': attributeName,
            'value': value
            }
        def responseCallback(response, callbacks):
            if response['responseType'] == 'OK':
                self.triggerSuccessCallback(callbacks)
            else:
                self.triggerFailureCallback(callbacks, response)
        self.sessionState.jsonInterface.send(req, lambda response: responseCallback(response, callbacks))
        self.sessionState.updateObjectIdThreshold()

    def invoke(self, name, args, callbacks):
        req = {
            'objectDefinition': self.toJSONWireFormat(),
            'messageType': 'Execute',
            'args': args,
            'field': name
            }
        def responseCallback(response, callbacks):
            if response['responseType'] == 'ExecutionResult':
                self.triggerSuccessCallback(
                    callbacks,
                    self.sessionState.convertJsonToObject(response['result'])
                    )
            else:
                self.triggerFailureCallback(callbacks, response)
        self.sessionState.jsonInterface.send(req, lambda response: responseCallback(response, callbacks))
        self.sessionState.updateObjectIdThreshold()

    @classmethod
    def triggerSuccessCallback(cls, callbacks, *args, **kwargs):
        return cls.triggerCallback(callbacks, 'onSuccess', *args, **kwargs)

    @classmethod
    def triggerFailureCallback(cls, callbacks, *args, **kwargs):
        return cls.triggerCallback(callbacks, 'onFailure', *args, **kwargs)

    @classmethod
    def triggerChangedCallback(cls, callbacks, *args, **kwargs):
        return cls.triggerCallback(callbacks, 'onChanged', *args, **kwargs)

    @staticmethod
    def triggerCallback(callbacks, name, *args, **kwargs):
        callback = getattr(callbacks, name, None) or callbacks.get(name)
        if callback:
            return callback(*args, **kwargs)



class SessionState(object):
    def __init__(self, maxObjectIds, useObjectIds, jsonInterface):
        self.useObjectIds = useObjectIds
        self.maxObjectIds = maxObjectIds
        self.minValidObjectId = 0
        self.curObjectId = 0
        self.jsonInterface = jsonInterface
        self.isConvertingToWireFormat = False
        self.objectsById = {}
        self.objectIdsMemoized = []
        self.timesCleared = 0

    def usableObjectId(self, objId):
        if objId is None:
            return False
        return objId >= self.minValidObjectId

    def allocateObjectId(self):
        self.curObjectId = self.curObjectId + 1
        return self.curObjectId - 1

    def updateObjectIdThreshold(self, forceFlush=False):
        if self.curObjectId > self.minValidObjectId + self.maxObjectIds or forceFlush:
            self.minValidObjectId = self.curObjectId
            req = {
                'messageType': 'ServerFlushObjectIdsBelow',
                'value': self.minValidObjectId,
                'objectDefinition': {}
                }
            self.jsonInterface.send(
                req,
                lambda x: x
                )

    def clearObjectIds(self):
        return self.updateObjectIdThreshold(True)

    def convertJsonToObject(self, val):
        if val is None:
            return

        if isinstance(val, list):
            return tuple(self.convertJsonToObject(x) for x in val)

        if not isinstance(val, dict):
            return val

        objectId = val.get('objectId_')
        if objectId is None:
            return {k: self.convertJsonToObject(v) for k, v in val.iteritems()}

        definition = val.get('objectDefinition_')
        if definition is None:
            objectToReturn = self.objectsById.get(objectId)
            if objectToReturn is None:
                raise Exception("Received an undefined object id: %s from the server" % objectId)
            return objectToReturn

        objectToReturn = (classes[definition['type']])(self, self.convertJsonToObject(definition['args']))
        self.objectsById[objectId] = objectToReturn
        self.objectIdsMemoized.append(objectId)
        return objectToReturn


    def clearObjectIdsBelow(self, minValidObjectId):
        self.timesCleared += 1
        self.objectIdsMemoized.sort()

        while len(self.objectIdsMemoized) > 0 and self.objectIdsMemoized[0] < minValidObjectId:
            objId = self.objectIdsMemoized.pop(0)
            del self.objectsById[objId]

class WebObjectFactory(object):
    def constructor(self, typeName):
        def construct(*args):
            return classes[typeName](self.sessionState, *args)
        return construct

    def __init__(self, jsonInterface, maxObjectIds=10000, useObjectIds=True):
        self.jsonInterface = jsonInterface
        self.sessionState = SessionState(maxObjectIds, useObjectIds, jsonInterface)

        self.jsonInterface.on('special_message', self.onSpecialMessage)
        self.__dict__.update({cls: self.constructor(cls) for cls in classes.iterkeys()})

    def onSpecialMessage(self, msg):
        if msg["responseType"] == 'ClientFlushObjectIdsBelow':
            self.sessionState.clearObjectIdsBelow(msg["value"])

    def flushObjectIds(self):
        return self.sessionState.clearObjectIds()

    def getJsonInterface(self):
        return self.jsonInterface
 generatePythonWrapperForClass N/A N/A lineWriter className classObj N/A N/A N/A N/A lineWriter N/A N/A lineWriter N/A class %s(WebObjectBase): className N/A lineWriter N/A addSpaces lineWriter N/A N/A lineWriter def __init__(self, sessionState, inArgs=None): N/A N/A lineWriter N/A     super(%s, self).__init__(sessionState, inArgs) className N/A N/A lineWriter     self.objectDefinition_.update({ N/A N/A lineWriter N/A         "type": "%s", className N/A N/A lineWriter     }) N/A N/A lineWriter N/A N/A memberName memberDef N/A N/A N/A getClassMember classObj iteritems N/A N/A N/A N/A Decorators isPropertyToExpose memberDef N/A N/A N/A N/A Decorators propertyHasSetter memberDef N/A N/A N/A lineWriter N/A def set_%s(self, value, callbacks): memberName N/A N/A lineWriter N/A     self.set('%s', value, callbacks) memberName N/A N/A lineWriter N/A N/A lineWriter N/A def get_%s(self, callbacks): memberName N/A N/A lineWriter N/A     self.get('%s', callbacks) memberName N/A N/A lineWriter N/A N/A lineWriter N/A def subscribe_%s(self, callbacks): memberName N/A N/A lineWriter N/A     self.subscribe('%s', callbacks) memberName N/A N/A lineWriter N/A N/A N/A Decorators isFunctionToExpose memberDef N/A N/A N/A lineWriter N/A def %s(self, args, callbacks): memberName N/A N/A lineWriter N/A     self.invoke('%s', args, callbacks) memberName N/A N/A lineWriter N/A generatePythonWrapper N/A N/A classMap N/A N/A N/A Returns the text of a function 'createPythonInterface' N/A outBuffer N/A N/A StringIO StringIO N/A lineWriter N/A N/A N/A line N/A N/A N/A outBuffer write N/A N/A N/A N/A line rstrip N/A N/A N/A outBuffer write N/A headerText N/A N/A className classObj N/A N/A classMap iteritems N/A N/A N/A generatePythonWrapperForClass N/A N/A N/A line N/A N/A N/A outBuffer write N/A N/A N/A line rstrip className classObj N/A N/A N/A outBuffer write 
classes = {
 N/A N/A className classObj N/A N/A classMap iteritems N/A N/A N/A N/A outBuffer write N/A     '%s': %s,
 N/A className className N/A N/A N/A outBuffer write     }
 N/A N/A N/A outBuffer getvalue N/A N/A N/A __name__ __main__ N/A N/A ufora.BackendGateway.SubscribableWebObjects.AllObjectClassesToExpose AllObjectClassesToExpose N/A N/A generatePythonWrapper N/A AllObjectClassesToExpose classMap