Example #1
0
 def jellyToAO(self, obj):
     """I turn an object into an AOT and return it."""
     objType = type(obj)
     self.stack.append(repr(obj))
     if objType in _SIMPLE_BUILTINS:
         retval = obj
     elif objType is types.MethodType:
         retval = InstanceMethod(obj.im_func.__name__, reflect.qual(obj.im_class),
                                 self.jellyToAO(obj.im_self))
     elif objType is types.ModuleType:
         retval = Module(obj.__name__)
     elif objType is types.ClassType:
         retval = Class(reflect.qual(obj))
     elif issubclass(objType, type):
         retval = Class(reflect.qual(obj))
     elif objType is types.FunctionType:
         retval = Function(reflect.fullFuncName(obj))
     else: #mutable! gotta watch for refs.
         if self.prepared.has_key(id(obj)):
             oldRef = self.prepared[id(obj)]
             if oldRef.refnum:
                 key = oldRef.refnum
             else:
                 self._ref_id = self._ref_id + 1
                 key = self._ref_id
                 oldRef.setRef(key)
             return Deref(key)
         retval = Ref()
         self.prepareForRef(retval, obj)
         if objType is types.ListType:
             retval.setObj(map(self.jellyToAO, obj)) #hah!
         elif objType is types.TupleType:
             retval.setObj(tuple(map(self.jellyToAO, obj)))
         elif objType is types.DictionaryType:
             d = {}
             for k,v in obj.items():
                 d[self.jellyToAO(k)] = self.jellyToAO(v)
             retval.setObj(d)
         elif objType is types.InstanceType:
             if hasattr(obj, "__getstate__"):
                 state = self.jellyToAO(obj.__getstate__())
             else:
                 state = self.jellyToAO(obj.__dict__)
             retval.setObj(Instance(reflect.qual(obj.__class__), state))
         elif copy_reg.dispatch_table.has_key(objType):
             unpickleFunc, state = copy_reg.dispatch_table[objType](obj)
             retval.setObj(Copyreg( reflect.fullFuncName(unpickleFunc),
                                    self.jellyToAO(state)))
         else:
             raise "Unsupported type: %s" % objType.__name__
     del self.stack[-1]
     return retval
Example #2
0
    def jellyToAO(self, obj):
        """I turn an object into an AOT and return it."""
        objType = type(obj)
        self.stack.append(repr(obj))

        #immutable: We don't care if these have multiple refs!
        if objType in _SIMPLE_BUILTINS:
            retval = obj

        elif objType is types.MethodType:
            # TODO: make methods 'prefer' not to jelly the object internally,
            # so that the object will show up where it's referenced first NOT
            # by a method.
            retval = InstanceMethod(obj.im_func.__name__,
                                    reflect.qual(obj.im_class),
                                    self.jellyToAO(obj.im_self))

        elif objType is types.ModuleType:
            retval = Module(obj.__name__)

        elif objType is types.ClassType:
            retval = Class(reflect.qual(obj))

        elif issubclass(objType, type):
            retval = Class(reflect.qual(obj))

        elif objType is types.FunctionType:
            retval = Function(reflect.fullFuncName(obj))

        else:  #mutable! gotta watch for refs.

            #Marmalade had the nicety of being able to just stick a 'reference' attribute
            #on any Node object that was referenced, but in AOT, the referenced object
            #is *inside* of a Ref call (Ref(num, obj) instead of
            #<objtype ... reference="1">). The problem is, especially for built-in types,
            #I can't just assign some attribute to them to give them a refnum. So, I have
            #to "wrap" a Ref(..) around them later -- that's why I put *everything* that's
            #mutable inside one. The Ref() class will only print the "Ref(..)" around an
            #object if it has a Reference explicitly attached.

            if self.prepared.has_key(id(obj)):
                oldRef = self.prepared[id(obj)]
                if oldRef.refnum:
                    # it's been referenced already
                    key = oldRef.refnum
                else:
                    # it hasn't been referenced yet
                    self._ref_id = self._ref_id + 1
                    key = self._ref_id
                    oldRef.setRef(key)
                return Deref(key)

            retval = Ref()
            self.prepareForRef(retval, obj)

            if objType is types.ListType:
                retval.setObj(map(self.jellyToAO, obj))  #hah!

            elif objType is types.TupleType:
                retval.setObj(tuple(map(self.jellyToAO, obj)))

            elif objType is types.DictionaryType:
                d = {}
                for k, v in obj.items():
                    d[self.jellyToAO(k)] = self.jellyToAO(v)
                retval.setObj(d)

            elif objType is types.InstanceType:
                if hasattr(obj, "__getstate__"):
                    state = self.jellyToAO(obj.__getstate__())
                else:
                    state = self.jellyToAO(obj.__dict__)
                retval.setObj(Instance(reflect.qual(obj.__class__), state))

            elif copy_reg.dispatch_table.has_key(objType):
                unpickleFunc, state = copy_reg.dispatch_table[objType](obj)

                retval.setObj(
                    Copyreg(reflect.fullFuncName(unpickleFunc),
                            self.jellyToAO(state)))

            else:
                raise TypeError("Unsupported type: %s" % objType.__name__)

        del self.stack[-1]
        return retval
