Custom traversing with Five and ITraversable
Sat, Oct 21, 2006,
200 Words
This blog already contains a technique on how to customize the traversal to return any object, using __bobo_traverse__
A more simple, modern and elegant way of doing achieving this is detailed in the newly released ImageRepository. Basically, it uses an adapter to change the implementation for ITraversable when the traversal is done on an object implementing a marker interface. I've lifted the relevant code and pasted it below, but the original sources should be consulted for reference.
class ImageRepositoryTraversable(FiveTraversable):
"""Intercepts traversal for IImageRepository, but only for 'tags'.
Everything else is left untouched.
"""
def traverse(self, name, furtherPath):
return some_obj
Next, the zcml configuration file contains:
<five:traversable class=".content.ImageRepository.ImageRepository" />
<adapter
for=".interfaces.IImageRepository"
provides="zope.app.traversing.interfaces.ITraversable"
factory=".traversal.ImageRepositoryTraversable" />