Author: jmorliaguet
Date: Sat Apr 29 19:56:05 2006
New Revision: 2986

Modified:
   cpsskins/branches/paris-sprint-2006/setup/interfaces.py
   cpsskins/branches/paris-sprint-2006/setup/io.py
   cpsskins/branches/paris-sprint-2006/setup/utils.py
Log:

- saving work:

  - much simplified API for exporting / importing object:

    there is only one adapter (IDOMAdapter) that saves the object as a DOM
    fragment during exports and loads from DOM during imports, instead of
    IDataExporter / IDataImporter.

    the actual XML writer is implemented by the application.



Modified: cpsskins/branches/paris-sprint-2006/setup/interfaces.py
==============================================================================
--- cpsskins/branches/paris-sprint-2006/setup/interfaces.py     (original)
+++ cpsskins/branches/paris-sprint-2006/setup/interfaces.py     Sat Apr 29 
19:56:05 2006
@@ -91,25 +91,23 @@
     def isCustom(name, context):
         """Returns true if the resource has been customized"""
 
-class IDataExporter(Interface):
-    """A data exporter exports data to XML
+class IDOMAdapter(Interface):
+    """A DOM adapter for loading and saving objects as DOM fragments.
     """
-    mapping = Attribute(u"URI mapping")
+
+    document = Attribute(u"The document element.")
+
+    mapping = Attribute(u"Mapping between old and new URIs")
 
     def __call__():
         """Export to XML."""
 
+    def setDocument(document):
+        """Set the document element"""
+
     def save():
         """Save data as a DOM fragment."""
 
-    def getInfo():
-        """Return some export information (dictionary, ...)."""
-
-class IDataImporter(Interface):
-    """A data importer imports data from XML
-    """
-    mapping = Attribute(u"URI mapping")
-
-    def load(dom):
+    def load():
         """Load data from a DOM fragment."""
 

Modified: cpsskins/branches/paris-sprint-2006/setup/io.py
==============================================================================
--- cpsskins/branches/paris-sprint-2006/setup/io.py     (original)
+++ cpsskins/branches/paris-sprint-2006/setup/io.py     Sat Apr 29 19:56:05 2006
@@ -17,65 +17,56 @@
 """
 __docformat__ = "reStructuredText"
 
+import logging
+
 import zope.schema
 
-from zope.component import createObject, adapts
+from zope.component import adapts
 from zope.interface import implements, Interface
 
-from cpsskins.elements.interfaces import IIdentifiable, IType
-from cpsskins.setup.interfaces import IDataExporter, IDataImporter
-from cpsskins.setup.utils import editObject
+from cpsskins.elements.interfaces import IType
+from cpsskins.setup.interfaces import IDOMAdapter
+
+logger = logging.getLogger("cpsskins")
 
-class DataExporter(object):
-    """Base class for data exporters
+class BaseDOMAdapter(object):
+    """Base class for DOM adapters.
     """
-    def __init__(self, context, request):
+    def __init__(self, context, node):
         self.context = context
-        self.request = request
+        self.node = node
 
         self.mapping = {}
 
     def __call__(self):
         """Export to XML.
         """
-        raise NotImplementedError
+        return self.save().toxml()
+
+    def setDocument(self, document):
+        self.document = document
 
     def save(self):
-        """Save data as a DOM fragment.
+        """Save data in the DOM fragment.
         """
         raise NotImplementedError
 
-    def getInfo(self):
-        """Return some export information (dictionary, ...).
+    def load(self):
+        """Load data from the DOM node.
         """
         raise NotImplementedError
 
-class DataImporter(object):
-    """Base class for data importers"""
-
-    def __init__(self, context, request):
-        self.context = context
-        self.request = request
-
-        self.mapping = {}
-
-    def load(self, dom):
-        """Load data from a DOM fragment. Returns an object.
-        """
-        raise NotImplementedError
-
-class ObjectExporter(DataExporter):
-    """Export objects
+class DOMAdapter(BaseDOMAdapter):
+    """Generic DOM adapter
     """
-    implements(IDataExporter)
+    implements(IDOMAdapter)
 
-    def __call__(self):
-        context = self.context
+    def save(self):
+        context = self.context 
+        node = self.node
 
-        properties = []
         schema = IType(context).getContentType()
 
-        content = []
         for name, field in zope.schema.getFieldsInOrder(schema):
             obj = getattr(context, name)
 
@@ -85,37 +76,21 @@
             else:
                 value = obj
 
-            content.append(
-                u'<%(name)s value="%(value)s"/>' % {
-                'name': name,
-                'value': value,
-                })
-
-        return u"""<?xml version="1.0"?>
-<%(name)s uri="%(uri)s">
-  %(content)s
-</%(name)s>
-        """ % {
-            'name': IType(context).resourcename,
-            'uri': IIdentifiable(context).getURI(),
-            'content': u'\n  '.join(content),
-            }
+            prop = self.document.createElement(name)
+            prop.setAttribute(u'value', value)
 
-class ObjectImporter(DataImporter):
-    """Import objects
-    """
-    implements(IDataImporter)
+            node.appendChild(prop)
 
-    def load(self, dom):
+    def load(self):
         context = self.context
+        node = self.node
 
-        object_name = dom.tagName
-        object = createObject(object_name)
-        schema = IType(object).getContentType()
+        schema = IType(context).getContentType()
+
+        _marker = object()
 
-        props = {}
         for name, field in zope.schema.getFieldsInOrder(schema):
-            text = dom.getAttribute(name)
+            text = node.getAttribute(name)
 
             field_io = IFieldIO(field, None)
             if field_io is not None:
@@ -123,12 +98,11 @@
             else:
                 value = text
 
-            props[name] = value
-
-        editObject(object, props)
+            if getattr(context, name, _marker) is _marker:
+                logger.warning("%s has no attribute '%s'", repr(context), name)
+                continue
+            setattr(context, name, value)
 
-        context.add(object)
-        return object
 
 #######################################################################
 # Field importer / exporter
@@ -144,8 +118,8 @@
         """convert the string into an object"""
 
 class ListField:
-    """A list field"""
-
+    """A list field.
+    """
     adapts(zope.schema.interfaces.IList)
     implements(IFieldIO)
 

Modified: cpsskins/branches/paris-sprint-2006/setup/utils.py
==============================================================================
--- cpsskins/branches/paris-sprint-2006/setup/utils.py  (original)
+++ cpsskins/branches/paris-sprint-2006/setup/utils.py  Sat Apr 29 19:56:05 2006
@@ -88,11 +88,3 @@
         self.archive.close()
         return self.stream.getvalue()
 
-def editObject(obj, props):
-    _marker = object()
-    for k, v in props.items():
-        if getattr(obj, k, _marker) is _marker:
-            logger.warning("%s has no attribute named '%s'", repr(obj), k)
-            continue
-        setattr(obj, k, v)
-
-- 
http://lists.nuxeo.com/mailman/listinfo/z3lab-checkins

Reply via email to