1 | from zope import interface |
---|
2 | from zope import component |
---|
3 | from Products.CMFCore.utils import getToolByName |
---|
4 | from Products.GenericSetup import interfaces as gsinterfaces |
---|
5 | from Products.CacheSetup import interfaces |
---|
6 | from Products.CacheSetup.exportimport import atcontent |
---|
7 | |
---|
8 | _PROJECT = 'CacheFu' |
---|
9 | _FILENAME = 'cachesettings.xml' |
---|
10 | |
---|
11 | def importSetup(context): |
---|
12 | """ Import setup. |
---|
13 | """ |
---|
14 | site = context.getSite() |
---|
15 | logger = context.getLogger(_PROJECT) |
---|
16 | |
---|
17 | body = context.readDataFile(_FILENAME) |
---|
18 | if body is None: |
---|
19 | logger.info('Nothing to import.') |
---|
20 | return |
---|
21 | |
---|
22 | repo_tool = getToolByName(site, 'portal_cache_settings', None) |
---|
23 | if repo_tool is None: |
---|
24 | logger.info('Can not import without cache tool') |
---|
25 | return |
---|
26 | |
---|
27 | importer = component.queryMultiAdapter((repo_tool, context), gsinterfaces.IBody) |
---|
28 | if importer is None: |
---|
29 | logger.warning('Import adapter misssing.') |
---|
30 | return |
---|
31 | |
---|
32 | importer.body = body |
---|
33 | logger.info('setup imported.') |
---|
34 | |
---|
35 | def exportSetup(context): |
---|
36 | """ Export setup. |
---|
37 | """ |
---|
38 | site = context.getSite() |
---|
39 | logger = context.getLogger(_PROJECT) |
---|
40 | |
---|
41 | repo_tool = getToolByName(site, 'portal_cache_settings', None) |
---|
42 | if repo_tool is None: |
---|
43 | logger.info('Nothing to export.') |
---|
44 | return |
---|
45 | |
---|
46 | exporter = component.queryMultiAdapter((repo_tool, context), gsinterfaces.IBody) |
---|
47 | if exporter is None: |
---|
48 | return '%s: Export adapter misssing.' % _PROJECT |
---|
49 | |
---|
50 | context.writeDataFile(_FILENAME, exporter.body, exporter.mime_type) |
---|
51 | logger.info('setup exported.') |
---|
52 | |
---|
53 | |
---|
54 | class CacheSettingsAdapter(atcontent.ATContentAdapterBase): |
---|
55 | _LOGGER_ID = "CacheSetup" |
---|
56 | name = "cachesettings" |
---|
57 | |
---|
58 | _fields = ['enabled', 'activePolicyId', 'proxyPurgeConfig', 'domains', |
---|
59 | 'squidURLs', 'gzip', 'varyHeader'] |
---|
60 | _pagecachemanager_fields = ["threshold", |
---|
61 | "cleanup_interval", |
---|
62 | "max_age", |
---|
63 | "active"] |
---|
64 | |
---|
65 | def _exportNode(self): |
---|
66 | node = super(CacheSettingsAdapter, self)._exportNode() |
---|
67 | node.appendChild(self._extractPageCacheManager()) |
---|
68 | return node |
---|
69 | |
---|
70 | |
---|
71 | def _extractPageCacheManager(self): |
---|
72 | fragment = self._doc.createDocumentFragment() |
---|
73 | mgr = getToolByName(self.context, "CacheSetup_PageCache", None) |
---|
74 | if mgr is None: |
---|
75 | return fragment |
---|
76 | |
---|
77 | node = self._doc.createElement("pagecachemanager") |
---|
78 | settings = mgr.getSettings() |
---|
79 | for fieldname in ["threshold", "cleanup_interval", "max_age", "active"]: |
---|
80 | prop = self._doc.createElement("property") |
---|
81 | prop.setAttribute("name", fieldname) |
---|
82 | value = self._convertValueToString(settings[fieldname]) |
---|
83 | child = self._doc.createTextNode(value) |
---|
84 | prop.appendChild(child) |
---|
85 | node.appendChild(prop) |
---|
86 | |
---|
87 | fragment.appendChild(node) |
---|
88 | return fragment |
---|
89 | |
---|
90 | |
---|
91 | def _initPageCacheManager(self, node): |
---|
92 | mgr = getToolByName(self.context, "CacheSetup_PageCache", None) |
---|
93 | if mgr is None: |
---|
94 | return |
---|
95 | |
---|
96 | for child in node.childNodes: |
---|
97 | if child.nodeName != "pagecachemanager": |
---|
98 | continue |
---|
99 | |
---|
100 | newsettings = mgr.getSettings() |
---|
101 | for prop in child.childNodes: |
---|
102 | if prop.nodeName != "property": |
---|
103 | continue |
---|
104 | |
---|
105 | name = str(prop.getAttribute("name")) |
---|
106 | value = self._getNodeText(prop) |
---|
107 | |
---|
108 | if isinstance(mgr._settings[name], int): |
---|
109 | newsettings[name] = int(value) |
---|
110 | else: |
---|
111 | newsettings[name] = value |
---|
112 | |
---|
113 | mgr.manage_editProps(mgr.Title(), newsettings) |
---|
114 | |
---|
115 | |
---|
116 | def _importNode(self, node): |
---|
117 | super(CacheSettingsAdapter, self)._importNode(node) |
---|
118 | self._initPageCacheManager(node) |
---|
119 | |
---|
120 | |
---|
121 | class CachePolicyAdapter(atcontent.ATContentAdapterBase): |
---|
122 | _fields = ["title"] |
---|
123 | |
---|
124 | |
---|
125 | class HeaderSetFolderAdapter(atcontent.ATContentAdapterBase): |
---|
126 | _fields = [] |
---|
127 | |
---|
128 | |
---|
129 | class HeaderSetAdapter(atcontent.ATContentAdapterBase): |
---|
130 | _fields = ['title', 'description', 'pageCache', 'lastModified', |
---|
131 | 'etag', 'enable304s', 'vary', 'maxAge', 'sMaxAge', |
---|
132 | 'mustRevalidate', 'proxyRevalidate', 'noCache', 'noStore', |
---|
133 | 'public', 'private', 'noTransform', 'preCheck', |
---|
134 | 'postCheck' ] |
---|
135 | |
---|
136 | |
---|
137 | class RuleFolderAdapter(atcontent.ATContentAdapterBase): |
---|
138 | _fields = [] |
---|
139 | |
---|
140 | |
---|
141 | |
---|
142 | |
---|
143 | header_set_fields = ['varyExpression', 'lastModifiedExpression', |
---|
144 | 'headerSetIdExpression', 'headerSetIdAuth', |
---|
145 | 'headerSetIdAnon', 'predicateExpression', 'cacheStop'] |
---|
146 | etag_fields = ['etagExpression', 'etagTimeout', 'etagRequestValues', |
---|
147 | 'etagComponents'] |
---|
148 | |
---|
149 | |
---|
150 | class PolicyCacheRuleAdapter(atcontent.ATContentAdapterBase): |
---|
151 | _fields = ['title', 'description', 'cacheManager', 'types', 'ids', |
---|
152 | 'cacheStop'] + header_set_fields |
---|
153 | |
---|
154 | class ContentCacheRuleAdapter(atcontent.ATContentAdapterBase): |
---|
155 | _fields = ['title', 'description', 'contentTypes', 'defaultView', |
---|
156 | 'templates', 'purgeExpression'] + \ |
---|
157 | header_set_fields + etag_fields |
---|
158 | |
---|
159 | |
---|
160 | class TemplateCacheRuleAdapter(atcontent.ATContentAdapterBase): |
---|
161 | _fields = ['title', 'description', 'templates'] + \ |
---|
162 | header_set_fields + etag_fields |
---|
163 | |
---|
164 | @component.adapter(interfaces.ICacheTool) |
---|
165 | @interface.implementer(gsinterfaces.IFilesystemExporter) |
---|
166 | def nullFilesystemExporter(object): |
---|
167 | pass |
---|