[ Index ] |
|
Code source de Kupu-1.3.5 |
1 ############################################################################## 2 # 3 # Copyright (c) 2003-2005 Kupu Contributors. All rights reserved. 4 # 5 # This software is distributed under the terms of the Kupu 6 # License. See LICENSE.txt for license text. For a list of Kupu 7 # Contributors see CREDITS.txt. 8 # 9 ############################################################################## 10 """Install kupu in CMF and, if available, Plone 11 12 This is best executed using CMFQuickInstaller 13 14 $Id: Install.py 18104 2005-10-03 14:10:11Z duncan $ 15 """ 16 import os.path 17 import sys 18 import re 19 from StringIO import StringIO 20 21 from App.Common import package_home 22 23 from Products.CMFCore.utils import getToolByName, minimalpath 24 from Products.CMFCore.DirectoryView import createDirectoryView 25 from Products.kupu import kupu_globals 26 from Products.kupu.config import TOOLNAME, PROJECTNAME, TOOLTITLE 27 from OFS.ObjectManager import BadRequestException 28 from zExceptions import BadRequest 29 30 try: 31 from Products.MimetypesRegistry import MimeTypeItem 32 except ImportError: 33 pass # Plone not available 34 35 kupu_package_dir = package_home(kupu_globals) 36 37 def register_layer(self, relpath, name, out): 38 """Register a file system directory as skin layer 39 """ 40 print >>out, "register skin layers" 41 skinstool = getToolByName(self, 'portal_skins') 42 if name not in skinstool.objectIds(): 43 kupu_plone_skin_dir = minimalpath(os.path.join(kupu_package_dir, relpath)) 44 createDirectoryView(skinstool, kupu_plone_skin_dir, name) 45 print >>out, "The layer '%s' was added to the skins tool" % name 46 47 # put this layer into all known skins 48 for skinName in skinstool.getSkinSelections(): 49 path = skinstool.getSkinPath(skinName) 50 path = [i.strip() for i in path.split(',')] 51 try: 52 if name not in path: 53 path.insert(path.index('custom')+1, name) 54 except ValueError: 55 if name not in path: 56 path.append(name) 57 58 path = ','.join(path) 59 skinstool.addSkinSelection(skinName, path) 60 61 def install_plone(self, out): 62 """Install with plone 63 """ 64 # register the plone skin layer 65 register_layer(self, 'plone/kupu_plone_layer', 'kupu_plone', out) 66 67 # register as editor 68 portal_props = getToolByName(self, 'portal_properties') 69 site_props = getattr(portal_props,'site_properties', None) 70 attrname = 'available_editors' 71 if site_props is not None: 72 editors = list(site_props.getProperty(attrname)) 73 if 'Kupu' not in editors: 74 editors.append('Kupu') 75 site_props._updateProperty(attrname, editors) 76 print >>out, "Added 'Kupu' to available editors in Plone." 77 install_libraries(self, out) 78 install_configlet(self, out) 79 install_transform(self, out) 80 install_resources(self, out) 81 install_customisation(self, out) 82 83 def _read_resources(): 84 resourcefile = open(os.path.join(kupu_package_dir, 'plone', 'head.kupu'), 'r') 85 try: 86 data = resourcefile.read() 87 return data 88 finally: 89 resourcefile.close() 90 91 def css_files(resources): 92 CSSPAT = re.compile(r'\<link [^>]*rel="stylesheet"[^>]*\$portal_url}/([^"]*)"') 93 for m in CSSPAT.finditer(resources): 94 id = m.group(1) 95 yield id 96 97 def js_files(resources): 98 JSPAT = re.compile(r'\<script [^>]*\$portal_url}/([^"]*)"') 99 for m in JSPAT.finditer(resources): 100 id = m.group(1) 101 if id=='sarissa.js': 102 continue 103 yield id 104 105 def install_resources(self, out): 106 """Add the js and css files to the resource registry so that 107 they can be merged for download. 108 """ 109 try: 110 from Products.ResourceRegistries.config import CSSTOOLNAME, JSTOOLNAME 111 except ImportError: 112 print >>out, "Resource registry not found: kupu will load its own resources" 113 return 114 115 data = _read_resources() 116 117 CONDITION = '''python:portal.kupu_library_tool.isKupuEnabled(REQUEST=request)''' 118 csstool = getToolByName(self, CSSTOOLNAME) 119 jstool = getToolByName(self, JSTOOLNAME) 120 121 for id in css_files(data): 122 print >>out, "CSS file", id 123 cookable = True 124 csstool.manage_removeStylesheet(id=id) 125 csstool.manage_addStylesheet(id=id, 126 expression=CONDITION, 127 rel='stylesheet', 128 enabled=True, 129 cookable=cookable) 130 131 for id in js_files(data): 132 print >>out, "JS file", id 133 jstool.manage_removeScript(id=id) 134 jstool.manage_addScript(id=id, 135 expression=CONDITION, 136 enabled=True, 137 cookable=True) 138 139 def uninstall_resources(self, out): 140 """Remove the js and css files from the resource registries""" 141 try: 142 from Products.ResourceRegistries.config import CSSTOOLNAME, JSTOOLNAME 143 except ImportError: 144 return 145 146 data = _read_resources() 147 148 csstool = getToolByName(self, CSSTOOLNAME) 149 jstool = getToolByName(self, JSTOOLNAME) 150 151 for id in css_files(data): 152 csstool.manage_removeStylesheet(id=id) 153 154 for id in js_files(data): 155 jstool.manage_removeScript(id=id) 156 print >>out, "Resource files removed" 157 158 def install_libraries(self, out): 159 """Install everything necessary to support Kupu Libraries 160 """ 161 # add the library tool 162 addTool = self.manage_addProduct['kupu'].manage_addTool 163 try: 164 addTool('Kupu Library Tool') 165 print >>out, "Added the Kupu Library Tool to the plone Site" 166 except BadRequest: 167 print >>out, "Kupu library Tool already added" 168 except: # Older Zopes 169 #heuristics for testing if an instance with the same name already exists 170 #only this error will be swallowed. 171 #Zope raises in an unelegant manner a 'Bad Request' error 172 e=sys.exc_info() 173 if e[0] != 'Bad Request': 174 raise 175 print >>out, "Kupu library Tool already added" 176 177 def install_configlet(self, out): 178 try: 179 portal_conf=getToolByName(self,'portal_controlpanel') 180 except AttributeError: 181 print >>out, "Configlet could not be installed" 182 return 183 try: 184 portal_conf.registerConfiglet( 'kupu' 185 , TOOLTITLE 186 , 'string:$portal_url}/%s/kupu_config' % TOOLNAME 187 , '' # a condition 188 , 'Manage portal' # access permission 189 , 'Products' # section to which the configlet should be added: 190 #(Plone,Products,Members) 191 , 1 # visibility 192 , PROJECTNAME 193 , 'kupuimages/kupu_icon.gif' # icon in control_panel 194 , 'Kupu Library Tool' 195 , None 196 ) 197 except KeyError: 198 pass # Get KeyError when registering duplicate configlet. 199 200 def install_transform(self, out): 201 try: 202 print >>out, "Adding new mimetype" 203 mimetypes_tool = getToolByName(self, 'mimetypes_registry') 204 newtype = MimeTypeItem.MimeTypeItem('HTML with captioned images', 205 ('text/x-html-captioned',), ('html-captioned',), 0) 206 mimetypes_tool.register(newtype) 207 208 print >>out,"Add transform" 209 transform_tool = getToolByName(self, 'portal_transforms') 210 try: 211 transform_tool.manage_delObjects(['html-to-captioned']) 212 except: # XXX: get rid of bare except 213 pass 214 transform_tool.manage_addTransform('html-to-captioned', 'Products.kupu.plone.html2captioned') 215 except (NameError,AttributeError): 216 print >>out, "No MimetypesRegistry, captioning not supported." 217 218 def install_customisation(self, out): 219 """Default settings may be stored in a customisation policy script so 220 that the entire setup may be 'productised'""" 221 222 # Skins are cached during the request so we (in case new skin 223 # folders have just been added) we need to force a refresh of the 224 # skin. 225 self.changeSkin(None) 226 227 scriptname = '%s-customisation-policy' % PROJECTNAME.lower() 228 cpscript = getattr(self, scriptname, None) 229 if cpscript: 230 cpscript = cpscript.__of__(self) 231 232 if cpscript: 233 print >>out,"Customising %s" % PROJECTNAME 234 print >>out,cpscript() 235 else: 236 print >>out,"No customisation policy" 237 238 def install(self): 239 out = StringIO() 240 241 # register the core layer 242 register_layer(self, 'common', 'kupu', out) 243 244 # try for plone 245 try: 246 import Products.CMFPlone 247 except ImportError: 248 pass 249 else: 250 install_plone(self, out) 251 252 print >>out, "kupu successfully installed" 253 return out.getvalue() 254 255 def uninstall_transform(self, out): 256 transform_tool = getToolByName(self, 'portal_transforms') 257 try: 258 transform_tool.manage_delObjects(['html-to-captioned']) 259 except: 260 pass 261 else: 262 print >>out, "Transform removed" 263 264 def uninstall_tool(self, out): 265 try: 266 self.manage_delObjects([TOOLNAME]) 267 except: 268 pass 269 else: 270 print >>out, "Kupu tool removed" 271 272 def uninstall(self): 273 out = StringIO() 274 275 # remove the configlet from the portal control panel 276 configTool = getToolByName(self, 'portal_controlpanel', None) 277 if configTool: 278 configTool.unregisterConfiglet('kupu') 279 out.write('Removed kupu configlet\n') 280 281 uninstall_transform(self, out) 282 uninstall_tool(self, out) 283 uninstall_resources(self, out) 284 285 print >> out, "Successfully uninstalled %s." % PROJECTNAME 286 return out.getvalue()
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sun Feb 25 15:30:41 2007 | par Balluche grâce à PHPXref 0.7 |