[Checkins] SVN: z3c.pt/trunk/src/z3c/pt/pagetemplate.py Use high-level compiler API to runtime-compile the path- and exists-expressions. Also, don't assume that the dynamic context (``econtext``) is the same everywhere; rather, acquire it using the system frame (nobody should ever use this functions anyway, so it's just a way to stay compatible).
Malthe Borch
mborch at gmail.com
Wed Feb 23 05:08:49 EST 2011
Log message for revision 120527:
Use high-level compiler API to runtime-compile the path- and exists-expressions. Also, don't assume that the dynamic context (``econtext``) is the same everywhere; rather, acquire it using the system frame (nobody should ever use this functions anyway, so it's just a way to stay compatible).
Changed:
U z3c.pt/trunk/src/z3c/pt/pagetemplate.py
-=-
Modified: z3c.pt/trunk/src/z3c/pt/pagetemplate.py
===================================================================
--- z3c.pt/trunk/src/z3c/pt/pagetemplate.py 2011-02-23 09:45:41 UTC (rev 120526)
+++ z3c.pt/trunk/src/z3c/pt/pagetemplate.py 2011-02-23 10:08:49 UTC (rev 120527)
@@ -1,8 +1,6 @@
import os
-import ast
import sys
-from functools import partial
from zope import i18n
from chameleon.i18n import fast_translate
@@ -10,12 +8,9 @@
from chameleon.tales import PythonExpr
from chameleon.tales import StringExpr
from chameleon.tales import NotExpr
-from chameleon.astutil import store
-from chameleon.astutil import param
-from chameleon.astutil import load
-from chameleon.codegen import TemplateCodeGenerator
-from chameleon.compiler import ExpressionCompiler
-from chameleon.tales import TalesEngine
+from chameleon.nodes import Assignment
+from chameleon.nodes import Macro
+from chameleon.compiler import Compiler
from z3c.pt import expressions
@@ -79,8 +74,8 @@
target_language = None
context['target_language'] = target_language
- context['path'] = partial(self.evaluate_path, econtext=context)
- context['exists'] = partial(self.evaluate_exists, econtext=context)
+ context['path'] = self.evaluate_path
+ context['exists'] = self.evaluate_exists
# bind translation-method to request
def translate(
@@ -118,47 +113,42 @@
modules=sys_modules
)
- def evaluate_expression(self, pragma, expr, econtext):
+ def evaluate_expression(self, pragma, expr):
key = "%s(%s)" % (pragma, expr)
try:
function = _expr_cache[key]
except KeyError:
- compiler = ExpressionCompiler(self.engine, {})
- target = store("_result")
- body = compiler("%s:%s" % (pragma, expr), target)
- body.append(ast.Return(load("_result")))
+ expression = "%s:%s" % (pragma, expr)
+ assignment = Assignment(["_expr_result"], expression, True)
+ macro = Macro(None, [assignment])
+ compiler = Compiler(self.engine, macro)
- fdef = ast.FunctionDef("_evaluate", ast.arguments(
- [param("econtext")], None, None, []), body, [])
-
- module = ast.Module([fdef])
- ast.fix_missing_locations(module)
-
- generator = TemplateCodeGenerator(module)
-
d = {}
- exec generator.code in d
- function = _expr_cache[key] = d[fdef.name]
+ exec compiler.code in d
+ function = _expr_cache[key] = d['render']
- if econtext is None:
- # 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.")
+ # acquire template locals and update with symbol mapping
+ frame = sys._getframe()
+ while True:
+ l = frame.f_locals
+ econtext = l.get('econtext')
+ if econtext is not None:
+ break
- econtext = frame.f_locals
+ frame = frame.f_back
+ if frame is None:
+ raise RuntimeError("Can't locate template frame.")
- return function(econtext)
+ function([], econtext, None)
+ return econtext['_expr_result']
- def evaluate_path(self, expr, econtext=None):
- return self.evaluate_expression('path', expr, econtext)
+ def evaluate_path(self, expr):
+ return self.evaluate_expression('path', expr)
- def evaluate_exists(self, expr, econtext=None):
+ def evaluate_exists(self, expr):
try:
- return self.evaluate_expression('exists', expr, econtext)
+ return self.evaluate_expression('exists', expr)
except NameError:
return False
More information about the checkins
mailing list