[Checkins] SVN: five.pt/trunk/ Added path and exists functions.
Malthe Borch
mborch at gmail.com
Sun Nov 16 22:42:21 EST 2008
Log message for revision 93033:
Added path and exists functions.
Changed:
U five.pt/trunk/CHANGES.txt
U five.pt/trunk/src/five/pt/cmf.py
U five.pt/trunk/src/five/pt/pagetemplate.py
-=-
Modified: five.pt/trunk/CHANGES.txt
===================================================================
--- five.pt/trunk/CHANGES.txt 2008-11-17 03:18:13 UTC (rev 93032)
+++ five.pt/trunk/CHANGES.txt 2008-11-17 03:42:20 UTC (rev 93033)
@@ -4,6 +4,9 @@
HEAD
----
+- Added ``path`` and ``exists`` functions to skin template
+ namespace. [malthe]
+
- Added call-support for old-style classes in path
expressions. [malthe]
Modified: five.pt/trunk/src/five/pt/cmf.py
===================================================================
--- five.pt/trunk/src/five/pt/cmf.py 2008-11-17 03:18:13 UTC (rev 93032)
+++ five.pt/trunk/src/five/pt/cmf.py 2008-11-17 03:42:20 UTC (rev 93033)
@@ -1,4 +1,3 @@
-import sys
import Globals
from Products.CMFCore.FSObject import FSObject
@@ -15,34 +14,10 @@
from pagetemplate import PageTemplateFile
from pagetemplate import FiveTemplateFile
+from pagetemplate import EContext
_marker = object()
-class EContext(object):
- """This class emulates the `econtext` variable scope dictionary of
- ZPT; it uses `sys._getframe` to acquire the variable and adds
- required methods."""
-
- _scope = None
-
- @property
- def vars(self):
- if self._scope is None:
- frame = sys._getframe()
- scope = _marker
- while scope is _marker and frame is not None:
- scope = frame.f_locals.get('_scope', _marker)
- frame = frame.f_back
- if vars is _marker:
- raise RuntimeError, 'Context not found'
- self._scope = scope
- return self._scope
-
- def setLocal(self, name, value):
- self.vars[name] = value
-
- setGlobal = setLocal
-
class CMFTemplateFile(FiveTemplateFile):
@property
def utility_builtins(self):
Modified: five.pt/trunk/src/five/pt/pagetemplate.py
===================================================================
--- five.pt/trunk/src/five/pt/pagetemplate.py 2008-11-17 03:18:13 UTC (rev 93032)
+++ five.pt/trunk/src/five/pt/pagetemplate.py 2008-11-17 03:42:20 UTC (rev 93033)
@@ -1,21 +1,103 @@
import os
import sys
+from zope import component
from zope.app.pagetemplate.viewpagetemplatefile import ViewMapper
from Acquisition import aq_get
from Acquisition import aq_inner
from Acquisition import aq_parent
+from AccessControl import getSecurityManager
+
from Products.PageTemplates.Expressions import SecureModuleImporter
from z3c.pt import pagetemplate
+from chameleon.core import types
+from chameleon.core import config
+from chameleon.core import clauses
+from chameleon.core import generation
+from chameleon.zpt.interfaces import IExpressionTranslator
+
+from five.pt.expressions import path_translator
+
+_marker = object()
+_expr_cache = {}
+
def get_physical_root(context):
method = aq_get(context, 'getPhysicalRoot', None)
if method is not None:
return method()
+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
+ _locals = EContext().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 EContext(object):
+ """This class emulates the `econtext` variable scope dictionary of
+ ZPT; it uses `sys._getframe` to acquire the variable and adds
+ required methods."""
+
+ _scope = None
+ _locals = None
+
+ @property
+ def locals(self):
+ self.vars; return self._locals
+
+ @property
+ def vars(self):
+ if self._scope is None:
+ frame = sys._getframe()
+ scope = _marker
+ while frame is not None:
+ scope = frame.f_locals.get('_scope', _marker)
+ if scope is not _marker:
+ self._locals = frame.f_locals
+ break
+ frame = frame.f_back
+ else:
+ raise RuntimeError, "Can't locate variable scope."
+ self._scope = scope
+ return self._scope
+
+ def setLocal(self, name, value):
+ self.vars[name] = value
+
+ setGlobal = setLocal
+
class FiveTemplateFile(pagetemplate.PageTemplateFile.template_class):
utility_builtins = {}
@@ -48,7 +130,10 @@
here=context,
container=context,
nothing=None,
+ path=evaluate_path,
+ exists=evaluate_exists,
root=root,
+ user=getSecurityManager().getUser(),
modules=SecureModuleImporter,
options=kwargs)
More information about the Checkins
mailing list