Changeset 3856


Ignore:
Timestamp:
16 Jan 2009, 12:29:37 (16 years ago)
Author:
uli
Message:

First basic exporter adapters.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • waeup/branches/ulif-rewrite/src/waeup/utils/importexport.py

    r3529 r3856  
     1import re
    12import csv
     3import grok
     4import cPickle
     5from cStringIO import StringIO
     6from xml.dom.minidom import Document, getDOMImplementation
     7from waeup.interfaces import IWAeUPObject, IWAeUPExporter, IWAeUPXMLExporter
    28
    39def readFile(f):
     
    1420    writer = csv.writer(open("export.csv", "wb"))
    1521    writer.writerows(data)
     22
     23class 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
     43ts_regexp = re.compile('^<type (.*)>$')
     44def 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
     51class 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.