[Erp5-report] r24825 - /erp5/trunk/products/ERP5Type/patches/ppml.py

nobody at svn.erp5.org nobody at svn.erp5.org
Mon Dec 8 11:07:11 CET 2008


Author: kazuhiko
Date: Mon Dec  8 11:07:08 2008
New Revision: 24825

URL: http://svn.erp5.org?rev=24825&view=rev
Log:
this patch makes a more readable expression for a valit utf8 string.
if the input string is a valid utf8 string, only [\x00-\x1f] characters
will be escaped.

Modified:
    erp5/trunk/products/ERP5Type/patches/ppml.py

Modified: erp5/trunk/products/ERP5Type/patches/ppml.py
URL: http://svn.erp5.org/erp5/trunk/products/ERP5Type/patches/ppml.py?rev=24825&r1=24824&r2=24825&view=diff
==============================================================================
--- erp5/trunk/products/ERP5Type/patches/ppml.py [utf8] (original)
+++ erp5/trunk/products/ERP5Type/patches/ppml.py [utf8] Mon Dec  8 11:07:08 2008
@@ -16,6 +16,53 @@
 # or new patch will not work
 from Shared.DC.xml.ppml import *
 from Shared.DC.xml import ppml
+
+# For converting to a more readable expression.
+reprs = {}
+### patch begin: create a conversion table for [\x00-\x1f]. this table is
+###              used for a valid utf-8 string.
+for c in map(chr, range(32)): reprs[c] = repr(c)[1:-1]
+### patch end
+reprs['\n'] = "\\n\n"
+reprs['\t'] = "\\t"
+reprs['\\'] = "\\\\"
+reprs['\r'] = "\\r"
+reprs["'"] = "\\'"
+reprs2={}
+reprs2['<'] = "\\074"
+reprs2['>'] = "\\076"
+reprs2['&'] = "\\046"
+### patch begin: create a conversion table for [\x00-\xff]. this table is
+###              used for a binary string.
+reprs3 = reprs.copy()
+for c in map(chr,range(32, 256)): reprs3[c] = reprs.get(c, repr(c)[1:-1])
+### patch end
+
+def convert(S, find=None):
+    new = ''
+    encoding = 'repr'
+    ### patch begin: if the input string is a valid utf8 string, only
+    ###              [\x00-\x1f] characters will be escaped to make a more
+    ###              readable output.
+    try:
+        S.decode('utf8')
+    except UnicodeDecodeError:
+        new = ''.join([reprs3.get(x) for x in S])
+    else:
+        new = ''.join([reprs.get(x, x) for x in S])
+    ### patch end
+    if len(new) > (1.4*len(S)):
+        encoding = 'base64'
+        new = base64.encodestring(S)[:-1]
+    elif new.find('>') >= 0 or new.find('<') >= 0 or new.find('&') >= 0:
+        if new.find(']]>') <0 :
+            new = '<![CDATA[\n\n%s\n\n]]>' % new
+            encoding = 'cdata'
+        else:
+            new = ''.join([reprs2.get(x, x) for x in new])
+    return encoding, new
+
+ppml.convert = convert
 
 # For optimization.
 def unconvert(encoding,S):




More information about the Erp5-report mailing list