Author: jmorliaguet
Date: Sat Apr 29 23:13:57 2006
New Revision: 2987

Removed:
   cpsskins/branches/paris-sprint-2006/standard/io/color.py
   cpsskins/branches/paris-sprint-2006/standard/io/element.py
   cpsskins/branches/paris-sprint-2006/standard/io/element.zcml
   cpsskins/branches/paris-sprint-2006/standard/io/page.py
   cpsskins/branches/paris-sprint-2006/standard/io/page.zcml
   cpsskins/branches/paris-sprint-2006/standard/io/setting.xml
   cpsskins/branches/paris-sprint-2006/standard/io/setting.zcml
   cpsskins/branches/paris-sprint-2006/standard/io/theme.py
   cpsskins/branches/paris-sprint-2006/standard/io/theme.zcml
Modified:
   cpsskins/branches/paris-sprint-2006/elements/theme.py
   cpsskins/branches/paris-sprint-2006/ftests/test_appsetup.py
   cpsskins/branches/paris-sprint-2006/setup/DOM.txt
   cpsskins/branches/paris-sprint-2006/setup/README.txt
   cpsskins/branches/paris-sprint-2006/setup/interfaces.py
   cpsskins/branches/paris-sprint-2006/setup/io.py
   cpsskins/branches/paris-sprint-2006/setup/registration.py
   cpsskins/branches/paris-sprint-2006/setup/test/dummy.py
   cpsskins/branches/paris-sprint-2006/standard/io/color.zcml
   cpsskins/branches/paris-sprint-2006/standard/io/configure.zcml
   cpsskins/branches/paris-sprint-2006/standard/io/setting.py
   cpsskins/branches/paris-sprint-2006/ui/screens/sitemanager/views.py
Log:

- saving work, new simpified IO API



