[Checkins] SVN: z3c.pt/trunk/src/z3c/pt/ - Move over evaluate_path and evaluate_exists from five.pt

Sidnei da Silva sidnei at enfoldsystems.com
Mon Jan 19 18:12:02 EST 2009


Log message for revision 94866:
  - Move over evaluate_path and evaluate_exists from five.pt

Changed:
  U   z3c.pt/trunk/src/z3c/pt/README.txt
  U   z3c.pt/trunk/src/z3c/pt/pagetemplate.py

-=-
Modified: z3c.pt/trunk/src/z3c/pt/README.txt
===================================================================
--- z3c.pt/trunk/src/z3c/pt/README.txt	2009-01-19 22:38:50 UTC (rev 94865)
+++ z3c.pt/trunk/src/z3c/pt/README.txt	2009-01-19 23:12:02 UTC (rev 94866)
@@ -167,11 +167,11 @@
   }
 
 
-Global Path Expression
+Global 'path' Function
 ----------------------
 
 Just like ``zope.pagetemplate``, it is possible to use a globally
-defined ``path`` function in a ``python:`` expression in ``z3c.pt``:
+defined ``path()`` function in a ``python:`` expression in ``z3c.pt``:
 
   >>> template = PageTemplate("""\
   ... <div xmlns="http://www.w3.org/1999/xhtml">
@@ -185,6 +185,21 @@
     <span>test</span>
   </div>
 
+Global 'exists' Function
+------------------------
+
+The same applies to the ``exists()`` function:
+
+  >>> template = PageTemplate("""\
+  ... <div xmlns="http://www.w3.org/1999/xhtml">
+  ...   <span tal:content="python: exists('options/test') and 'Yes' or 'No'" />
+  ... </div>""")
+
+  >>> print template(test='test')
+  <div xmlns="http://www.w3.org/1999/xhtml">
+    <span>Yes</span>
+  </div>
+
 'default' and path expressions
 ------------------------------
 

Modified: z3c.pt/trunk/src/z3c/pt/pagetemplate.py
===================================================================
--- z3c.pt/trunk/src/z3c/pt/pagetemplate.py	2009-01-19 22:38:50 UTC (rev 94865)
+++ z3c.pt/trunk/src/z3c/pt/pagetemplate.py	2009-01-19 23:12:02 UTC (rev 94866)
@@ -2,10 +2,62 @@
 import sys
 
 from zope import i18n
+from zope import component
+
+from chameleon.core import types
+from chameleon.core import config
+from chameleon.core import clauses
+from chameleon.core import generation
+
 from chameleon.zpt import template
+from chameleon.zpt.interfaces import IExpressionTranslator
 
-import language
+from z3c.pt import language
 
+_marker = object()
+_expr_cache = {}
+
+def evaluate_expression(pragma, expr):
+    key = "%s(%s)" % (pragma, expr)
+    cache = getattr(_expr_cache, key, _marker)
+    if cache is not _marker:
+        symbol_mapping, parts, source = cache
+    else:
+        translator = component.getUtility(IExpressionTranslator, name=pragma)
+        parts = translator.tales(expr)
+        stream = generation.CodeIO(symbols=config.SYMBOLS)
+        assign = clauses.Assign(parts, 'result')
+        assign.begin(stream)
+        assign.end(stream)
+        source = stream.getvalue()
+
+        symbol_mapping = parts.symbol_mapping.copy()
+        if isinstance(parts, types.parts):
+            for value in parts:
+                symbol_mapping.update(value.symbol_mapping)    
+
+        _expr_cache[key] = symbol_mapping, parts, source
+
+    # acquire template locals and update with symbol mapping
+    frame = sys._getframe()
+    while frame.f_locals.get('econtext', _marker) is _marker:
+        frame = frame.f_back
+        if frame is None:
+            raise RuntimeError, "Can't locate template frame."
+
+    _locals = frame.f_locals
+    _locals.update(symbol_mapping)    
+
+    # execute code and return evaluation
+    exec source in _locals
+    return _locals['result']
+
+def evaluate_path(expr):
+    return evaluate_expression('path', expr)
+
+def evaluate_exists(expr):
+    return evaluate_expression('exists', expr)
+
 class BaseTemplate(template.PageTemplate):
     default_parser = language.Parser()
 
@@ -39,6 +91,8 @@
             options=kwargs,
             request=request,
             template=self,
+            path=evaluate_path,
+            exists=evaluate_exists,
             nothing=None)
 
 class BaseTemplateFile(BaseTemplate, template.PageTemplateFile):
@@ -102,6 +156,8 @@
             context=kwargs.get('context', view.context),
             request=request or kwargs.get('request', view.request),
             template=self,
+            path=evaluate_path,
+            exists=evaluate_exists,
             options=kwargs,
             nothing=None)
 



More information about the Checkins mailing list