Example #3
0
    def jellyToNode(self, obj):
        """Create a node representing the given object and return it.
        """
        objType = type(obj)

        # XXX Freevo changes:
        # transform unicode to str with String
        if objType is types.UnicodeType:
            obj = String(obj)
            objType = str

        #immutable (We don't care if these have multiple refs)
        if objType is types.NoneType:
            node = self.document.createElement("None")
        elif objType is types.StringType:
            node = self.document.createElement("string")
            r = repr(obj)
            if r[0] == '"':
                r = r.replace("'", "\\'")
            else:
                r = r.replace('"', '\\"')
            node.setAttribute("value", r[1:-1])
            # node.appendChild(CDATASection(obj))
        elif objType is types.IntType:
            node = self.document.createElement("int")
            node.setAttribute("value", str(obj))
        elif objType is types.LongType:
            node = self.document.createElement("longint")
            s = str(obj)
            if s[-1] == 'L':
                s = s[:-1]
            node.setAttribute("value", s)
        elif objType is types.FloatType:
            node = self.document.createElement("float")
            node.setAttribute("value", repr(obj))
        elif objType is types.MethodType:
            node = self.document.createElement("method")
            node.setAttribute("name", obj.im_func.__name__)
            node.setAttribute("class", qual(obj.im_class))
            # TODO: make methods 'prefer' not to jelly the object internally,
            # so that the object will show up where it's referenced first NOT
            # by a method.
            node.appendChild(self.jellyToNode(obj.im_self))
        elif hasattr(types, 'BooleanType') and objType is types.BooleanType:
            node = self.document.createElement("bool")
            node.setAttribute("value", str(int(obj)))
        elif objType is types.ModuleType:
            node = self.document.createElement("module")
            node.setAttribute("name", obj.__name__)
        elif objType==types.ClassType or issubclass(objType, type):
            node = self.document.createElement("class")
            node.setAttribute("name", qual(obj))
        elif objType is types.UnicodeType:
            node = self.document.createElement("unicode")
            obj = obj.encode('raw_unicode_escape')
            s = obj.replace("\n", "\\n").replace("\t", "\\t")
            node.setAttribute("value", s)
        elif objType in (types.FunctionType, types.BuiltinFunctionType):
            # TODO: beat pickle at its own game, and do BuiltinFunctionType
            # separately, looking for __self__ attribute and unpickling methods
            # of C objects when possible.
            node = self.document.createElement("function")
            node.setAttribute("name", fullFuncName(obj))
        else:
            #mutable!
            if self.prepared.has_key(id(obj)):
                oldNode = self.prepared[id(obj)][1]
                if oldNode.hasAttribute("reference"):
                    # it's been referenced already
                    key = oldNode.getAttribute("reference")
                else:
                    # it hasn't been referenced yet
                    self._ref_id = self._ref_id + 1
                    key = str(self._ref_id)
                    oldNode.setAttribute("reference", key)
                node = self.document.createElement("reference")
                node.setAttribute("key", key)
                return node
            node = self.document.createElement("UNNAMED")
            self.prepareElement(node, obj)
            if objType is types.ListType or __builtin__.__dict__.has_key('object') and isinstance(obj, NodeList):
                node.tagName = "list"
                for subobj in obj:
                    node.appendChild(self.jellyToNode(subobj))
            elif objType is types.TupleType:
                node.tagName = "tuple"
                for subobj in obj:
                    node.appendChild(self.jellyToNode(subobj))
            elif objType is types.DictionaryType:
                node.tagName = "dictionary"
                for k, v in obj.items():
                    n = self.jellyToNode(k)
                    n.setAttribute("role", "key")
                    n2 = self.jellyToNode(v)
                    node.appendChild(n)
                    node.appendChild(n2)
            elif copy_reg.dispatch_table.has_key(objType):
                unpickleFunc, state = copy_reg.dispatch_table[objType](obj)
                node = self.document.createElement("copyreg")
                # node.setAttribute("type", objType.__name__)
                node.setAttribute("loadfunc", fullFuncName(unpickleFunc))
                node.appendChild(self.jellyToNode(state))
            elif objType is types.InstanceType or hasattr(objType, "__module__"):
                className = qual(obj.__class__)
                node.tagName = "instance"
                node.setAttribute("class", className)
                if isinstance(obj, DOMJellyable):
                    obj.jellyToDOM(self, node)
                else:
                    if hasattr(obj, "__getstate__"):
                        state = obj.__getstate__()
                    elif hasattr(obj, "__dict__"):
                        state = obj.__dict__
                    else:
                        raise Exception("Unsupported type: %s %s" % (objType.__name__, repr(obj)))
                    n = self.jellyToNode(state)
                    node.appendChild(n)
            else:
                raise Exception("Unsupported type: %s" % objType.__name__)
        return node