Modified: cpsskins/branches/paris-sprint-2006/elements/theme.py
==============================================================================
--- cpsskins/branches/paris-sprint-2006/elements/theme.py       (original)
+++ cpsskins/branches/paris-sprint-2006/elements/theme.py       Sat Apr 29 
23:13:57 2006
@@ -17,7 +17,7 @@
 """
 __docformat__ = "reStructuredText"
 
-from zope.component import adapts, getUtilitiesFor
+from zope.component import adapts
 from zope.component.persistentregistry import PersistentComponents
 from zope.interface import implements
 from zope.app.container.btree import BTreeContainer

Modified: cpsskins/branches/paris-sprint-2006/ftests/test_appsetup.py
==============================================================================
--- cpsskins/branches/paris-sprint-2006/ftests/test_appsetup.py (original)
+++ cpsskins/branches/paris-sprint-2006/ftests/test_appsetup.py Sat Apr 29 
23:13:57 2006
@@ -24,14 +24,14 @@
 from zope.app import zapi
 from zope.app.testing import ztapi
 from zope.app.testing.functional import FunctionalDocFileSuite
-from zope.interface import alsoProvides
+from zope.interface import alsoProvides, Interfaces
 from zope.component.interfaces import IFactory
 
 from cpsskins import elements
 from cpsskins.elements.interfaces import IElement, IIdentifiable, IType
 from cpsskins.setup.interfaces import IResourceType
-from cpsskins.setup.interfaces import IDataImporter, IDataExporter
-from cpsskins.setup.test.dummy import IDummyResource, DummyResourceImporter
+from cpsskins.setup.interfaces import IDOMAdapter
+from cpsskins.setup.test.dummy import IDummyResource, DummyDOMAdapter
 from cpsskins.setup.test.dummy import DummyFactory
 
 import cpsskins.elements.interfaces
@@ -39,7 +39,8 @@
 def setUp(test):
     gsm = zapi.getGlobalSiteManager()
     # export adapters
-    gsm.registerAdapter(DummyResourceImporter, (IDummyResource,), 
IDataImporter)
+    gsm.registerAdapter(DummyDOMAdapter, (IDummyResource, Interface),
+                        IDOMAdapter)
     gsm.registerAdapter(elements.element.Identifiable, (IDummyResource,),
                         IIdentifiable)
 

Modified: cpsskins/branches/paris-sprint-2006/setup/DOM.txt
==============================================================================
--- cpsskins/branches/paris-sprint-2006/setup/DOM.txt   (original)
+++ cpsskins/branches/paris-sprint-2006/setup/DOM.txt   Sat Apr 29 23:13:57 2006
@@ -36,19 +36,19 @@
 
 Hierarchical DOM structures can be created using a recursive traversal.
 
-    >>> from cpsskins import elements
-    >>> tree = elements.theme.Theme(u'A Theme')
-    >>> tree[u'page'] = elements.themepage.ThemePage(u'A Theme page')
-    >>> tree[u'page'][u'top'] = elements.pageblock.PageBlock(u'Top block')
-    >>> tree[u'page'][u'top'][u'left'] = elements.cell.Cell(u'Left cell')
-    >>> tree[u'page'][u'top'][u'main'] = elements.cell.Cell(u'Main cell')
+    >>> from cpsskins.elements import theme, themepage, pageblock, cell
+    >>> tree = theme.Theme(u'A Theme')
+    >>> tree[u'page'] = themepage.ThemePage(u'A Theme page')
+    >>> tree[u'page'][u'top'] = pageblock.PageBlock(u'Top block')
+    >>> tree[u'page'][u'top'][u'left'] = cell.Cell(u'Left cell')
+    >>> tree[u'page'][u'top'][u'main'] = cell.Cell(u'Main cell')
 
 Let us traverse the entire tree recursively:
 
-    >>> def traverse(node):
-    ...     print node
-    ...     for child in node:
-    ...         traverse(node[child])
+    >>> def traverse(context):
+    ...     print context
+    ...     for child in context:
+    ...         traverse(context[child])
 
     >>> traverse(tree)
     Theme('A Theme')
@@ -63,12 +63,12 @@
     >>> doc = dom.createDocument(None, u'themes', None)
     >>> root = doc.documentElement
 
-    >>> def export(tree_node, dom_node):
+    >>> def export(context, node):
     ...    element = doc.createElement(u'element')
-    ...    element.setAttribute(u'title', unicode(tree_node)) 
-    ...    dom_child = dom_node.appendChild(element)
-    ...    for child in tree_node:
-    ...        export(tree_node[child], dom_child)
+    ...    element.setAttribute(u'title', unicode(context)) 
+    ...    node_child = node.appendChild(element)
+    ...    for child in context:
+    ...        export(context[child], node_child)
 
 We can the export the document to XML:
 

Modified: cpsskins/branches/paris-sprint-2006/setup/README.txt
==============================================================================
--- cpsskins/branches/paris-sprint-2006/setup/README.txt        (original)
+++ cpsskins/branches/paris-sprint-2006/setup/README.txt        Sat Apr 29 
23:13:57 2006
@@ -596,21 +596,21 @@
     >>> exporter = getMultiAdapter((pageblock, request), IDataExporter)
     >>> print exporter()
     <?xml version="1.0"?>
-    <element
-      uri="cpsskins://canvas-pageblock:12345"
-      title="Some page block"
-      parent=""/>
-    <BLANKLINE>
-    <element
-      uri="cpsskins://canvas-cell:12345"
-      title="Some cell 1"
-      parent="cpsskins://canvas-pageblock:12345"/>
-    <BLANKLINE>
-    <element
-      uri="cpsskins://canvas-cell:12345"
-      title="Some cell 2"
-      parent="cpsskins://canvas-pageblock:12345"/>
-    <BLANKLINE>
+    <pageblock
+        uri="cpsskins://canvas-pageblock:12345"
+        title="Some page block"
+        parent=""
+    />
+    <cell
+        uri="cpsskins://canvas-cell:12345"
+        title="Some cell 1"
+        parent="cpsskins://canvas-pageblock:12345"
+    />
+    <cell
+        uri="cpsskins://canvas-cell:12345"
+        title="Some cell 2"
+        parent="cpsskins://canvas-pageblock:12345"
+    />
 
 
 Export of container elements
@@ -697,3 +697,10 @@
     u'div.body.style12345  {color:#ff0000}'
 
 
+Appendix
+========
+
+Integration with xml.dom (the Document Object Model API)
+--------------------------------------------------------
+
+see DOM.txt

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 
23:13:57 2006
@@ -91,20 +91,24 @@
     def isCustom(name, context):
         """Returns true if the resource has been customized"""
 
+##### IO  #############################################################
+
+class IDOMFragment(Interface):
+    """Marker interface for DOM fragments"""
+
 class IDOMAdapter(Interface):
-    """A DOM adapter for loading and saving objects as DOM fragments.
-    """
+    """A DOM adapter for loading and saving objects as DOM fragments."""
 
     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 getDocument():
+        """Get the document element"""
+
     def save():
         """Save data as 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 23:13:57 2006
