Commit fb972e8d authored by Hanno Schlichting's avatar Hanno Schlichting

Ported the lazy expression into zope.tales and require a new version of it.

parent 362cb476
......@@ -11,6 +11,8 @@ Trunk (unreleased)
Restructuring
+++++++++++++
- Ported the lazy expression into zope.tales and require a new version of it.
- Updated Five documentation to clarify its role in regard to Zope packages.
- Removed the deprecated ``five:containerEvents`` directive, which had been
......
......@@ -130,7 +130,7 @@ setup(name='Zope2',
'zope.size',
'zope.structuredtext',
'zope.tal',
'zope.tales',
'zope.tales >= 3.5.0',
'zope.testbrowser',
'zope.testing',
'zope.traversing',
......
......@@ -10,48 +10,7 @@
# FOR A PARTICULAR PURPOSE
#
##############################################################################
"""Lazy expression handler
A lazy expressions is implemented similarly to the defer expression
but has a different result. While a defer expression is evaluated
every time it is used according to its context a lazy expression is
evaluted only the first time it is used. Lazy expression are known
under the name lazy initialization of variables, too. A common use
case for a lazy expression is a lazy binding of a costly expression.
While one could call an expression only when it's required it makes
sense to define it only one time when it could be used multiple times.
Example
<div tal:define="lazyvar lazy:here/suckMyCPU">
<div tal:condition="foo" tal:content="lazyvar" />
<div tal:condition="bar" tal:content="lazyvar" />
<div tal:condition"python: not (foo or bar)">...</div>
</div>
"""
# BBB
from zope.tales.expressions import DeferWrapper, DeferExpr
_marker = object()
# TODO These should really be integrated into the zope.tales implementation
class LazyWrapper(DeferWrapper):
"""Wrapper for lazy: expression
"""
def __init__(self, expr, econtext):
DeferWrapper.__init__(self, expr, econtext)
self._result = _marker
def __call__(self):
r = self._result
if r is _marker:
self._result = r = self._expr(self._econtext)
return r
class LazyExpr(DeferExpr):
"""lazy: expression handler for lazy initialization of expressions
"""
def __call__(self, econtext):
return LazyWrapper(self._c, econtext)
def __repr__(self):
return 'lazy:%s' % `self._s`
from zope.tales.expressions import LazyWrapper, LazyExpr
......@@ -26,6 +26,7 @@ from zope.interface import implements
from zope.pagetemplate.engine import ZopeEngine as Z3Engine
from zope.proxy import removeAllProxies
from zope.tales.expressions import DeferExpr
from zope.tales.expressions import LazyExpr
from zope.tales.expressions import NotExpr
from zope.tales.expressions import PathExpr
from zope.tales.expressions import StringExpr
......@@ -45,7 +46,6 @@ from zExceptions import Unauthorized
from zope.contentprovider.tales import TALESProviderExpression
from Products.PageTemplates import ZRPythonExpr
from Products.PageTemplates.DeferExpr import LazyExpr
from Products.PageTemplates.interfaces import IUnicodeEncodingConflictResolver
SecureModuleImporter = ZRPythonExpr._SecureModuleImporter()
......
......@@ -83,18 +83,6 @@ class EngineTestsBase(PlacelessSetup):
ec = self._makeContext()
self.failUnless(ec.evaluate('x | nothing') is None)
def DONT_test_evaluate_with_empty_element(self):
# empty path elements aren't supported anymore, for the lack
# of a use case
ec = self._makeContext()
self.assertEqual(ec.evaluate('d/'), 'blank')
def DONT_test_evaluate_with_empty_element_and_alternative(self):
# empty path elements aren't supported anymore, for the lack
# of a use case
ec = self._makeContext()
self.assertEqual(ec.evaluate('d/ | nothing'), 'blank')
def test_evaluate_dict_key_as_underscore(self):
ec = self._makeContext()
self.assertEqual(ec.evaluate('d/_'), 'under')
......@@ -131,13 +119,13 @@ class EngineTestsBase(PlacelessSetup):
ec.endScope()
def test_defer_expression_returns_wrapper(self):
from Products.PageTemplates.DeferExpr import DeferWrapper
from zope.tales.expressions import DeferWrapper
ec = self._makeContext()
defer = ec.evaluate('defer: b')
self.failUnless(isinstance(defer, DeferWrapper))
def test_lazy_expression_returns_wrapper(self):
from Products.PageTemplates.DeferExpr import LazyWrapper
from zope.tales.expressions import LazyWrapper
ec = self._makeContext()
lazy = ec.evaluate('lazy: b')
self.failUnless(isinstance(lazy, LazyWrapper))
......@@ -269,6 +257,3 @@ def test_suite():
unittest.makeSuite(UnicodeEncodingConflictResolverTests),
unittest.makeSuite(ZopeContextTests),
))
if __name__=='__main__':
main()
......@@ -66,7 +66,7 @@ zope.site = 3.9.0
zope.size = 3.4.1
zope.structuredtext = 3.4.0
zope.tal = 3.5.2
zope.tales = 3.4.0
zope.tales = 3.5.0
zope.testbrowser = 3.7.0
zope.testing = 3.8.3
zope.traversing = 3.12.0
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment