Author: jmorliaguet
Date: Thu Apr 27 23:57:08 2006
New Revision: 2974

Modified:
   cpsskins/branches/paris-sprint-2006/setup/configure.zcml
   cpsskins/branches/paris-sprint-2006/setup/utils.py
Log:

- added an adapter lookup for exporting / importing non-string fields 
  (only zope.schema.interfaces.IList is supported for now)

- indent the xml output



Modified: cpsskins/branches/paris-sprint-2006/setup/configure.zcml
==============================================================================
--- cpsskins/branches/paris-sprint-2006/setup/configure.zcml    (original)
+++ cpsskins/branches/paris-sprint-2006/setup/configure.zcml    Thu Apr 27 
23:57:08 2006
@@ -59,4 +59,11 @@
 
   </class>
 
+
+  <!-- field IO -->
+
+  <adapter
+      factory="cpsskins.setup.utils.ListField"
+  />
+
 </configure>

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  Thu Apr 27 23:57:08 2006
@@ -24,11 +24,12 @@
 from tarfile import TarFile
 from tarfile import TarInfo
 
-from zope.component import createObject
+import zope.schema
+
+from zope.component import createObject, provideUtility, adapts
 from zope.component import getUtility, queryUtility, getUtilitiesFor
 from zope.component.interface import getInterface
-from zope.interface import implements
-from zope.schema import getFieldNamesInOrder
+from zope.interface import implements, Interface
 
 from cpsskins.elements.interfaces import IIdentifiable, IType
 from cpsskins.setup.interfaces import IDataExporter, IDataImporter
@@ -95,16 +96,6 @@
         self.archive.close()
         return self.stream.getvalue()
 
-def serialize(obj):
-    if isinstance(obj, list):
-        return u','.join(obj)
-    return obj
-
-def unserialize(text):
-    if ',' in text:
-        return text.split(',')
-    return text
-
 def editObject(obj, props):
     _marker = object()
     for k, v in props.items():
@@ -126,21 +117,29 @@
         schema = IType(context).getContentType()
 
         content = []
-        for name in getFieldNamesInOrder(schema):
+        for name, field in zope.schema.getFieldsInOrder(schema):
+            obj = getattr(context, name)
+
+            field_io = IFieldIO(field, None)
+            if field_io is not None:
+                value = field_io.dump(obj)
+            else:
+                value = obj
+
             content.append(
-                u'<%(name)s value="%(value)s" />' % {
+                u'<%(name)s value="%(value)s"/>' % {
                 'name': name,
-                'value': serialize(getattr(context, name)),
+                'value': value,
                 })
 
         return u"""<?xml version="1.0"?>
 <%(name)s uri="%(uri)s">
-%(content)s
+  %(content)s
 </%(name)s>
         """ % {
             'name': IType(context).resourcename,
             'uri': IIdentifiable(context).getURI(),
-            'content': u'\n'.join(content),
+            'content': u'\n  '.join(content),
             }
 
 class ObjectImporter(object):
@@ -153,13 +152,51 @@
 
         object_name = dom.tagName
         object = createObject(object_name)
-        schema = IType(object).getResourceType()
+        schema = IType(object).getContentType()
 
         props = {}
-        for name in getFieldNamesInOrder(schema):
-            props[name] = unserialize(dom.getAttribute(name))
+        for name, field in zope.schema.getFieldsInOrder(schema):
+            text = dom.getAttribute(name)
+
+            field_io = IFieldIO(field, None)
+            if field_io is not None:
+                value = field_io.load(text)
+            else:
+                value = text
+
+            props[name] = value
 
         editObject(object, props)
 
         context.add(object)
         return object
+
+#######################################################################
+# Field importer / exporter
+#######################################################################
+
+class IFieldIO(Interface):
+    """A field importer / exporter"""
+
+    def dump(obj):
+        """convert the object into a string"""
+
+    def load(text):
+        """convert the string into an object"""
+
+class ListField:
+    """A list field"""
+
+    adapts(zope.schema.interfaces.IList)
+    implements(IFieldIO)
+
+    def __init__(self, field):
+        self.field = field
+
+    def dump(self, obj):
+        return u','.join(obj)
+
+    def load(self, text):
+        return text.split(',')
+
+
-- 
http://lists.nuxeo.com/mailman/listinfo/z3lab-checkins

Reply via email to