@@ -21,10 +21,11 @@
 
 import zope.schema
 
-from zope.component import adapts
+from zope.component import adapts, getMultiAdapter
 from zope.interface import implements, Interface
+from zope.app.container.interfaces import IItemContainer
 
-from cpsskins.elements.interfaces import IType
+from cpsskins.elements.interfaces import IIdentifiable, IType
 from cpsskins.setup.interfaces import IDOMAdapter
 
 logger = logging.getLogger("cpsskins")
@@ -32,20 +33,22 @@
 class BaseDOMAdapter(object):
     """Base class for DOM adapters.
     """
+    document = None
+    mapping = {}
+
     def __init__(self, context, node):
         self.context = context
         self.node = node
 
-        self.mapping = {}
-
-    def __call__(self):
-        """Export to XML.
-        """
-        return self.save().toxml()
-
     def setDocument(self, document):
         self.document = document
 
+    def getDocument(self):
+        if self.document is None:
+            raise ValueError("The DOM document is not set, "
+                "use setDocument(document) to set the document element.")
+        return self.document
+
     def save(self):
         """Save data in the DOM fragment.
         """
@@ -62,11 +65,18 @@
     implements(IDOMAdapter)
 
     def save(self):
-        context = self.context 
+        context = self.context
         node = self.node
 
+        document = self.getDocument()
+
+        element = document.createElement(IType(context).resourcename)
+        element.setAttribute(u'uri', IIdentifiable(context).getURI())
+
         schema = IType(context).getContentType()
 
+        # properties
+
         for name, field in zope.schema.getFieldsInOrder(schema):
             obj = getattr(context, name)
 
@@ -76,10 +86,20 @@
             else:
                 value = obj
 
-            prop = self.document.createElement(name)
+            prop = document.createElement(name)
             prop.setAttribute(u'value', value)
 
-            node.appendChild(prop)
+            element.appendChild(prop)
+
+        # child nodes
+
+        if IItemContainer.providedBy(context):
+            node_child = node.appendChild(element)
+            for child in context:
+                exporter = getMultiAdapter((context[child], node_child),
+                                            IDOMAdapter)
+                exporter.setDocument(document)
+                exporter.save()
 
     def load(self):
         context = self.context

Modified: cpsskins/branches/paris-sprint-2006/setup/registration.py
==============================================================================
--- cpsskins/branches/paris-sprint-2006/setup/registration.py   (original)
+++ cpsskins/branches/paris-sprint-2006/setup/registration.py   Sat Apr 29 
23:13:57 2006
@@ -26,9 +26,9 @@
 from xml.parsers.expat import ExpatError
 
 from zope.component import getUtility, queryUtility, createObject
-from zope.component import getGlobalSiteManager
+from zope.component import getGlobalSiteManager, getMultiAdapter
 
