vendor/CMF/1.6.3/CMFCore

view exportimport/cachingpolicymgr.py @ 0:587011552858

import CMF 1.6.3
author bdelbosc
date Mon, 23 Apr 2007 13:58:01 +0000
parents
children
line source
1 ##############################################################################
2 #
3 # Copyright (c) 2005 Zope Corporation and Contributors. All Rights Reserved.
4 #
5 # This software is subject to the provisions of the Zope Public License,
6 # Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
7 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
10 # FOR A PARTICULAR PURPOSE.
11 #
12 ##############################################################################
13 """Caching policy manager xml adapters and setup handlers.
15 $Id$
16 """
18 from zope.app import zapi
20 from Products.GenericSetup.utils import exportObjects
21 from Products.GenericSetup.utils import importObjects
22 from Products.GenericSetup.interfaces import INode
23 from Products.GenericSetup.utils import NodeAdapterBase
24 from Products.GenericSetup.utils import XMLAdapterBase
26 from Products.CMFCore.interfaces import ICachingPolicy
27 from Products.CMFCore.interfaces import ICachingPolicyManager
28 from Products.CMFCore.utils import getToolByName
31 class CachingPolicyNodeAdapter(NodeAdapterBase):
33 """Node im- and exporter for CachingPolicy.
34 """
36 __used_for__ = ICachingPolicy
38 def _exportNode(self):
39 """Export the object as a DOM node.
40 """
41 obj = self.context
42 node = self._doc.createElement('caching-policy')
43 node.setAttribute('name', obj.getPolicyId())
44 node.setAttribute('predicate', obj.getPredicate())
45 node.setAttribute('mtime_func', obj.getMTimeFunc())
46 node.setAttribute('max_age_secs', str(obj.getMaxAgeSecs()))
47 node.setAttribute('no_cache', str(bool(obj.getNoCache())))
48 node.setAttribute('no_store', str(bool(obj.getNoStore())))
49 node.setAttribute('must_revalidate',
50 str(bool(obj.getMustRevalidate())))
51 node.setAttribute('vary', obj.getVary())
52 node.setAttribute('etag_func', obj.getETagFunc())
53 s_max_age_secs = obj.getSMaxAgeSecs()
54 if s_max_age_secs is not None:
55 node.setAttribute('s_max_age_secs', str(s_max_age_secs))
56 node.setAttribute('proxy_revalidate',
57 str(bool(obj.getProxyRevalidate())))
58 node.setAttribute('public', str(bool(obj.getPublic())))
59 node.setAttribute('private', str(bool(obj.getPrivate())))
60 node.setAttribute('no_transform', str(bool(obj.getNoTransform())))
61 node.setAttribute('enable_304s', str(bool(obj.getEnable304s())))
62 node.setAttribute('last_modified', str(bool(obj.getLastModified())))
63 pre_check = obj.getPreCheck()
64 if pre_check is not None:
65 node.setAttribute('pre_check', str(pre_check))
66 post_check = obj.getPostCheck()
67 if post_check is not None:
68 node.setAttribute('post_check', str(post_check))
69 return node
71 def _importNode(self, node):
72 """Import the object from the DOM node.
73 """
74 info = {}
75 policy_id = node.getAttribute('name')
76 if not policy_id:
77 # BBB: for CMF 1.5 profiles
78 policy_id = node.getAttribute('policy_id')
79 info['policy_id'] = str(policy_id)
80 info['predicate'] = str(node.getAttribute('predicate'))
81 info['mtime_func'] = str(node.getAttribute('mtime_func'))
82 info['max_age_secs'] = int(node.getAttribute('max_age_secs'))
83 no_cache = node.getAttribute('no_cache')
84 info['no_cache'] = self._convertToBoolean(no_cache)
85 no_store = node.getAttribute('no_store')
86 info['no_store'] = self._convertToBoolean(no_store)
87 must_revalidate = node.getAttribute('must_revalidate')
88 info['must_revalidate'] = self._convertToBoolean(must_revalidate)
89 info['vary'] = str(node.getAttribute('vary'))
90 info['etag_func'] = str(node.getAttribute('etag_func'))
91 s_max_age_secs = node.getAttribute('s_max_age_secs')
92 if s_max_age_secs != '':
93 info['s_max_age_secs'] = int(s_max_age_secs)
94 proxy_revalidate = node.getAttribute('proxy_revalidate')
95 info['proxy_revalidate'] = self._convertToBoolean(proxy_revalidate)
96 info['public'] = self._convertToBoolean(node.getAttribute('public'))
97 info['private'] = self._convertToBoolean(node.getAttribute('private'))
98 no_transform = node.getAttribute('no_transform')
99 info['no_transform'] = self._convertToBoolean(no_transform)
100 enable_304s = node.getAttribute('enable_304s')
101 info['enable_304s'] = self._convertToBoolean(enable_304s)
102 last_modified = node.getAttribute('last_modified')
103 info['last_modified'] = self._convertToBoolean(last_modified)
104 pre_check = node.getAttribute('pre_check')
105 if pre_check != '':
106 info['pre_check'] = int(pre_check)
107 post_check = node.getAttribute('post_check')
108 if post_check != '':
109 info['post_check'] = int(post_check)
110 self.context.__init__(**info)
112 node = property(_exportNode, _importNode)
115 class CachingPolicyManagerXMLAdapter(XMLAdapterBase):
117 """XML im- and exporter for CachingPolicyManager.
118 """
120 __used_for__ = ICachingPolicyManager
122 _LOGGER_ID = 'cachingpolicies'
124 name = 'cachingpolicymgr'
126 def _exportNode(self):
127 """Export the object as a DOM node.
128 """
129 node = self._getObjectNode('object')
130 node.appendChild(self._extractCachingPolicies())
132 self._logger.info('Caching policy manager exported.')
133 return node
135 def _importNode(self, node):
136 """Import the object from the DOM node.
137 """
138 if self.environ.shouldPurge():
139 self._purgeCachingPolicies()
141 self._initCachingPolicies(node)
143 self._logger.info('Caching policy manager imported.')
145 def _extractCachingPolicies(self):
146 fragment = self._doc.createDocumentFragment()
147 for policy_id, policy in self.context.listPolicies():
148 exporter = zapi.queryMultiAdapter((policy, self.environ), INode)
149 if exporter:
150 fragment.appendChild(exporter.node)
151 return fragment
153 def _purgeCachingPolicies(self):
154 self.context.__init__()
156 def _initCachingPolicies(self, node):
157 for child in node.childNodes:
158 if child.nodeName != 'caching-policy':
159 continue
160 parent = self.context
162 policy_id = str(child.getAttribute('name'))
163 if policy_id not in parent._policy_ids:
164 parent.addPolicy(policy_id, 'python:1', 'object/modified',
165 0, 0, 0, 0, '', '')
167 policy = self.context._policies[policy_id]
168 importer = zapi.queryMultiAdapter((policy, self.environ), INode)
169 if importer:
170 importer.node = child
173 def importCachingPolicyManager(context):
174 """Import caching policy manager settings from an XML file.
175 """
176 site = context.getSite()
177 tool = getToolByName(site, 'caching_policy_manager')
179 importObjects(tool, '', context)
181 def exportCachingPolicyManager(context):
182 """Export caching policy manager settings as an XML file.
183 """
184 site = context.getSite()
185 tool = getToolByName(site, 'caching_policy_manager', None)
186 if tool is None:
187 logger = context.getLogger('cachingpolicies')
188 logger.info('Nothing to export.')
189 return
191 exportObjects(tool, '', context)