Back to index

plone3  3.1.7
properties.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 """Site properties xml adapters and setup handlers.
00014 
00015 $Id: properties.py 72049 2007-01-16 11:06:30Z yuppie $
00016 """
00017 
00018 from zope.component import adapts
00019 from zope.component import queryMultiAdapter
00020 
00021 from Products.GenericSetup.interfaces import IBody
00022 from Products.GenericSetup.interfaces import ISetupEnviron
00023 from Products.GenericSetup.utils import PropertyManagerHelpers
00024 from Products.GenericSetup.utils import XMLAdapterBase
00025 
00026 from Products.CMFCore.interfaces import ISiteRoot
00027 
00028 _FILENAME = 'properties.xml'
00029 
00030 
00031 class PropertiesXMLAdapter(XMLAdapterBase, PropertyManagerHelpers):
00032 
00033     """XML im- and exporter for properties.
00034     """
00035 
00036     adapts(ISiteRoot, ISetupEnviron)
00037 
00038     _LOGGER_ID = 'properties'
00039 
00040     def _exportNode(self):
00041         """Export the object as a DOM node.
00042         """
00043         self._encoding = self.context.getProperty('default_charset', 'utf-8')
00044 
00045         node = self._doc.createElement('site')
00046         node.appendChild(self._extractProperties())
00047 
00048         self._logger.info('Site properties exported.')
00049         return node
00050 
00051     def _importNode(self, node):
00052         """Import the object from the DOM node.
00053         """
00054         for child in node.childNodes:
00055             if child.nodeName != 'property':
00056                 continue
00057             if child.getAttribute('name') != 'default_charset':
00058                 continue
00059             self._encoding = self._getNodeText(child) or 'utf-8'
00060             break
00061 
00062         if self.environ.shouldPurge():
00063             self._purgeProperties()
00064 
00065         self._initProperties(node)
00066 
00067         self._logger.info('Site properties imported.')
00068 
00069 
00070 def importSiteProperties(context):
00071     """ Import site properties from an XML file.
00072     """
00073     site = context.getSite()
00074     logger = context.getLogger('properties')
00075 
00076     body = context.readDataFile(_FILENAME)
00077     if body is None:
00078         logger.info('Nothing to import.')
00079         return
00080 
00081     importer = queryMultiAdapter((site, context), IBody)
00082     if importer is None:
00083         logger.warning('Import adapter missing.')
00084         return
00085 
00086     importer.body = body
00087 
00088 def exportSiteProperties(context):
00089     """ Export site properties as an XML file.
00090     """
00091     site = context.getSite()
00092     logger = context.getLogger('properties')
00093 
00094     exporter = queryMultiAdapter((site, context), IBody)
00095     if exporter is None:
00096         logger.warning('Export adapter missing.')
00097         return
00098 
00099     context.writeDataFile(_FILENAME, exporter.body, exporter.mime_type)