-from cpsskins.setup.interfaces import IResourceManager, IDataImporter
+from cpsskins.setup.interfaces import IResourceManager, IDOMAdapter
 from cpsskins.setup.interfaces import ISetting, IResource
 
 logger = logging.getLogger("cpsskins")
@@ -91,7 +91,7 @@
         logger.debug("reloaded setting %s in %s", name, filename)
 
     # load the data
-    #IDataImporter(resource).load(resource_def.toxml())
+    #getMultiAdapter(IDOMAdapter, (resource, resource_def)).load()
     return resource
 
 

Modified: cpsskins/branches/paris-sprint-2006/setup/test/dummy.py
==============================================================================
--- cpsskins/branches/paris-sprint-2006/setup/test/dummy.py     (original)
+++ cpsskins/branches/paris-sprint-2006/setup/test/dummy.py     Sat Apr 29 
23:13:57 2006
@@ -23,7 +23,8 @@
 from zope.component.factory import Factory
 from zope.interface import Interface, implements
 
-from cpsskins.setup.interfaces import IDataExporter, IDataImporter
+from cpsskins.setup.io import BaseDOMAdapter
+from cpsskins.setup.interfaces import IDOMAdapter
 
 class IDummyResource(Interface):
     """Dummy resource interface"""
@@ -45,20 +46,13 @@
 DummyFactory = Factory(DummyResource, "Dummy Factory",
                       "Factory that creates dummy resources")
 
-class DummyResourceImporter(object):
+class DummyDOMAdapter(BaseDOMAdapter):
 
-    adapts(IDummyResource)
-    implements(IDataImporter)
+    implements(IDOMAdapter)
 
-    def __init__(self, context):
-        self.context = context
+    def load(self):
+        """ """
 
-    def load(self, xml=u''):
-        context = self.context
-        dom = parseString(xml)
-        dummies = dom.getElementsByTagName('dummy')
-        if dummies:
-            dummy = dummies[0]
-            context.name = dummy.getAttribute('name')
-            context.title = dummy.getAttribute('title')
+    def save(self):
+        """ """
 

Modified: cpsskins/branches/paris-sprint-2006/standard/io/color.zcml
==============================================================================
--- cpsskins/branches/paris-sprint-2006/standard/io/color.zcml  (original)
+++ cpsskins/branches/paris-sprint-2006/standard/io/color.zcml  Sat Apr 29 
23:13:57 2006
@@ -5,7 +5,7 @@
   <!-- XML export -->
   <browser:view
       provides="cpsskins.setup.interfaces.IDataExporter"
-      for="cpsskins.standard.fields.color.IColor"
+      class="cpsskins.setup.io.ObjectExporter"
       class=".color.Exporter"
       permission="zope.ManageContent"
   />
@@ -14,7 +14,7 @@
   <browser:view
       provides="cpsskins.setup.interfaces.IDataImporter"
       for="cpsskins.setup.settings.ISettings"
-      class=".color.Importer"
+      class="cpsskins.setup.io.ObjectImporter"
       permission="zope.ManageContent"
       name="color"
   />

Modified: cpsskins/branches/paris-sprint-2006/standard/io/configure.zcml
==============================================================================
--- cpsskins/branches/paris-sprint-2006/standard/io/configure.zcml      
(original)
+++ cpsskins/branches/paris-sprint-2006/standard/io/configure.zcml      Sat Apr 
29 23:13:57 2006
@@ -1,38 +1,28 @@
 <configure
     xmlns="http://namespaces.zope.org/zope";>
 
