Author: jmorliaguet
Date: Fri May 19 23:15:38 2006
New Revision: 3161

Modified:
   cpsskins/branches/paris-sprint-2006/setup/io/configure.zcml
   cpsskins/branches/paris-sprint-2006/setup/io/fields.py

Log:

- added an IterableFieldIO adapter for lists and tuples

- the value type of List and Tuple is taken into account



Modified: cpsskins/branches/paris-sprint-2006/setup/io/configure.zcml
==============================================================================
--- cpsskins/branches/paris-sprint-2006/setup/io/configure.zcml (original)
+++ cpsskins/branches/paris-sprint-2006/setup/io/configure.zcml Fri May 19 
23:15:38 2006
@@ -8,6 +8,10 @@
   />
 
   <adapter
+      factory=".fields.TupleField"
+  />
+
+  <adapter
       factory=".fields.TextField"
   />
 

Modified: cpsskins/branches/paris-sprint-2006/setup/io/fields.py
==============================================================================
--- cpsskins/branches/paris-sprint-2006/setup/io/fields.py      (original)
+++ cpsskins/branches/paris-sprint-2006/setup/io/fields.py      Fri May 19 
23:15:38 2006
@@ -22,16 +22,18 @@
 
 import zope.schema
 
+from zope.app.file.image import getImageInfo
 from zope.component import adapts
 from zope.interface import implements
-from zope.app.file.image import getImageInfo
+from zope.schema.interfaces import ITuple
 
 from cpsskins.setup.io.interfaces import IFieldIO, IBinaryFieldIO
 
 logger = logging.getLogger("cpsskins")
 
-class BaseFieldIO:
-
+class BaseFieldIO(object):
+    """Base field IO.
+    """
     def __init__(self, field):
         self.field = field
 
@@ -41,22 +43,43 @@
     def dump(self, obj):
         return unicode(obj)
 
-class ListField(BaseFieldIO):
-    """A list field.
+class IterableFieldIO(object):
+    """An iterable field.
     """
-    adapts(zope.schema.interfaces.IList)
-    implements(IFieldIO)
+    def __init__(self, field):
+        self.field = field
+        self.field_io = IFieldIO(field.value_type)
 
     def dump(self, obj):
-        # TODO escape ','
-        return u', '.join(obj)
+        res = []
+        for o in obj:
+            res.append(self.field_io.dump(o))
+        return u', '.join(res)
 
     def load(self, text):
-        # TODO unescape ','
         obj = text.split(', ')
+        res = []
+        for o in obj:
+            res.append(self.field_io.load(o))
+        if ITuple.providedBy(self.field):
+            obj = tuple(obj)
         self.field.validate(obj)
         return obj
 
+##### Fields  ##########################################################
+
+class TupleField(IterableFieldIO):
+    """A tuple field.
+    """
+    adapts(zope.schema.interfaces.ITuple)
+    implements(IFieldIO)
+
+class ListField(IterableFieldIO):
+    """A list field.
+    """
+    adapts(zope.schema.interfaces.IList)
+    implements(IFieldIO)
+
 class IntField(BaseFieldIO):
     """An integer field.
     """
-- 
http://lists.nuxeo.com/mailman/listinfo/z3lab-checkins

Reply via email to