Code

documentation cleanup
[roundup.git] / roundup / cgi / PageTemplates / PathIterator.py
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 ##############################################################################
14 """Path Iterator
16 A TALES Iterator with the ability to use first() and last() on
17 subpaths of elements.
18 """
19 __docformat__ = 'restructuredtext'
21 __version__='$Revision: 1.2 $'[11:-2]
23 import TALES
24 from Expressions import restrictedTraverse, Undefs, getSecurityManager
25 from string import split
27 class Iterator(TALES.Iterator):
28     def __bobo_traverse__(self, REQUEST, name):
29         if name in ('first', 'last'):
30             path = REQUEST['TraversalRequestNameStack']
31             names = list(path)
32             names.reverse()
33             path[:] = [tuple(names)]
34         return getattr(self, name)
36     def same_part(self, name, ob1, ob2):
37         if name is None:
38             return ob1 == ob2
39         if isinstance(name, type('')):
40             name = split(name, '/')
41         name = filter(None, name)
42         securityManager = getSecurityManager()
43         try:
44             ob1 = restrictedTraverse(ob1, name, securityManager)
45             ob2 = restrictedTraverse(ob2, name, securityManager)
46         except Undefs:
47             return 0
48         return ob1 == ob2