-  <include file="area.zcml" />
-
-  <include file="box.zcml" />
-
-  <include file="boxgroup.zcml" />
-
-  <include file="color.zcml" />
-
-  <include file="effect.zcml" />
-
-  <include file="element.zcml" />
-
-  <include file="layout.zcml" />
-
-  <include file="page.zcml" />
-
-  <include file="perspective.zcml" />
-
-  <include file="portlet.zcml" />
-
-  <include file="relation.zcml" />
-
-  <include file="style.zcml" />
-
-  <include file="setting.zcml" />
-
-  <include file="settings.zcml" />
-
-  <include file="storage.zcml" />
-
-  <include file="theme.zcml" />
-
-  <include file="widget.zcml" />
+  <!-- elements -->
+  <adapter
+      for="cpsskins.elements.interfaces.IElement *"
+      factory="cpsskins.setup.io.DOMAdapter"
+      permission="zope.ManageContent"
+      provides="cpsskins.setup.interfaces.IDOMAdapter"
+  />
+
+  <!-- perspectives -->
+  <adapter
+      for="cpsskins.perspectives.interfaces.IPerspective *"
+      factory="cpsskins.setup.io.DOMAdapter"
+      permission="zope.ManageContent"
+      provides="cpsskins.setup.interfaces.IDOMAdapter"
+  />
+
+  <!-- settings -->
+  <adapter
+      for="cpsskins.setup.interfaces.ISetting *"
+      factory="cpsskins.standard.io.setting.DOMAdapter"
+      permission="zope.ManageContent"
+      provides="cpsskins.setup.interfaces.IDOMAdapter"
+  />
 
 </configure>

Modified: cpsskins/branches/paris-sprint-2006/standard/io/setting.py
==============================================================================
--- cpsskins/branches/paris-sprint-2006/standard/io/setting.py  (original)
+++ cpsskins/branches/paris-sprint-2006/standard/io/setting.py  Sat Apr 29 
23:13:57 2006
@@ -17,51 +17,42 @@
 """
 __docformat__ = "reStructuredText"
 
-from xml.dom.minidom import parseString
+import logging
+
+import zope.schema
 
-from zope.app import pagetemplate
-from zope.app.interface import queryType
 from zope.component import adapts, getMultiAdapter
-from zope.interface import implements
+from zope.interface import implements, Interface
 
-from cpsskins.setup.interfaces import IDataExporter, IDataImporter
-from cpsskins.setup.interfaces import IResource, ISetting, IResourceType
-from cpsskins.setup.io import DataImporter, DataExporter
+from cpsskins.elements.interfaces import IIdentifiable, IType
+from cpsskins.setup.interfaces import ISetting, IResource, IDOMAdapter
+from cpsskins.setup.io import BaseDOMAdapter
 
-class Exporter(DataExporter):
-    """Export a setting
+class DOMAdapter(BaseDOMAdapter):
+    """DOM adapter for settings
     """
-    implements(IDataExporter)
+    implements(IDOMAdapter)
 
-    template = pagetemplate.ViewPageTemplateFile('setting.xml')
+    def save(self):
+        context = self.context
+        node = self.node
 
-    def __call__(self):
-        return self.template()
+        document = self.getDocument()
 
-    def getInfo(self):
-        context = self.context
-        request = self.request
+        element = document.createElement(u'setting')
+        element.setAttribute(u'title', context.title)
+        #element.setAttribute(u'uri', IIdentifiable(resource).getURI())
+        resource_node = node.appendChild(element)
 
         resource = IResource(context).getResource()
-        resource_type = queryType(resource, IResourceType)
 
         # export the resource
-        exporter = getMultiAdapter((resource, request), IDataExporter)
-        # remove the xml header used in the resource
-        resource_xml = exporter().replace(u'<?xml version="1.0"?>', u'')
-
-        return {
-            'name': context.name,
-            'title': context.title,
-            'content': resource_xml,
-            }
-
-
-class Importer(DataImporter):
-    """Import a setting
-    """
-    implements(IDataImporter)
+        exporter = getMultiAdapter((resource, resource_node), IDOMAdapter)
+        exporter.setDocument(document)
+        exporter.save()
 
-    def load(self, dom):
+    def load(self):
         context = self.context
+        node = self.node
+        # TODO
 

