Author: jmorliaguet
Date: Fri Apr 14 14:18:48 2006
New Revision: 2842

Modified:
   cpsskins/branches/jmo-perspectives/setup/utils.py
Log:

- added a generic object importer / exporter



Modified: cpsskins/branches/jmo-perspectives/setup/utils.py
==============================================================================
--- cpsskins/branches/jmo-perspectives/setup/utils.py   (original)
+++ cpsskins/branches/jmo-perspectives/setup/utils.py   Fri Apr 14 14:18:48 2006
@@ -18,15 +18,22 @@
 __docformat__ = "reStructuredText"
 
 import time
+import logging
 
 from StringIO import StringIO
 from tarfile import TarFile
 from tarfile import TarInfo
 
-from zope.component import queryUtility, getUtility
+from zope.component import getUtility, createObject
+from zope.interface import implements
+from zope.schema import getFieldNamesInOrder
 
-from cpsskins.utils import getThemeManager
+from cpsskins.elements.interfaces import IIdentifiable, IType
+from cpsskins.setup.interfaces import IDataExporter, IDataImporter
 from cpsskins.setup.interfaces import IResourceManager
+from cpsskins.utils import getThemeManager
+
+logger = logging.getLogger("cpsskins")
 
 def resource(name):
     mgr = getThemeManager()
@@ -59,14 +66,6 @@
         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)
-
 def serialize(obj):
     if isinstance(obj, list):
         return u','.join(obj)
@@ -77,3 +76,60 @@
         return text.split(',')
     return text
 
+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)
+
+
+class ObjectExporter(object):
+    """Export objects
+    """
+    implements(IDataExporter)
+
+    def __call__(self):
+        context = self.context
+
+        properties = []
+        schema = IType(context).getResourceType()
+
+        content = u''
+        for name in getFieldNamesInOrder(schema):
+            content += u'<property name=%(name)s value="%(value)s" />' % {
+                'name': name,
+                'value': serialize(getattr(context, name)),
+                }
+
+        return u"""<?xml version="1.0"?>
+        <%(name)s uri="%(uri)s">
+          %(content)s
+        </%(name)s>
+        """ % {
+            'name': schema.getTaggedValue(u'name'),
+            'uri': IIdentifiable(context).getURI(),
+            'content': content,
+            }
+
+class ObjectImporter(object):
+    """Import objects
+    """
+    implements(IDataImporter)
+
+    def load(self, dom):
+        context = self.context
+
+        object_name = dom.tagName
+        object = createObject(object_name)
+        schema = IType(object).getResourceType()
+
+        props = {}
+        for name in getFieldNamesInOrder(schema):
+            props[name] = unserialize(dom.getAttribute(name))
+
+        editObject(object, props)
+
+        context.add(object)
+        return object
-- 
http://lists.nuxeo.com/mailman/listinfo/z3lab-checkins

Reply via email to