Back to index

plone3  3.1.7
exportimport.py
Go to the documentation of this file.
00001 ##############################################################################
00002 #
00003 # Copyright (c) 2005 Zope Corporation and Contributors. All Rights Reserved.
00004 #
00005 # This software is subject to the provisions of the Zope Public License,
00006 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
00007 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
00008 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00009 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
00010 # FOR A PARTICULAR PURPOSE.
00011 #
00012 ##############################################################################
00013 """PluginIndexes export / import support.
00014 
00015 $Id: exportimport.py 71199 2006-11-20 11:03:29Z yuppie $
00016 """
00017 
00018 from zope.component import adapts
00019 from zope.component import queryMultiAdapter
00020 
00021 from Products.GenericSetup.interfaces import INode
00022 from Products.GenericSetup.interfaces import ISetupEnviron
00023 from Products.GenericSetup.utils import NodeAdapterBase
00024 from Products.GenericSetup.utils import PropertyManagerHelpers
00025 
00026 from Products.PluginIndexes.interfaces import IDateIndex
00027 from Products.PluginIndexes.interfaces import IDateRangeIndex
00028 from Products.PluginIndexes.interfaces import IFilteredSet
00029 from Products.PluginIndexes.interfaces import IPathIndex
00030 from Products.PluginIndexes.interfaces import IPluggableIndex
00031 from Products.PluginIndexes.interfaces import ITextIndex
00032 from Products.PluginIndexes.interfaces import ITopicIndex
00033 from Products.PluginIndexes.interfaces import IVocabulary
00034 
00035 
00036 class PluggableIndexNodeAdapter(NodeAdapterBase):
00037 
00038     """Node im- and exporter for FieldIndex, KeywordIndex.
00039     """
00040 
00041     adapts(IPluggableIndex, ISetupEnviron)
00042 
00043     def _exportNode(self):
00044         """Export the object as a DOM node.
00045         """
00046         node = self._getObjectNode('index')
00047         for value in self.context.getIndexSourceNames():
00048             child = self._doc.createElement('indexed_attr')
00049             child.setAttribute('value', value)
00050             node.appendChild(child)
00051         return node
00052 
00053     def _importNode(self, node):
00054         """Import the object from the DOM node.
00055         """
00056         indexed_attrs = []
00057         for child in node.childNodes:
00058             if child.nodeName == 'indexed_attr':
00059                 indexed_attrs.append(
00060                                   child.getAttribute('value').encode('utf-8'))
00061         self.context.indexed_attrs = indexed_attrs
00062         self.context.clear()
00063 
00064     node = property(_exportNode, _importNode)
00065 
00066 
00067 class DateIndexNodeAdapter(NodeAdapterBase, PropertyManagerHelpers):
00068 
00069     """Node im- and exporter for DateIndex.
00070     """
00071 
00072     adapts(IDateIndex, ISetupEnviron)
00073 
00074     def _exportNode(self):
00075         """Export the object as a DOM node.
00076         """
00077         node = self._getObjectNode('index')
00078         node.appendChild(self._extractProperties())
00079         return node
00080 
00081     def _importNode(self, node):
00082         """Import the object from the DOM node.
00083         """
00084         if self.environ.shouldPurge():
00085             self._purgeProperties()
00086 
00087         self._initProperties(node)
00088         self.context.clear()
00089 
00090     node = property(_exportNode, _importNode)
00091 
00092 
00093 class DateRangeIndexNodeAdapter(NodeAdapterBase):
00094 
00095     """Node im- and exporter for DateRangeIndex.
00096     """
00097 
00098     adapts(IDateRangeIndex, ISetupEnviron)
00099 
00100     def _exportNode(self):
00101         """Export the object as a DOM node.
00102         """
00103         node = self._getObjectNode('index')
00104         node.setAttribute('since_field', self.context.getSinceField())
00105         node.setAttribute('until_field', self.context.getUntilField())
00106         return node
00107 
00108     def _importNode(self, node):
00109         """Import the object from the DOM node.
00110         """
00111         self.context._edit(node.getAttribute('since_field').encode('utf-8'),
00112                            node.getAttribute('until_field').encode('utf-8'))
00113         self.context.clear()
00114 
00115     node = property(_exportNode, _importNode)
00116 
00117 
00118 class PathIndexNodeAdapter(NodeAdapterBase):
00119 
00120     """Node im- and exporter for PathIndex.
00121     """
00122 
00123     adapts(IPathIndex, ISetupEnviron)
00124 
00125     def _exportNode(self):
00126         """Export the object as a DOM node.
00127         """
00128         return self._getObjectNode('index')
00129 
00130     node = property(_exportNode, lambda self, val: None)
00131 
00132 
00133 class VocabularyNodeAdapter(NodeAdapterBase):
00134 
00135     """Node im- and exporter for Vocabulary.
00136     """
00137 
00138     adapts(IVocabulary, ISetupEnviron)
00139 
00140     def _exportNode(self):
00141         """Export the object as a DOM node.
00142         """
00143         node = self._getObjectNode('object')
00144         node.setAttribute('deprecated', 'True')
00145         return node
00146 
00147     node = property(_exportNode, lambda self, val: None)
00148 
00149 
00150 class TextIndexNodeAdapter(NodeAdapterBase):
00151 
00152     """Node im- and exporter for TextIndex.
00153     """
00154 
00155     adapts(ITextIndex, ISetupEnviron)
00156 
00157     def _exportNode(self):
00158         """Export the object as a DOM node.
00159         """
00160         node = self._getObjectNode('index')
00161         node.setAttribute('deprecated', 'True')
00162         return node
00163 
00164     node = property(_exportNode, lambda self, val: None)
00165 
00166 
00167 class FilteredSetNodeAdapter(NodeAdapterBase):
00168 
00169     """Node im- and exporter for FilteredSet.
00170     """
00171 
00172     adapts(IFilteredSet, ISetupEnviron)
00173 
00174     def _exportNode(self):
00175         """Export the object as a DOM node.
00176         """
00177         node = self._getObjectNode('filtered_set')
00178         node.setAttribute('expression', self.context.getExpression())
00179         return node
00180 
00181     def _importNode(self, node):
00182         """Import the object from the DOM node.
00183         """
00184         self.context.setExpression(
00185                               node.getAttribute('expression').encode('utf-8'))
00186         self.context.clear()
00187 
00188     node = property(_exportNode, _importNode)
00189 
00190 
00191 class TopicIndexNodeAdapter(NodeAdapterBase):
00192 
00193     """Node im- and exporter for TopicIndex.
00194     """
00195 
00196     adapts(ITopicIndex, ISetupEnviron)
00197 
00198     def _exportNode(self):
00199         """Export the object as a DOM node.
00200         """
00201         node = self._getObjectNode('index')
00202         for set in self.context.filteredSets.values():
00203             exporter = queryMultiAdapter((set, self.environ), INode)
00204             node.appendChild(exporter.node)
00205         return node
00206 
00207     def _importNode(self, node):
00208         """Import the object from the DOM node.
00209         """
00210         for child in node.childNodes:
00211             if child.nodeName == 'filtered_set':
00212                 set_id = str(child.getAttribute('name'))
00213                 if set_id not in self.context.filteredSets:
00214                     set_meta_type = str(child.getAttribute('meta_type'))
00215                     self.context.addFilteredSet(set_id, set_meta_type, '')
00216                 set = self.context.filteredSets[set_id]
00217                 importer = queryMultiAdapter((set, self.environ), INode)
00218                 importer.node = child
00219         self.context.clear()
00220 
00221     node = property(_exportNode, _importNode)