Changeset 3856
- Timestamp:
- 16 Jan 2009, 12:29:37 (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
waeup/branches/ulif-rewrite/src/waeup/utils/importexport.py
r3529 r3856 1 import re 1 2 import csv 3 import grok 4 import cPickle 5 from cStringIO import StringIO 6 from xml.dom.minidom import Document, getDOMImplementation 7 from waeup.interfaces import IWAeUPObject, IWAeUPExporter, IWAeUPXMLExporter 2 8 3 9 def readFile(f): … … 14 20 writer = csv.writer(open("export.csv", "wb")) 15 21 writer.writerows(data) 22 23 class Exporter(grok.Adapter): 24 """Export a WAeUP object. 25 26 This all-purpose exporter exports attributes defined in schemata 27 and contained objects (if the exported thing is a container). 28 """ 29 grok.context(IWAeUPObject) 30 grok.provides(IWAeUPExporter) 31 32 def export(self, obj, filepath=None): 33 if filepath is None: 34 filelike_obj = StringIO() 35 else: 36 filelike_obj = open(filepath, 'wb') 37 exported_obj = cPickle.dump(obj, filelike_obj) 38 filelike_obj.close() 39 return filelike_obj 40 41 42 43 ts_regexp = re.compile('^<type (.*)>$') 44 def getTypeString(obj): 45 type_str = str(type(obj)) 46 m = ts_regexp.match(type_str).groups() 47 return m[0][1:-1] 48 49 50 51 class XMLExporter(grok.Adapter): 52 """Export a WAeUP object as XML. 53 54 This all-purpose exporter exports attributes defined in schemata 55 and contained objects (if the exported thing is a container). 56 """ 57 grok.context(IWAeUPObject) 58 grok.provides(IWAeUPXMLExporter) 59 60 doc = None 61 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 node 67 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.documentElement 74 75 # Handle object meta data... 76 obj_node = self.getObjectNode(obj) 77 parent.appendChild(obj_node) 78 parent = obj_node 79 80 # Handle attributes... 81 for name in obj.__dict__: 82 if name.startswith('_'): 83 continue 84 val = unicode(getattr(obj, 'name')) 85 type_name = getTypeString(getattr(obj, 'name')) 86 newnode = self.doc.createElement('attribute') 87 newnode.setAttribute('name', name) 88 newnode.setAttribute('type', type_name) 89 text = self.doc.createTextNode(val) 90 newnode.appendChild(text) 91 parent.appendChild(newnode) 92 return self.doc 93 94 def cleanUp(self): 95 if self.doc is not None: 96 self.doc.unlink() 97 self.doc = None 98 return 99 100 def export(self, obj, filepath=None): 101 result = self._export(obj) 102 if filepath is None: 103 filelike_obj = StringIO() 104 else: 105 filelike_obj = open(filepath, 'wb') 106 filelike_obj.write(result.toprettyxml(encoding='UTF-8')) 107 filelike_obj.seek(0) 108 self.cleanUp() 109 return filelike_obj 110 111 def exportPrettyXML(self, obj): 112 result = self._export(obj).toprettyxml(encoding='UTF-8') 113 self.cleanUp() 114 return result
Note: See TracChangeset for help on using the changeset viewer.