Changeset 4045


Ignore:
Timestamp:
2 Apr 2009, 11:05:08 (16 years ago)
Author:
uli
Message:

Use external XML pickler for XML exports.

File:
1 edited

Legend:

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

    r4040 r4045  
    33import grok
    44import cPickle
     5import zope.xmlpickle
    56from cStringIO import StringIO
    67from xml.dom.minidom import Document, getDOMImplementation
     8from zope.interface import Interface
    79from waeup.interfaces import IWAeUPObject, IWAeUPExporter, IWAeUPXMLExporter
    810
     
    2224
    2325class Exporter(grok.Adapter):
    24     """Export a WAeUP object.
     26    """Export a WAeUP object as pickle.
    2527
    2628    This all-purpose exporter exports attributes defined in schemata
     
    3941        return filelike_obj
    4042
    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 
    5143class XMLExporter(grok.Adapter):
    5244    """Export a WAeUP object as XML.
    5345
    54     This all-purpose exporter exports attributes defined in schemata
    55     and contained objects (if the exported thing is a container).
     46    This all-purpose exporter exports XML representations of pickable
     47    objects.
    5648    """
    57     grok.context(IWAeUPObject)
     49    grok.context(Interface)
    5850    grok.provides(IWAeUPXMLExporter)
    5951
    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_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                 continue
    89             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.doc
    96 
    97     def cleanUp(self):
    98         if self.doc is not None:
    99             self.doc.unlink()
    100         self.doc = None
    101         return
    102 
    10352    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)
    11655        if filepath is None:
    11756            filelike_obj = StringIO()
     
    12059        filelike_obj.write(result)
    12160        filelike_obj.seek(0)
    122         self.cleanUp()
    12361        return filelike_obj
Note: See TracChangeset for help on using the changeset viewer.