Example #4
0
    def jellyToAO(self, obj):
        """I turn an object into an AOT and return it."""
        objType = type(obj)
        self.stack.append(repr(obj))

        #immutable: We don't care if these have multiple refs!
        if objType in _SIMPLE_BUILTINS:
            retval = obj

        elif objType is types.MethodType:
            # TODO: make methods 'prefer' not to jelly the object internally,
            # so that the object will show up where it's referenced first NOT
            # by a method.
            retval = InstanceMethod(_funcOfMethod(obj).__name__,
                                    reflect.qual(_classOfMethod(obj)),
                                    self.jellyToAO(_selfOfMethod(obj)))

        elif objType is types.ModuleType:
            retval = Module(obj.__name__)

        elif objType is _OldStyleClass:
            retval = Class(reflect.qual(obj))

        elif issubclass(objType, type):
            retval = Class(reflect.qual(obj))

        elif objType is types.FunctionType:
            retval = Function(reflect.fullFuncName(obj))

        else: #mutable! gotta watch for refs.

#Marmalade had the nicety of being able to just stick a 'reference' attribute
#on any Node object that was referenced, but in AOT, the referenced object
#is *inside* of a Ref call (Ref(num, obj) instead of
#<objtype ... reference="1">). The problem is, especially for built-in types,
#I can't just assign some attribute to them to give them a refnum. So, I have
#to "wrap" a Ref(..) around them later -- that's why I put *everything* that's
#mutable inside one. The Ref() class will only print the "Ref(..)" around an
#object if it has a Reference explicitly attached.

            if id(obj) in self.prepared:
                oldRef = self.prepared[id(obj)]
                if oldRef.refnum:
                    # it's been referenced already
                    key = oldRef.refnum
                else:
                    # it hasn't been referenced yet
                    self._ref_id = self._ref_id + 1
                    key = self._ref_id
                    oldRef.setRef(key)
                return Deref(key)

            retval = Ref()
            def _stateFrom(state):
                retval.setObj(Instance(reflect.qual(obj.__class__),
                                       self.jellyToAO(state)))
            self.prepareForRef(retval, obj)

            if objType is list:
                retval.setObj([self.jellyToAO(o) for o in obj]) #hah!

            elif objType is tuple:
                retval.setObj(tuple(map(self.jellyToAO, obj)))

            elif objType is dict:
                d = {}
                for k,v in obj.items():
                    d[self.jellyToAO(k)] = self.jellyToAO(v)
                retval.setObj(d)

            elif objType in copy_reg.dispatch_table:
                unpickleFunc, state = copy_reg.dispatch_table[objType](obj)

                retval.setObj(Copyreg( reflect.fullFuncName(unpickleFunc),
                                       self.jellyToAO(state)))

            elif hasattr(obj, "__getstate__"):
                _stateFrom(obj.__getstate__())
            elif hasattr(obj, "__dict__"):
                _stateFrom(obj.__dict__)
            else:
                raise TypeError("Unsupported type: %s" % objType.__name__)

        del self.stack[-1]
        return retval
