[Zope-Checkins] CVS: Zope3/lib/python/Zope/App - ITraversable.py:1.1.2.1 Traversable.py:1.1.2.1

Martijn Pieters mj@zope.com
Fri, 30 Nov 2001 22:36:42 -0500


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

Added Files:
      Tag: Zope-3x-branch
	ITraversable.py Traversable.py 
Log Message:
Checkpoint checkin of ITraversable work.


=== Added File Zope3/lib/python/Zope/App/ITraversable.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 ITraversable(Interface.Base):
    """Provide traverse features"""

    def getPhysicalRoot():
        """
        Returns the top-level Application object.
        """

    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.
        """
    
    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/Traversable.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 ITraversable import ITraversable
from Zope.ContextWrapper.IWrapper import IWrapper
from Zope.ContextWrapper import wrapper

_marker = []

class Traversable:
    """Provide traverse features"""

    __implements__ = ITraversable

    __used_for__ = IWrapper

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

    def getPhysicalRoot(self):
        return wrapper.getbaseobject(self._wrapper)

    def getPhysicalPath(self):
        path = ()
        w = self._wrapper
        while w:
            path.insert(0, wrapper.getdict(w)['name'])
            w = wrapper.getcontext(w)
        path.insert(0, '')

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

        raise KeyError

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