Changeset 4045
- Timestamp:
- 2 Apr 2009, 11:05:08 (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
waeup/branches/ulif-rewrite/src/waeup/utils/importexport.py
r4040 r4045 3 3 import grok 4 4 import cPickle 5 import zope.xmlpickle 5 6 from cStringIO import StringIO 6 7 from xml.dom.minidom import Document, getDOMImplementation 8 from zope.interface import Interface 7 9 from waeup.interfaces import IWAeUPObject, IWAeUPExporter, IWAeUPXMLExporter 8 10 … … 22 24 23 25 class Exporter(grok.Adapter): 24 """Export a WAeUP object .26 """Export a WAeUP object as pickle. 25 27 26 28 This all-purpose exporter exports attributes defined in schemata … … 39 41 return filelike_obj 40 42 41 42 43 ts_regexp = re.compile('^<((type)|(class)) (.*)>$')44 def getTypeString(obj):45 type_str = str(type(obj))46 m = ts_regexp.match(type_str).groups()47 return (m[0], m[3][1:-1])48 49 50 51 43 class XMLExporter(grok.Adapter): 52 44 """Export a WAeUP object as XML. 53 45 54 This all-purpose exporter exports attributes defined in schemata55 and contained objects (if the exported thing is a container).46 This all-purpose exporter exports XML representations of pickable 47 objects. 56 48 """ 57 grok.context(I WAeUPObject)49 grok.context(Interface) 58 50 grok.provides(IWAeUPXMLExporter) 59 51 60 doc = None61 62 def getObjectNode(self, obj):63 dotted_name = obj.__module__ + '.' + obj.__class__.__name__64 node = self.doc.createElement('object')65 node.setAttribute('type', dotted_name)66 return node67 68 def _export(self, obj, parent=None):69 if self.doc is None:70 impl = getDOMImplementation()71 self.doc = impl.createDocument(None, 'waeupdata', None)72 if parent is None:73 parent = self.doc.documentElement74 75 # Handle object meta data...76 obj_node = self.getObjectNode(obj)77 parent.appendChild(obj_node)78 parent = obj_node79 80 # Handle attributes...81 for name in obj.__dict__:82 if name.startswith('_'):83 continue84 val = unicode(getattr(obj, name))85 type_type, type_name = getTypeString(getattr(obj, name))86 if type_type not in ['type',]:87 # XXX: if type_type == 'class', we should deliver an object.88 continue89 newnode = self.doc.createElement('attribute')90 newnode.setAttribute('name', name)91 newnode.setAttribute('type', type_name)92 text = self.doc.createTextNode(val)93 newnode.appendChild(text)94 parent.appendChild(newnode)95 return self.doc96 97 def cleanUp(self):98 if self.doc is not None:99 self.doc.unlink()100 self.doc = None101 return102 103 52 def export(self, obj, filepath=None): 104 result = self._export(obj) 105 if filepath is None: 106 filelike_obj = StringIO() 107 else: 108 filelike_obj = open(filepath, 'wb') 109 filelike_obj.write(result.toxml(encoding='UTF-8')) 110 filelike_obj.seek(0) 111 self.cleanUp() 112 return filelike_obj 113 114 def exportPrettyXML(self, obj, filepath=None): 115 result = self._export(obj).toprettyxml(encoding='UTF-8') 53 pickled_obj = cPickle.dumps(obj) 54 result = zope.xmlpickle.toxml(pickled_obj) 116 55 if filepath is None: 117 56 filelike_obj = StringIO() … … 120 59 filelike_obj.write(result) 121 60 filelike_obj.seek(0) 122 self.cleanUp()123 61 return filelike_obj
Note: See TracChangeset for help on using the changeset viewer.