[Checkins] SVN: z3c.pt/trunk/ Improved unicode-handling; attributes that evaluate to None are now disregarded.

Malthe Borch mborch at gmail.com
Sat Feb 23 06:41:54 EST 2008


Log message for revision 84174:
  Improved unicode-handling; attributes that evaluate to None are now disregarded.

Changed:
  U   z3c.pt/trunk/setup.py
  U   z3c.pt/trunk/z3c/pt/BENCHMARKS.txt
  U   z3c.pt/trunk/z3c/pt/clauses.py
  U   z3c.pt/trunk/z3c/pt/pagetemplate.py
  U   z3c.pt/trunk/z3c/pt/translation.txt

-=-
Modified: z3c.pt/trunk/setup.py
===================================================================
--- z3c.pt/trunk/setup.py	2008-02-23 02:36:33 UTC (rev 84173)
+++ z3c.pt/trunk/setup.py	2008-02-23 11:41:52 UTC (rev 84174)
@@ -1,6 +1,6 @@
 from setuptools import setup, find_packages
 
-version = '0.4.2'
+version = '0.4.3'
 
 setup(name='z3c.pt',
       version=version,

Modified: z3c.pt/trunk/z3c/pt/BENCHMARKS.txt
===================================================================
--- z3c.pt/trunk/z3c/pt/BENCHMARKS.txt	2008-02-23 02:36:33 UTC (rev 84173)
+++ z3c.pt/trunk/z3c/pt/BENCHMARKS.txt	2008-02-23 11:41:52 UTC (rev 84174)
@@ -10,7 +10,7 @@
 
                   zope.pagetemplate     z3c.pt    pure python
 Hello World        3.6                  1         0.02        
-1000 x 10 table   13.5                  1         0.58
+1000 x 10 table   11.3                  1         0.48
 
 There's a setup cost in using a template language which explains the
 50x factor in the 'Hello World' benchmark versus a pure python

Modified: z3c.pt/trunk/z3c/pt/clauses.py
===================================================================
--- z3c.pt/trunk/z3c/pt/clauses.py	2008-02-23 02:36:33 UTC (rev 84173)
+++ z3c.pt/trunk/z3c/pt/clauses.py	2008-02-23 11:41:52 UTC (rev 84174)
@@ -1,7 +1,8 @@
 from expressions import value
-
 from utils import unicode_required_flag
 
+from cgi import escape
+
 class Assign(object):
     """
       >>> from z3c.pt.io import CodeIO; stream = CodeIO()
@@ -359,18 +360,56 @@
     def begin(self, stream):
         stream.out('<%s' % self.tag)
 
-        # attributes
-        for attribute, expression in self.attributes.items():
-            stream.out(' %s="' % attribute)
+        # static attributes
+        static = filter(
+            lambda (attribute, expression): \
+            not isinstance(expression, (tuple, list)),
+            self.attributes.items())
 
-            if isinstance(expression, (tuple, list)):
-                write = Write(expression)
-                write.begin(stream, escape='"')
-                write.end(stream)
+        dynamic = filter(
+            lambda (attribute, expression): \
+            isinstance(expression, (tuple, list)),
+            self.attributes.items())
+
+        for attribute, expression in static:
+            stream.out(' %s="%s"' %
+               (attribute,
+                escape(expression, '"').replace("'", "\\'")))
+
+        temp = stream.save()
+        
+        for attribute, expression in dynamic:
+            assign = Assign(expression)
+            assign.begin(stream, temp)
+            
+            # only include attribute if value is non-trivial
+            stream.write("if %s is not None:" % temp)
+            stream.indent()
+
+            # if callable, evaluate method
+            stream.write("if callable(%s): %s = %s()" % (temp, temp, temp))
+
+            if unicode_required_flag:
+                stream.write("if isinstance(%s, unicode):" % temp)
+                stream.indent()
+                stream.write("%s = %s.encode('utf-8')" % (temp, temp))
+                stream.outdent()
+                stream.write("else:")
+                stream.indent()
+                stream.write("%s = str(%s)" % (temp, temp))
+                stream.outdent()
             else:
-                stream.out(expression.replace("'", "\\'"))
+                stream.write("%s = str(%s)" % (temp, temp))
+                
+            stream.write("_out.write(' %s=\"' + _escape(%s, \"\\\"\"))" %
+                         (attribute, temp))
             stream.out('"')
+            
+            assign.end(stream)
+            stream.outdent()
 
+        stream.restore()
+
         if self.selfclosing:
             stream.out(" />")
         else:
@@ -463,7 +502,7 @@
         self.expressions = expressions
         self.count = len(expressions)
         
-    def begin(self, stream, escape=False):
+    def begin(self, stream):
         temp = stream.save()
                 
         if self.count == 1:
@@ -476,18 +515,14 @@
         stream.write("if callable(_urf): _urf = _urf()")
         stream.write("if _urf is None: _urf = ''")
 
-        if escape:
-            escape_char = escape.replace("'", "\'")
-            stream.write("_urf = _escape(_urf, '%s')" % escape_char)
-            
         if unicode_required_flag:
-            stream.write("try:")
+            stream.write("if isinstance('_urf', unicode):")
             stream.indent()
-            stream.write("_out.write(str(_urf))")
+            stream.write("_out.write(_urf.encode('utf-8'))")
             stream.outdent()
-            stream.write("except TypeError:")
+            stream.write("else:")
             stream.indent()
-            stream.write("_out.write(unicode(_urf, 'utf-8'))")
+            stream.write("_out.write(str(_urf))")
             stream.outdent()
         else:
             stream.write("_out.write(str(_urf))")

Modified: z3c.pt/trunk/z3c/pt/pagetemplate.py
===================================================================
--- z3c.pt/trunk/z3c/pt/pagetemplate.py	2008-02-23 02:36:33 UTC (rev 84173)
+++ z3c.pt/trunk/z3c/pt/pagetemplate.py	2008-02-23 11:41:52 UTC (rev 84174)
@@ -14,6 +14,8 @@
         source, _globals = translation.translate(self.body, params)
         suite = codegen.Suite(source)
 
+        self.source = source
+        
         _globals.update(suite._globals)
         _locals = {}
 

Modified: z3c.pt/trunk/z3c/pt/translation.txt
===================================================================
--- z3c.pt/trunk/z3c/pt/translation.txt	2008-02-23 02:36:33 UTC (rev 84173)
+++ z3c.pt/trunk/z3c/pt/translation.txt	2008-02-23 11:41:52 UTC (rev 84174)
@@ -30,6 +30,7 @@
   ...   </ul>
   ...   <tal:example replace="'Hello World!'" />
   ...   <tal:div content="'Hello World!'" />
+  ...   <img alt="'Hello World!'" />
   ...   <tal:multiple repeat="i range(5)" replace="i" />
   ...   <p tal:omit-tag="">No paragraph here.</p>
   ...   <p tal:omit-tag="True">No paragraph here either.</p>
@@ -44,7 +45,7 @@
 
   >>> print render(body)
   <div>
-    <span style="position: absolute" id="test" class="defabc">abcghi</span>
+    <span id="test" style="position: absolute" class="defabc">abcghi</span>
     <ul>
       <li>
 	Item 0)
@@ -64,6 +65,7 @@
     </ul>
     Hello World!
     <div>Hello World!</div>
+    <img alt="'Hello World!'" />
     0
     1
     2



More information about the Checkins mailing list