Modified: cpsskins/branches/paris-sprint-2006/ui/screens/sitemanager/views.py
==============================================================================
--- cpsskins/branches/paris-sprint-2006/ui/screens/sitemanager/views.py 
(original)
+++ cpsskins/branches/paris-sprint-2006/ui/screens/sitemanager/views.py Sat Apr 
29 23:13:57 2006
@@ -17,17 +17,17 @@
 """
 __docformat__ = "reStructuredText"
 
-from xml.dom.minidom import parseString
+from xml.dom.minidom import parseString, DOMImplementation
 
 from zope.app.interface import queryType
 from zope.app.file import File
 from zope.component import getUtilitiesFor, getUtility, getMultiAdapter
 from zope.component import queryUtility, getAllUtilitiesRegisteredFor
+from zope.component import getMultiAdapter
 
 from cpsskins import minjson as json
 from cpsskins.elements.interfaces import IType
-from cpsskins.setup.interfaces import ISetting, IGlobalSetting
-from cpsskins.setup.interfaces import IDataExporter, IDataImporter
+from cpsskins.setup.interfaces import ISetting, IGlobalSetting, IDOMAdapter
 from cpsskins.setup.interfaces import IResourceType, IResourceManager, 
IResource
 from cpsskins.setup.registration import reloadSetting, refreshSettings
 from cpsskins.setup.settings import ISettings
@@ -95,8 +95,13 @@
         resource = IResource(setting).getResource()
         resource_type = queryType(resource, IResourceType)
 
-        exporter = getMultiAdapter((setting, request), IDataExporter)
-        return exporter()
+        document = DOMImplementation().createDocument(None, u'setting', None)
+        root = document.documentElement
+
+        exporter = getMultiAdapter((setting, root), IDOMAdapter)
+        exporter.setDocument(document)
+        exporter.save()
+        return document.toxml()
 
     def reloadSetting(self, name=u''):
         """Reload the setting from the file-system
@@ -119,8 +124,15 @@
         mgr = getThemeManager(context)
         settings = mgr.getSettings()
 
-        exporter = getMultiAdapter((settings, request), IDataExporter)
-        return exporter()
+        document = DOMImplementation().createDocument(None, u'settings', None)
+        root = document.documentElement
+
+        for name in settings:
+            exporter = getMultiAdapter((settings[name], root), IDOMAdapter)
+            exporter.setDocument(document)
+            exporter.save()
+
+        return document.toxml()
 
     def importSettings(self, xml=u''):
         """Import site settings
@@ -132,9 +144,8 @@
         settings = mgr.getSettings()
 
         dom = parseString(xml)
-        importer = getMultiAdapter((mgr, request), IDataImporter, 
-                                   name=u'settings')
-        return importer.load(dom)
+        importer = getMultiAdapter((mgr, dom), IDOMAdapter, name=u'settings')
+        importer.load()
 
     ###################################################################
     # Site
@@ -154,11 +165,8 @@
         return archive()
 
     def importSite(self, file=None):
-
         archive = TarArchive(mode='r', data=file.read())
-
         mgr = getThemeManager(self.context)
-
         uri_mapping = {}
 
         # themes
@@ -264,14 +272,13 @@
         context = self.context
         request = self.request
 
-        xml = u''
+        document = DOMImplementation().createDocument(None, u'themes', None)
+        root = document.documentElement
+
         for theme in getThemeManager(context).getThemes():
-            exporter = getMultiAdapter((theme, request), IDataExporter)
-            xml += exporter().replace(u'<?xml version="1.0"?>', u'')
+            exporter = getMultiAdapter((theme, root), IDOMAdapter)
+            exporter.setDocument(document)
+            exporter.save()
 
-        return u"""<?xml version="1.0"?>
-<themes>
-  %(content)s
-</themes>
-""" % {'content': xml} 
+        return document.toxml()
 
-- 
http://lists.nuxeo.com/mailman/listinfo/z3lab-checkins

Reply via email to