Correction crash
[auf_roundup.git] / build / lib / roundup / cgi / PageTemplates / PathIterator.py
CommitLineData
c638d827
CR
1##############################################################################
2#
3# Copyright (c) 2001 Zope Corporation and Contributors. All Rights Reserved.
4#
5# This software is subject to the provisions of the Zope Public License,
6# Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution.
7# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
8# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
9# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
10# FOR A PARTICULAR PURPOSE
11#
12##############################################################################
13
14"""Path Iterator
15
16A TALES Iterator with the ability to use first() and last() on
17subpaths of elements.
18"""
19
20__version__='$Revision: 1.3 $'[11:-2]
21
22import TALES
23from Expressions import restrictedTraverse, Undefs, getSecurityManager
24
25class Iterator(TALES.Iterator):
26 def __bobo_traverse__(self, REQUEST, name):
27 if name in ('first', 'last'):
28 path = REQUEST['TraversalRequestNameStack']
29 names = list(path)
30 names.reverse()
31 path[:] = [tuple(names)]
32 return getattr(self, name)
33
34 def same_part(self, name, ob1, ob2):
35 if name is None:
36 return ob1 == ob2
37 if isinstance(name, type('')):
38 name = name.split('/')
39 name = filter(None, name)
40 securityManager = getSecurityManager()
41 try:
42 ob1 = restrictedTraverse(ob1, name, securityManager)
43 ob2 = restrictedTraverse(ob2, name, securityManager)
44 except Undefs:
45 return 0
46 return ob1 == ob2