[Zope-Checkins] CVS: Zope3/lib/python/Zope/App/Traversing - ITraverser.py:1.1.2.1 Traverser.py:1.1.2.1 __init__.py:1.1.2.1

Martijn Pieters mj@zope.com
Mon, 3 Dec 2001 13:59:56 -0500


Update of /cvs-repository/Zope3/lib/python/Zope/App/Traversing
In directory cvs.zope.org:/tmp/cvs-serv9562/lib/python/Zope/App/Traversing

Added Files:
      Tag: Zope-3x-branch
	ITraverser.py Traverser.py __init__.py 
Log Message:
Move traversing code to new package and rename the interface and class; this in
anticipation of related interfaces and code.


=== Added File Zope3/lib/python/Zope/App/Traversing/ITraverser.py ===
# Copyright (c) 2001 Zope Corporation and Contributors.  All Rights Reserved.
# 
# This software is subject to the provisions of the Zope Public License,
# Version 1.1 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS 
# FOR A PARTICULAR PURPOSE.

import Interface

_RAISE_KEYERROR=[]

class ITraverser(Interface.Base):
    """Provide traverse features"""

    def getPhysicalRoot():
        """
        Returns the top-level Application object, or the bas eobject is it is
        unwrapped.
        """

    def getPhysicalPath():
        """
        Returns a path (an immutable sequence of strings) from the root.
        
        This path can be used to access this object again later, for example in
        a copy/paste operation. Returns an empty tuple if the base object is not
        wrapped.
        """
    
    def unrestrictedTraverse(path, default=_RAISE_KEYERROR):
        """
        Return an object given a path (an immutable sequence of strings).

        If the first string in the path sequence is an empty string, start at
        the root. Otherwise the path is relative to the current context.

        If the object is not found then the 'default' argument will be returned.
        """

    def restrictedTraverse(path, default=_RAISE_KEYERROR):
        """
        Return the object obtained by traversing the given path from the object
        on which the method was called, performing security checks along the
        way.
                
        If an object is not found then the 'default' argument will be returned.
        """


=== Added File Zope3/lib/python/Zope/App/Traversing/Traverser.py ===
# Copyright (c) 2001 Zope Corporation and Contributors.  All Rights Reserved.
# 
# This software is subject to the provisions of the Zope Public License,
# Version 1.1 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS 
# FOR A PARTICULAR PURPOSE.

from ITraverser import ITraverser
from Zope.ContextWrapper.IWrapper import IWrapper
from Zope.ContextWrapper import wrapper

from __future__ import generators

# A chain generator; let's us walk the wrapper chain down to the root
def WrapperChain(w):
    while w is not None:
        yield w
        w = wrapper.getcontext(w)

_marker = []

class Traverser:
    """Provide traverse features"""

    __implements__ = ITraverser

    # XXX: This is wishful thinking, as it doesn't work like this yet
    #      Arguably this feature should work for unwrapped objects as well.
    # __used_for__ = IWrapper

    def __init__(self, wrapper):
        self._wrapper = wrapper

    def getPhysicalRoot(self):
        # Loop over all wrappers until the last one, which is the root.
        for w in WrapperChain(self._wrapper): pass
        return w

    def getPhysicalPath(self):
        path = []
        
        for w in WrapperChain(self._wrapper):
            d = wrapper.getdict(w)
            if d: path.insert(0, d['name'])

        return tuple(path)
    
    def unrestrictedTraverse(self, path, default=_marker, restricted=0):
        # stub, never finds.
        if default is not _marker:
            return default

        raise KeyError

    def restrictedTraverse(self, path, default=_marker):
        self.unrestrictedTraverse(self, path, default, restricted=1)



=== Added File Zope3/lib/python/Zope/App/Traversing/__init__.py ===
# This software is subject to the provisions of the Zope Public License,
# Version 1.1 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS 
# FOR A PARTICULAR PURPOSE.

"""
Traversing the object tree.
"""