Back to index

plone3  3.1.7
archetypetool.py
Go to the documentation of this file.
00001 from sets import Set
00002 
00003 from Products.CMFCore.utils import getToolByName
00004 from Products.Archetypes.interfaces import IArchetypeTool
00005 from Products.Archetypes.config import TOOL_NAME
00006 from Products.GenericSetup.utils import XMLAdapterBase
00007 from Products.GenericSetup.utils import exportObjects
00008 from Products.GenericSetup.utils import importObjects
00009 
00010 
00011 class ArchetypeToolXMLAdapter(XMLAdapterBase):
00012     """Mode in- and exporter for ArchetypesTool.
00013     """
00014 
00015     __used_for__ = IArchetypeTool
00016 
00017     def _exportNode(self):
00018         """Export the object as a DOM node.
00019         """
00020         node=self._doc.createElement('archetypetool')
00021         node.appendChild(self._extractCatalogSettings())
00022 
00023         self._logger.info('ArchetypeTool settings exported.')
00024         return node
00025 
00026 
00027     def _importNode(self, node):
00028         if self.environ.shouldPurge():
00029             self._purgeCatalogSettings()
00030 
00031         self._initCatalogSettings(node)
00032         self._logger.info('ArchetypeTool settings imported.')
00033     
00034 
00035 
00036     def _purgeCatalogSettings(self):
00037         self.context.catalog_map.clear()
00038 
00039 
00040     def _initCatalogSettings(self, node):
00041         for child in node.childNodes:
00042             if child.nodeName=='catalogmap':
00043                 for type in child.getElementsByTagName('type'):
00044                     portaltype=type.getAttribute('portal_type')
00045                     catalogs=[e.getAttribute('value') \
00046                               for e in type.getElementsByTagName('catalog')]
00047                     already = [cat.getId() for cat in
00048                                self.context.getCatalogsByType(portaltype)]
00049                     catalogs=Set(catalogs + already)
00050                     self.context.setCatalogsByType(portaltype, list(catalogs))
00051 
00052 
00053     def _extractCatalogSettings(self):
00054         node=self._doc.createElement('catalogmap')
00055         for type in self.context.listRegisteredTypes(True):
00056             child=self._doc.createElement('type')
00057             child.setAttribute('portal_type', type['name'])
00058             for cat in self.context.getCatalogsByType(type['name']):
00059                 sub=self._doc.createElement('catalog')
00060                 sub.setAttribute('value', cat.id)
00061                 child.appendChild(sub)
00062             node.appendChild(child)
00063 
00064         return node
00065 
00066 
00067 def importArchetypeTool(context):
00068     """Import Archetype Tool configuration.
00069     """
00070     site = context.getSite()
00071     tool = getToolByName(site, TOOL_NAME)
00072     importObjects(tool, '', context)
00073 
00074 
00075 def exportArchetypeTool(context):
00076     """Export Archetype Tool configuration.
00077     """
00078     site = context.getSite()
00079     tool = getToolByName(site, TOOL_NAME, None)
00080     if tool is None:
00081         logger = context.getLogger("archetypestool")
00082         logger.info("Nothing to export.")
00083         return
00084 
00085     exportObjects(tool, '', context)