Back to index

plone3  3.1.7
cachingpolicymgr.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 """Caching policy manager xml adapters and setup handlers.
00014 
00015 $Id: cachingpolicymgr.py 77113 2007-06-26 20:36:26Z 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 exportObjects
00024 from Products.GenericSetup.utils import importObjects
00025 from Products.GenericSetup.utils import NodeAdapterBase
00026 from Products.GenericSetup.utils import XMLAdapterBase
00027 
00028 from Products.CMFCore.interfaces import ICachingPolicy
00029 from Products.CMFCore.interfaces import ICachingPolicyManager
00030 from Products.CMFCore.utils import getToolByName
00031 
00032 
00033 class CachingPolicyNodeAdapter(NodeAdapterBase):
00034 
00035     """Node im- and exporter for CachingPolicy.
00036     """
00037 
00038     adapts(ICachingPolicy, ISetupEnviron)
00039 
00040     def _exportNode(self):
00041         """Export the object as a DOM node.
00042         """
00043         obj = self.context
00044         node = self._doc.createElement('caching-policy')
00045         node.setAttribute('name', obj.getPolicyId())
00046         node.setAttribute('predicate', obj.getPredicate())
00047         node.setAttribute('mtime_func', obj.getMTimeFunc())
00048         node.setAttribute('max_age_secs', str(obj.getMaxAgeSecs()))
00049         node.setAttribute('no_cache', str(bool(obj.getNoCache())))
00050         node.setAttribute('no_store', str(bool(obj.getNoStore())))
00051         node.setAttribute('must_revalidate',
00052                           str(bool(obj.getMustRevalidate())))
00053         node.setAttribute('vary', obj.getVary())
00054         node.setAttribute('etag_func', obj.getETagFunc())
00055         s_max_age_secs = obj.getSMaxAgeSecs()
00056         if s_max_age_secs is not None:
00057             node.setAttribute('s_max_age_secs', str(s_max_age_secs))
00058         node.setAttribute('proxy_revalidate',
00059                           str(bool(obj.getProxyRevalidate())))
00060         node.setAttribute('public', str(bool(obj.getPublic())))
00061         node.setAttribute('private', str(bool(obj.getPrivate())))
00062         node.setAttribute('no_transform', str(bool(obj.getNoTransform())))
00063         node.setAttribute('enable_304s', str(bool(obj.getEnable304s())))
00064         node.setAttribute('last_modified', str(bool(obj.getLastModified())))
00065         pre_check = obj.getPreCheck()
00066         if pre_check is not None:
00067             node.setAttribute('pre_check', str(pre_check))
00068         post_check = obj.getPostCheck()
00069         if post_check is not None:
00070             node.setAttribute('post_check', str(post_check))
00071         return node
00072 
00073     def _importNode(self, node):
00074         """Import the object from the DOM node.
00075         """
00076         info = {}
00077         policy_id = node.getAttribute('name')
00078         info['policy_id'] = str(policy_id)
00079         info['predicate'] = str(node.getAttribute('predicate'))
00080         info['mtime_func'] = str(node.getAttribute('mtime_func'))
00081         info['max_age_secs'] = int(node.getAttribute('max_age_secs'))
00082         no_cache = node.getAttribute('no_cache')
00083         info['no_cache'] = self._convertToBoolean(no_cache)
00084         no_store = node.getAttribute('no_store')
00085         info['no_store'] = self._convertToBoolean(no_store)
00086         must_revalidate = node.getAttribute('must_revalidate')
00087         info['must_revalidate'] = self._convertToBoolean(must_revalidate)
00088         info['vary'] = str(node.getAttribute('vary'))
00089         info['etag_func'] = str(node.getAttribute('etag_func'))
00090         s_max_age_secs = node.getAttribute('s_max_age_secs')
00091         if s_max_age_secs != '':
00092             info['s_max_age_secs'] = int(s_max_age_secs)
00093         proxy_revalidate = node.getAttribute('proxy_revalidate')
00094         info['proxy_revalidate'] = self._convertToBoolean(proxy_revalidate)
00095         info['public'] = self._convertToBoolean(node.getAttribute('public'))
00096         info['private'] = self._convertToBoolean(node.getAttribute('private'))
00097         no_transform = node.getAttribute('no_transform')
00098         info['no_transform'] = self._convertToBoolean(no_transform)
00099         enable_304s = node.getAttribute('enable_304s')
00100         info['enable_304s'] = self._convertToBoolean(enable_304s)
00101         last_modified = node.getAttribute('last_modified')
00102         info['last_modified'] = self._convertToBoolean(last_modified)
00103         pre_check = node.getAttribute('pre_check')
00104         if pre_check != '':
00105             info['pre_check'] = int(pre_check)
00106         post_check = node.getAttribute('post_check')
00107         if post_check != '':
00108             info['post_check'] = int(post_check)
00109         self.context.__init__(**info)
00110 
00111     node = property(_exportNode, _importNode)
00112 
00113 
00114 class CachingPolicyManagerXMLAdapter(XMLAdapterBase):
00115 
00116     """XML im- and exporter for CachingPolicyManager.
00117     """
00118 
00119     adapts(ICachingPolicyManager, ISetupEnviron)
00120 
00121     _LOGGER_ID = 'cachingpolicies'
00122 
00123     name = 'cachingpolicymgr'
00124 
00125     def _exportNode(self):
00126         """Export the object as a DOM node.
00127         """
00128         node = self._getObjectNode('object')
00129         node.appendChild(self._extractCachingPolicies())
00130 
00131         self._logger.info('Caching policy manager exported.')
00132         return node
00133 
00134     def _importNode(self, node):
00135         """Import the object from the DOM node.
00136         """
00137         if self.environ.shouldPurge():
00138             self._purgeCachingPolicies()
00139 
00140         self._initCachingPolicies(node)
00141 
00142         self._logger.info('Caching policy manager imported.')
00143 
00144     def _extractCachingPolicies(self):
00145         fragment = self._doc.createDocumentFragment()
00146         for policy_id, policy in self.context.listPolicies():
00147             exporter = queryMultiAdapter((policy, self.environ), INode)
00148             if exporter:
00149                 fragment.appendChild(exporter.node)
00150         return fragment
00151 
00152     def _purgeCachingPolicies(self):
00153         self.context.__init__()
00154 
00155     def _initCachingPolicies(self, node):
00156         for child in node.childNodes:
00157             if child.nodeName != 'caching-policy':
00158                 continue
00159             parent = self.context
00160 
00161             policy_id = str(child.getAttribute('name'))
00162             if policy_id not in parent._policy_ids:
00163                 parent.addPolicy(policy_id, 'python:1', 'object/modified',
00164                                  0, 0, 0, 0, '', '')
00165 
00166             policy = self.context._policies[policy_id]
00167             importer = queryMultiAdapter((policy, self.environ), INode)
00168             if importer:
00169                 importer.node = child
00170 
00171 
00172 def importCachingPolicyManager(context):
00173     """Import caching policy manager settings from an XML file.
00174     """
00175     site = context.getSite()
00176     tool = getToolByName(site, 'caching_policy_manager')
00177 
00178     importObjects(tool, '', context)
00179 
00180 def exportCachingPolicyManager(context):
00181     """Export caching policy manager settings as an XML file.
00182     """
00183     site = context.getSite()
00184     tool = getToolByName(site, 'caching_policy_manager', None)
00185     if tool is None:
00186         logger = context.getLogger('cachingpolicies')
00187         logger.info('Nothing to export.')
00188         return
00189 
00190     exportObjects(tool, '', context)