Back to index

plone3  3.1.7
utils.py
Go to the documentation of this file.
00001 ##############################################################################
00002 #
00003 # PlonePAS - Adapt PluggableAuthService for use in Plone
00004 # Copyright (C) 2005 Enfold Systems, Kapil Thangavelu, et al
00005 #
00006 # This software is subject to the provisions of the Zope Public License,
00007 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this
00008 # distribution.
00009 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
00010 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00011 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
00012 # FOR A PARTICULAR PURPOSE.
00013 #
00014 ##############################################################################
00015 """
00016 """
00017 
00018 from Products.CMFCore.utils import getToolByName
00019 from urllib import quote as url_quote
00020 from urllib import unquote as url_unquote
00021 
00022 def unique(iterable):
00023     d = {}
00024     for i in iterable:
00025         d[i] = None
00026     return d.keys()
00027 
00028 def getCharset(context):
00029     """Returns the site default charset, or utf-8.
00030     """
00031     properties = getToolByName(context, "portal_properties")
00032     site_properties = getattr(properties, 'site_properties', None)
00033     if site_properties is not None:
00034         return site_properties.getProperty('default_charset')
00035     return 'utf-8'
00036 
00037 def cleanId(id):
00038     """'url_quote' turns strange chars into '%xx', which is not a valid char
00039     for ObjectManager. Here we encode '%' into '-' (and '-' into '--' as escaping).
00040     De-clean is possible; see 'decleanId'.
00041     Assumes that id can start with non-alpha(numeric), which is true.
00042     """
00043     __traceback_info__ = (id,)
00044     if id:
00045         # note: we provide the 'safe' param to get '/' encoded
00046         return url_quote(id, '').replace('-','--').replace('%','-')
00047     return ''
00048 
00049 def decleanId(id):
00050    """Reverse cleanId."""
00051    if id:
00052        id = id.replace('--', '\x00').replace('-', '%').replace('\x00', '-')
00053        return url_unquote(id)
00054    return ''