vendor/CMF/1.5.3/CMFSetup

view skins.py @ 0:3ed006215eb6

Vendor import of CMF 1.5.3
author fguillaume
date Tue, 09 Aug 2005 10:47:34 +0000
parents
children
line source
1 ##############################################################################
2 #
3 # Copyright (c) 2004 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 """ Skin path configuration management
15 Setup step and export script
17 $Id: skins.py 37010 2005-05-07 12:44:18Z jens $
18 """
20 import re
22 from AccessControl import ClassSecurityInfo
23 from Globals import InitializeClass
24 from Products.PageTemplates.PageTemplateFile import PageTemplateFile
26 from Products.CMFCore.utils import getToolByName
27 from Products.CMFCore.utils import minimalpath
28 from Products.CMFCore.DirectoryView import createDirectoryView
29 from Products.CMFCore.DirectoryView import DirectoryView
31 from permissions import ManagePortal
32 from utils import _xmldir
33 from utils import ConfiguratorBase
34 from utils import CONVERTER, DEFAULT, KEY
37 #
38 # Entry points
39 #
40 _FILENAME = 'skins.xml'
42 def importSkinsTool( context ):
44 """ Import skins tool FSDirViews and skin paths from an XML file
46 o 'context' must implement IImportContext.
48 o Register via Python:
50 registry = site.portal_setup.getImportStepRegistry()
51 registry.registerStep( 'importSkinsTool'
52 , '20040518-01'
53 , Products.CMFSetup.skins.importSkinsTool
54 , ()
55 , 'Skins Tool import'
56 , 'Import skins tool FSDVs and skin paths.'
57 )
59 o Register via XML:
61 <setup-step id="importSkinsTool"
62 version="20040524-01"
63 handler="Products.CMFSetup.skins.importSkinsTool"
64 title="Skins Tool import"
65 >Import skins tool FSDVs and skin paths.</setup-step>
67 """
68 site = context.getSite()
69 encoding = context.getEncoding()
71 stool = getToolByName(site, 'portal_skins')
73 if context.shouldPurge():
75 stool.request_varname = 'portal_skin'
76 stool.allow_any = 0
77 stool.cookie_persistence = 0
79 stool._getSelections().clear()
81 for id in stool.objectIds(DirectoryView.meta_type):
82 stool._delObject(id)
84 text = context.readDataFile( _FILENAME )
86 if text is not None:
88 stc = SkinsToolConfigurator( site, encoding )
89 tool_info = stc.parseXML( text )
91 if 'default_skin' in tool_info:
92 stool.default_skin = str(tool_info['default_skin'])
93 if 'request_varname' in tool_info:
94 stool.request_varname = str(tool_info['request_varname'])
95 if 'allow_any' in tool_info:
96 stool.allow_any = tool_info['allow_any'] and 1 or 0
97 if 'cookie_persistence' in tool_info:
98 stool.cookie_persistence = \
99 tool_info['cookie_persistence'] and 1 or 0
101 for dir_info in tool_info['skin_dirs']:
102 dir_id = dir_info['id']
103 if dir_id in stool.objectIds(DirectoryView.meta_type):
104 stool._delObject(dir_id)
105 createDirectoryView(stool, dir_info['directory'], dir_id)
107 for path_info in tool_info['skin_paths']:
108 path_id = path_info['id']
109 if path_id == '*':
110 for path_id, path in stool._getSelections().items():
111 path = _updatePath(path, path_info['layers'])
112 stool.addSkinSelection(path_id, path)
113 else:
114 if stool._getSelections().has_key(path_id):
115 path = stool._getSelections()[path_id]
116 else:
117 path = ''
118 path = _updatePath(path, path_info['layers'])
119 stool.addSkinSelection(path_id, path)
121 #
122 # Purge and rebuild the skin path, now that we have added our stuff.
123 # Don't bother if no REQUEST is present, e.g. when running unit tests
124 #
125 request = getattr(site, 'REQUEST', None)
126 if request is not None:
127 site.clearCurrentSkin()
128 site.setupCurrentSkin(request)
130 return 'Skins tool imported'
132 def _updatePath(path, layer_infos):
133 path = [ name.strip() for name in path.split(',') if name.strip() ]
135 for layer in layer_infos:
136 if layer['name'] in path:
137 path.remove(layer['name'])
139 if 'insert-before' in layer:
140 if layer['insert-before'] == '*':
141 path.insert(0, layer['name'])
142 continue
143 else:
144 try:
145 index = path.index(layer['insert-before'])
146 path.insert(index, layer['name'])
147 continue
148 except ValueError:
149 pass
150 elif 'insert-after' in layer:
151 if layer['insert-after'] == '*':
152 pass
153 else:
154 try:
155 index = path.index(layer['insert-after'])
156 path.insert(index+1, layer['name'])
157 continue
158 except ValueError:
159 pass
161 if not 'remove' in layer:
162 path.append(layer['name'])
164 return str( ','.join(path) )
166 def exportSkinsTool( context ):
168 """ Export skins tool FSDVs and skin paths as an XML file
170 o 'context' must implement IExportContext.
172 o Register via Python:
174 registry = site.portal_setup.getExportStepRegistry()
175 registry.registerStep( 'exportSkinsTool'
176 , Products.CMFSetup.skins.exportSkinsTool
177 , 'Skins Tool export'
178 , 'Export skins tool FSDVs and skin paths.'
179 )
181 o Register via XML:
183 <export-script id="exportSkinsTool"
184 version="20040518-01"
185 handler="Products.CMFSetup.skins.exportSkinsTool"
186 title="Skins Tool export"
187 >Export skins tool FSDVs and skin paths.</export-script>
189 """
190 site = context.getSite()
191 stc = SkinsToolConfigurator( site ).__of__( site )
192 text = stc.generateXML()
194 context.writeDataFile( _FILENAME, text, 'text/xml' )
196 return 'Skins tool exported.'
199 class SkinsToolConfigurator(ConfiguratorBase):
201 security = ClassSecurityInfo()
203 _COMMA_SPLITTER = re.compile( r',[ ]*' )
205 security.declareProtected(ManagePortal, 'getToolInfo' )
206 def getToolInfo( self ):
208 """ Return the tool's settings.
209 """
210 stool = getToolByName(self._site, 'portal_skins')
212 return { 'default_skin': stool.default_skin,
213 'request_varname': stool.request_varname,
214 'allow_any': stool.allow_any,
215 'cookie_persistence': stool.cookie_persistence }
217 security.declareProtected(ManagePortal, 'listSkinPaths' )
218 def listSkinPaths( self ):
220 """ Return a sequence of mappings for each skin path in the tool.
222 o Keys include:
224 'id' -- folder ID
226 'path' -- sequence of layer IDs
227 """
228 stool = getToolByName(self._site, 'portal_skins')
230 return [ { 'id' : k
231 , 'path' : self._COMMA_SPLITTER.split( v )
232 } for k, v in stool.getSkinPaths() ]
234 security.declareProtected(ManagePortal, 'listFSDirectoryViews' )
235 def listFSDirectoryViews( self ):
237 """ Return a sequence of mappings for each FSDV.
239 o Keys include:
241 'id' -- FSDV ID
243 'directory' -- filesystem path of the FSDV.
244 """
245 result = []
246 stool = getToolByName(self._site, 'portal_skins')
248 fsdvs = stool.objectItems( DirectoryView.meta_type )
249 fsdvs.sort()
251 for id, fsdv in fsdvs:
253 dirpath = fsdv._dirpath
255 if dirpath.startswith( '/' ):
256 dirpath = minimalpath( fsdv._dirpath )
258 result.append( { 'id' : id
259 , 'directory' : dirpath
260 } )
262 return result
264 def _getExportTemplate(self):
266 return PageTemplateFile('stcExport.xml', _xmldir)
268 def _getImportMapping(self):
270 return {
271 'skins-tool':
272 { 'default_skin': {},
273 'request_varname': {},
274 'allow_any': {CONVERTER: self._convertToBoolean},
275 'cookie_persistence': {CONVERTER: self._convertToBoolean},
276 'skin-directory': {KEY: 'skin_dirs', DEFAULT: ()},
277 'skin-path': {KEY: 'skin_paths', DEFAULT: ()} },
278 'skin-directory':
279 { 'id': {},
280 'directory': {} },
281 'skin-path':
282 { 'id': {},
283 'layer': {KEY: 'layers', DEFAULT: ()} },
284 'layer':
285 { 'name': {},
286 'insert-after': {},
287 'insert-before': {},
288 'remove': {} } }
290 InitializeClass(SkinsToolConfigurator)