Back to index

plone3  3.1.7
util.py
Go to the documentation of this file.
00001 # Utility functions used by Kupu tool
00002 import os
00003 from App.Common import package_home
00004 from Products.CMFCore.utils import minimalpath
00005 from Products.CMFCore.DirectoryView import createDirectoryView
00006 from Products.MimetypesRegistry import MimeTypeItem
00007 from Products.kupu import kupu_globals
00008 
00009 kupu_package_dir = package_home(kupu_globals)
00010 
00011 # trying to get rid of some deprecation warnings in a
00012 # backwards compatible way
00013 from Products.CMFCore.utils import getToolByName
00014 
00015 def register_layer(self, relpath, name, out, add=True):
00016     """Register a file system directory as skin layer
00017     """
00018     print >>out, "register skin layers"
00019     skinstool = getToolByName(self, 'portal_skins')
00020     if name not in skinstool.objectIds():
00021         kupu_plone_skin_dir = minimalpath(os.path.join(kupu_package_dir, relpath))
00022         createDirectoryView(skinstool, kupu_plone_skin_dir, name)
00023         print >>out, "The layer '%s' was added to the skins tool" % name
00024 
00025     if not add:
00026         return
00027 
00028     # put this layer into all known skins
00029     for skinName in skinstool.getSkinSelections():
00030         path = skinstool.getSkinPath(skinName) 
00031         path = [i.strip() for i in path.split(',')]
00032         try:
00033             if name not in path:
00034                 path.insert(path.index('custom')+1, name)
00035         except ValueError:
00036             if name not in path:
00037                 path.append(name)
00038 
00039         path = ','.join(path)
00040         skinstool.addSkinSelection(skinName, path)
00041 
00042 def unregister_layers(self, names, out):
00043     """Remove a directory from the skins"""
00044     skinstool = getToolByName(self, 'portal_skins')
00045 
00046     # remove this layer from all known skins
00047     for skinName in skinstool.getSkinSelections():
00048         path = skinstool.getSkinPath(skinName) 
00049         path = [i.strip() for i in path.split(',')]
00050 
00051         opath = list(path)
00052         for name in names:
00053             if name in path:
00054                 path.remove(name)
00055 
00056         if opath != path:
00057             path = ','.join(path)
00058             skinstool.addSkinSelection(skinName, path)
00059 
00060             print >>out, "removed layers '%s' from '%s'" % (', '.join(names), skinName)
00061     self.changeSkin(None)
00062 
00063 
00064 def layer_installed(self, name):
00065     skinstool = getToolByName(self, 'portal_skins')
00066 
00067     # remove this layer from all known skins
00068     for skinName in skinstool.getSkinSelections():
00069         path = skinstool.getSkinPath(skinName) 
00070         path = [i.strip() for i in path.split(',')]
00071 
00072         if name not in path:
00073             return False
00074     return True
00075 
00076 UID_TRANSFORM = 'html-to-captioned'
00077 INVERSE_TRANSFORM = 'captioned-to-html'
00078 MT_SAFE = 'text/x-html-safe'
00079 MT_CAPTIONED = 'text/x-html-captioned'
00080 
00081 def install_transform(self):
00082     """Install the uid transform and set up the policy chain to include it when going from html to safe html"""
00083     mimetypes_tool = getToolByName(self, 'mimetypes_registry')
00084     if not mimetypes_tool.lookup(MT_CAPTIONED):
00085         newtype = MimeTypeItem.MimeTypeItem('HTML with captioned images',
00086             (MT_CAPTIONED,), ('html-captioned',), 0)
00087         mimetypes_tool.register(newtype)
00088 
00089     transform_tool = getToolByName(self, 'portal_transforms')
00090     if not hasattr(transform_tool, UID_TRANSFORM):
00091         transform_tool.manage_addTransform(UID_TRANSFORM, 'Products.kupu.plone.html2captioned')
00092 
00093     if not hasattr(transform_tool, INVERSE_TRANSFORM):
00094         transform_tool.manage_addTransform(INVERSE_TRANSFORM, 'Products.PortalTransforms.transforms.identity')
00095         # Need to commit a subtransaction here, otherwise setting the
00096         # parameters will cause an exception when the transaction is
00097         # finally comitted.
00098         try:
00099             import transaction
00100             transaction.savepoint(optimistic=True)
00101         except ImportError:
00102             get_transaction().commit(1)
00103 
00104     inverse = transform_tool[INVERSE_TRANSFORM]
00105     if inverse.get_parameter_value('inputs') != [MT_CAPTIONED] or inverse.get_parameter_value('output') != 'text/html':
00106         inverse.set_parameters(inputs=[MT_CAPTIONED], output='text/html')
00107 
00108     # Set policy
00109     policies = [ (mimetype, required) for (mimetype, required) in transform_tool.listPolicies()
00110          if mimetype==MT_SAFE ]
00111     required = [UID_TRANSFORM]
00112     if policies:
00113         if not UID_TRANSFORM in required:
00114             required.append(UID_TRANSFORM)
00115 
00116         transform_tool.manage_delPolicies([MT_SAFE])
00117     transform_tool.manage_addPolicy(MT_SAFE, required)
00118 
00119 def remove_transform(self):
00120     """Disable the UID transform: remove the policy but leave everything else intact."""
00121     transform_tool = getToolByName(self, 'portal_transforms', None)
00122     if transform_tool is None:
00123         return
00124     policies = [ (mimetype, required) for (mimetype, required) in transform_tool.listPolicies() if mimetype==MT_SAFE ]
00125     if not policies:
00126         return
00127     required = list(policies[0][1])
00128     if UID_TRANSFORM in required:
00129         required.remove(UID_TRANSFORM)
00130         transform_tool.manage_delPolicies([MT_SAFE])
00131         if required:
00132             transform_tool.manage_addPolicy(MT_SAFE, required)
00133 
00134 try:
00135     from zope.i18nmessageid import Message
00136     from zope.i18n import translate as i18n_translate
00137     def translate(label, context):
00138         if isinstance(label, Message):
00139             return i18n_translate(label, context=context)
00140         return label
00141 except ImportError:
00142     def translate(label, context):
00143         return label
00144