Example #5
0
    def jellyToNode(self, obj):
        """Create a node representing the given object and return it.
        """
        objType = type(obj)

        # XXX Freevo changes:
        # transform unicode to str with String
        if objType is types.UnicodeType:
            obj = String(obj)
            objType = str

        #immutable (We don't care if these have multiple refs)
        if objType is types.NoneType:
            node = self.document.createElement("None")
        elif objType is types.StringType:
            node = self.document.createElement("string")
            r = repr(obj)
            if r[0] == '"':
                r = r.replace("'", "\\'")
            else:
                r = r.replace('"', '\\"')
            node.setAttribute("value", r[1:-1])
            # node.appendChild(CDATASection(obj))
        elif objType is types.IntType:
            node = self.document.createElement("int")
            node.setAttribute("value", str(obj))
        elif objType is types.LongType:
            node = self.document.createElement("longint")
            s = str(obj)
            if s[-1] == 'L':
                s = s[:-1]
            node.setAttribute("value", s)
        elif objType is types.FloatType:
            node = self.document.createElement("float")
            node.setAttribute("value", repr(obj))
        elif objType is types.MethodType:
            node = self.document.createElement("method")
            node.setAttribute("name", obj.im_func.__name__)
            node.setAttribute("class", qual(obj.im_class))
            # TODO: make methods 'prefer' not to jelly the object internally,
            # so that the object will show up where it's referenced first NOT
            # by a method.
            node.appendChild(self.jellyToNode(obj.im_self))
        elif hasattr(types, 'BooleanType') and objType is types.BooleanType:
            node = self.document.createElement("bool")
            node.setAttribute("value", str(int(obj)))
        elif objType is types.ModuleType:
            node = self.document.createElement("module")
            node.setAttribute("name", obj.__name__)
        elif objType == types.ClassType or issubclass(objType, type):
            node = self.document.createElement("class")
            node.setAttribute("name", qual(obj))
        elif objType is types.UnicodeType:
            node = self.document.createElement("unicode")
            obj = obj.encode('raw_unicode_escape')
            s = obj.replace("\n", "\\n").replace("\t", "\\t")
            node.setAttribute("value", s)
        elif objType in (types.FunctionType, types.BuiltinFunctionType):
            # TODO: beat pickle at its own game, and do BuiltinFunctionType
            # separately, looking for __self__ attribute and unpickling methods
            # of C objects when possible.
            node = self.document.createElement("function")
            node.setAttribute("name", fullFuncName(obj))
        else:
            #mutable!
            if self.prepared.has_key(id(obj)):
                oldNode = self.prepared[id(obj)][1]
                if oldNode.hasAttribute("reference"):
                    # it's been referenced already
                    key = oldNode.getAttribute("reference")
                else:
                    # it hasn't been referenced yet
                    self._ref_id = self._ref_id + 1
                    key = str(self._ref_id)
                    oldNode.setAttribute("reference", key)
                node = self.document.createElement("reference")
                node.setAttribute("key", key)
                return node
            node = self.document.createElement("UNNAMED")
            self.prepareElement(node, obj)
            if objType is types.ListType or __builtin__.__dict__.has_key(
                    'object') and isinstance(obj, NodeList):
                node.tagName = "list"
                for subobj in obj:
                    node.appendChild(self.jellyToNode(subobj))
            elif objType is types.TupleType:
                node.tagName = "tuple"
                for subobj in obj:
                    node.appendChild(self.jellyToNode(subobj))
            elif objType is types.DictionaryType:
                node.tagName = "dictionary"
                for k, v in obj.items():
                    n = self.jellyToNode(k)
                    n.setAttribute("role", "key")
                    n2 = self.jellyToNode(v)
                    node.appendChild(n)
                    node.appendChild(n2)
            elif copy_reg.dispatch_table.has_key(objType):
                unpickleFunc, state = copy_reg.dispatch_table[objType](obj)
                node = self.document.createElement("copyreg")
                # node.setAttribute("type", objType.__name__)
                node.setAttribute("loadfunc", fullFuncName(unpickleFunc))
                node.appendChild(self.jellyToNode(state))
            elif objType is types.InstanceType or hasattr(
                    objType, "__module__"):
                className = qual(obj.__class__)
                node.tagName = "instance"
                node.setAttribute("class", className)
                if isinstance(obj, DOMJellyable):
                    obj.jellyToDOM(self, node)
                else:
                    if hasattr(obj, "__getstate__"):
                        state = obj.__getstate__()
                    elif hasattr(obj, "__dict__"):
                        state = obj.__dict__
                    else:
                        raise "Unsupported type: %s %s" % (objType.__name__,
                                                           repr(obj))
                    n = self.jellyToNode(state)
                    node.appendChild(n)
            else:
                raise "Unsupported type: %s" % objType.__name__
        return node
Example #6
0
 def jellyToNode(self, obj):
     """Create a node representing the given object and return it.
     """
     objType = type(obj)
     if objType is types.NoneType:
         node = self.document.createElement("None")
     elif objType is types.StringType:
         node = self.document.createElement("string")
         r = repr(obj)
         if r[0] == '"':
             r = r.replace("'", "\\'")
         else:
             r = r.replace('"', '\\"')
         node.setAttribute("value", r[1:-1])
     elif objType is types.IntType:
         node = self.document.createElement("int")
         node.setAttribute("value", str(obj))
     elif objType is types.LongType:
         node = self.document.createElement("longint")
         s = str(obj)
         if s[-1] == 'L':
             s = s[:-1]
         node.setAttribute("value", s)
     elif objType is types.FloatType:
         node = self.document.createElement("float")
         node.setAttribute("value", repr(obj))
     elif objType is types.MethodType:
         node = self.document.createElement("method")
         node.setAttribute("name", obj.im_func.__name__)
         node.setAttribute("class", qual(obj.im_class))
         node.appendChild(self.jellyToNode(obj.im_self))
     elif hasattr(types, 'BooleanType') and objType is types.BooleanType:
         node = self.document.createElement("bool")
         node.setAttribute("value", str(int(obj)))
     elif objType is types.ModuleType:
         node = self.document.createElement("module")
         node.setAttribute("name", obj.__name__)
     elif objType==types.ClassType or issubclass(objType, type):
         node = self.document.createElement("class")
         node.setAttribute("name", qual(obj))
     elif objType is types.UnicodeType:
         node = self.document.createElement("unicode")
         obj = obj.encode('raw_unicode_escape')
         s = obj.replace("\n", "\\n").replace("\t", "\\t")
         node.setAttribute("value", s)
     elif objType in (types.FunctionType, types.BuiltinFunctionType):
         node = self.document.createElement("function")
         node.setAttribute("name", fullFuncName(obj))
     else:
         if self.prepared.has_key(id(obj)):
             oldNode = self.prepared[id(obj)][1]
             if oldNode.hasAttribute("reference"):
                 key = oldNode.getAttribute("reference")
             else:
                 self._ref_id = self._ref_id + 1
                 key = str(self._ref_id)
                 oldNode.setAttribute("reference", key)
             node = self.document.createElement("reference")
             node.setAttribute("key", key)
             return node
         node = self.document.createElement("UNNAMED")
         self.prepareElement(node, obj)
         if objType is types.ListType or __builtin__.__dict__.has_key('object') and isinstance(obj, NodeList):
             node.tagName = "list"
             for subobj in obj:
                 node.appendChild(self.jellyToNode(subobj))
         elif objType is types.TupleType:
             node.tagName = "tuple"
             for subobj in obj:
                 node.appendChild(self.jellyToNode(subobj))
         elif objType is types.DictionaryType:
             node.tagName = "dictionary"
             for k, v in obj.items():
                 n = self.jellyToNode(k)
                 n.setAttribute("role", "key")
                 n2 = self.jellyToNode(v)
                 node.appendChild(n)
                 node.appendChild(n2)
         elif copy_reg.dispatch_table.has_key(objType):
             unpickleFunc, state = copy_reg.dispatch_table[objType](obj)
             node = self.document.createElement("copyreg")
             node.setAttribute("loadfunc", fullFuncName(unpickleFunc))
             node.appendChild(self.jellyToNode(state))
         elif objType is types.InstanceType or hasattr(objType, "__module__"):
             className = qual(obj.__class__)
             node.tagName = "instance"
             node.setAttribute("class", className)
             if isinstance(obj, DOMJellyable):
                 obj.jellyToDOM(self, node)
             else:
                 if hasattr(obj, "__getstate__"):
                     state = obj.__getstate__()
                 else:
                     state = obj.__dict__
                 n = self.jellyToNode(state)
                 node.appendChild(n)
         else:
             raise "Unsupported type: %s" % objType.__name__
     return node