Back to index

plone3  3.1.7
unicodeEncode.py
Go to the documentation of this file.
00001 ## Script (Python) "unicodeEncode"
00002 ##title=Return an encoded string using the site charset
00003 ##bind container=container
00004 ##bind context=context
00005 ##bind namespace=
00006 ##bind script=script
00007 ##bind subpath=traverse_subpath
00008 ##parameters=value, site_charset=None
00009 
00010 # Recursively deal with sequences
00011 tuplevalue = same_type(value, ())
00012 if (tuplevalue or same_type(value, [])):
00013     encoded = [context.unicodeEncode(v) for v in value]
00014     if tuplevalue:
00015         encoded = tuple(encoded)
00016     return encoded
00017 
00018 if not isinstance(value, basestring):
00019     value = str(value)
00020 
00021 if site_charset is None:
00022     site_charset = context.getCharset()
00023 
00024 if same_type(value, ''):
00025     value = unicode(value, site_charset)
00026 
00027 # don't try to catch unicode error here
00028 # if one occurs, that means the site charset must be changed !
00029 return value.encode(site_charset)