Back to index

plone3  3.1.7
unicodehacks.py
Go to the documentation of this file.
00001 from StringIO import StringIO
00002 import textwrap
00003 import warnings
00004 from zope.tal.talinterpreter import _write_ValueError
00005 
00006 def _unicode_replace(structure):
00007     if isinstance(structure, str):
00008         try:
00009             text = structure.decode('utf-8')
00010         except UnicodeDecodeError:
00011             # XXX Maybe, raise an exception here instead of a warning?
00012             warnings.warn(textwrap.dedent('''\
00013 
00014             *** *** Insertion of non-unicode non-ascii non-utf8 encoded text in TAL is deprecated and will be broken in Plone 4.0 !!!
00015 
00016             %s...
00017             ''' % (repr(structure), )), DeprecationWarning, 2)
00018             # XXX the next line is fool-proof and will substitute ??-s if the encoding was not
00019             # unicode
00020             text = structure.decode('utf-8', 'replace')
00021     else:
00022         text = unicode(structure)
00023     return text
00024 
00025 
00026 def _nulljoin(valuelist):
00027     try:
00028         return u''.join(valuelist)
00029     except UnicodeDecodeError:
00030         pass
00031     return u''.join([_unicode_replace(value) for value in valuelist])
00032 
00033 
00034 def new__call__(self, econtext):
00035     try:
00036         return self._expr % tuple([var(econtext) for var in self._vars])
00037     except UnicodeDecodeError:
00038         pass
00039     return self._expr % tuple([_unicode_replace(var(econtext)) for var in self._vars])
00040 
00041 
00042 class FasterStringIO(StringIO):
00043     """Append-only version of StringIO, which ignores any initial buffer.
00044 
00045     This let's us have much faster write() and getvalue methods.
00046 
00047     Most of this code was taken from zope.tal.talinterpreter.py licenced under
00048     the ZPL 2.1.
00049     """
00050     def __init__(self, buf=None):
00051         self.buf = u''
00052         self.len = 0
00053         self.buflist = []
00054         self.bufappend = self.buflist.append
00055         self.pos = 0
00056         self.closed = False
00057         self.softspace = 0
00058 
00059     def close(self):
00060         if not self.closed:
00061             self.write = _write_ValueError
00062             StringIO.close(self)
00063 
00064     def seek(self, pos, mode=0):
00065         raise RuntimeError("FasterStringIO.seek() not allowed")
00066 
00067     def write(self, s):
00068         self.bufappend(s)
00069         self.len = self.pos = self.pos + len(s)
00070 
00071     def getvalue(self):
00072         if self.buflist:
00073             try:
00074                 self.buf = u''.join(self.buflist)
00075             except UnicodeDecodeError:
00076                 self.buf = u''.join([_unicode_replace(value) for value in self.buflist])
00077             self.buflist = []
00078         return self.buf