Wire in Zope2-compatible, but much more simple iterator implementation.

parent 39e73938
...@@ -17,7 +17,7 @@ for Python expressions, string literals, and paths. ...@@ -17,7 +17,7 @@ for Python expressions, string literals, and paths.
$Id$ $Id$
""" """
from zope.tales.tales import ExpressionEngine, Context from zope.tales.tales import ExpressionEngine, Context, Iterator
from zope.tales.expressions import PathExpr, StringExpr, NotExpr from zope.tales.expressions import PathExpr, StringExpr, NotExpr
from zope.tales.expressions import DeferExpr, SubPathExpr from zope.tales.expressions import DeferExpr, SubPathExpr
from zope.tales.expressions import SimpleModuleImporter from zope.tales.expressions import SimpleModuleImporter
...@@ -99,10 +99,48 @@ class ZopeEngine(ExpressionEngine): ...@@ -99,10 +99,48 @@ class ZopeEngine(ExpressionEngine):
kwcontexts = contexts kwcontexts = contexts
return ZopeContext(self, kwcontexts) return ZopeContext(self, kwcontexts)
class ZopeIterator(Iterator):
# these used to be properties in ZTUtils.Iterator.Iterator
@property
def index(self):
return super(ZopeIterator, self).index()
@property
def start(self):
return super(ZopeIterator, self).start()
@property
def end(self):
return super(ZopeIterator, self).end()
@property
def item(self):
return super(ZopeIterator, self).item()
# these aren't in zope.tales.tales.Iterator, but were in
# ZTUtils.Iterator.Iterator
def first(self, name=None):
if self.start:
return True
return not self.same_part(name, self._last, self.item)
def last(self, name=None):
if self.end:
return True
return not self.same_part(name, self.item, self._next)
def same_part(self, name, ob1, ob2):
if name is None:
return ob1 == ob2
no = object()
return getattr(ob1, name, no) == getattr(ob2, name, no) is not no
def createZopeEngine(): def createZopeEngine():
e = ZopeEngine() e = ZopeEngine()
#TODO wire in PathIterator.Iterator after fixing it e.iteratorFactory = ZopeIterator
# e.iteratorFactory = Iterator
for pt in ZopePathExpr._default_type_names: for pt in ZopePathExpr._default_type_names:
e.registerType(pt, ZopePathExpr) e.registerType(pt, ZopePathExpr)
e.registerType('string', StringExpr) e.registerType('string', StringExpr)
......
...@@ -12,33 +12,14 @@ ...@@ -12,33 +12,14 @@
############################################################################## ##############################################################################
"""Path Iterator """Path Iterator
A TALES Iterator with the ability to use first() and last() on BBB 2005/05/01 -- to be removed after 12 months
subpaths of elements.
$Id$ $Id$
""" """
import TALES import zope.deferredimport
from Expressions import restrictedTraverse, Undefs, getSecurityManager zope.deferredimport.deprecated(
"It has been renamed to ZopeIterator and moved to the "
class Iterator(TALES.Iterator): "Products.PageTemplates.Expressions module. This reference will be "
def __bobo_traverse__(self, REQUEST, name): "gone in Zope 2.12.",
if name in ('first', 'last'): PathIterator = "Products.PageTemplates.Expressions:ZopeIterator"
path = REQUEST['TraversalRequestNameStack'] )
names = list(path)
names.reverse()
path[:] = [tuple(names)]
return getattr(self, name)
def same_part(self, name, ob1, ob2):
if name is None:
return ob1 == ob2
if isinstance(name, type('')):
name = name.split('/')
name = filter(None, name)
securityManager = getSecurityManager()
try:
ob1 = restrictedTraverse(ob1, name, securityManager)
ob2 = restrictedTraverse(ob2, name, securityManager)
except Undefs:
return 0
return ob1 == ob2
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