def __init__(self): # dict of {id(obj): (obj, node)} self.prepared = {} self.document = Document() self._ref_id = 0
class DOMJellier: def __init__(self): # dict of {id(obj): (obj, node)} self.prepared = {} self.document = Document() self._ref_id = 0 def prepareElement(self, element, object): self.prepared[id(object)] = (object, element) 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 def jelly(self, obj): """Create a document representing the current object, and return it. """ node = self.jellyToNode(obj) self.document.appendChild(node) return self.document
class DOMJellier: def __init__(self): # dict of {id(obj): (obj, node)} self.prepared = {} self.document = Document() self._ref_id = 0 def prepareElement(self, element, object): self.prepared[id(object)] = (object, element) 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 def jelly(self, obj): """Create a document representing the current object, and return it. """ node = self.jellyToNode(obj) self.document.appendChild(node) return self.document
def __init__(self): self.prepared = {} self.document = Document() self._ref_id = 0
class DOMJellier: def __init__(self): self.prepared = {} self.document = Document() self._ref_id = 0 def prepareElement(self, element, object): self.prepared[id(object)] = (object, element) 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 def jelly(self, obj): """Create a document representing the current object, and return it. """ node = self.jellyToNode(obj) self.document.appendChild(node) return self.document