Ejemplo n.º 1
0
 def publishTraverse(self, request, name):
     adapter = ITraversable(self.context)
     if adapter is not None:
         try:
             return adapter.traverse(name, ())
         except TraversalError:
             pass
     view = zapi.queryMultiAdapter((self.context, request), name=name)
     if view is not None:
         return view
     return getResource(self.context, name, request)
Ejemplo n.º 2
0
 def testImplementsITraversable(self):
     self.failUnless(ITraversable.providedBy(DefaultTraversable(None)))
Ejemplo n.º 3
0
def traversePathElement(obj, name, further_path, default=_marker, traversable=None, request=None):
    """Traverse a single step 'name' relative to the given object.

    'name' must be a string. '.' and '..' are treated specially, as well as
    names starting with '@' or '+'. Otherwise 'name' will be treated as a
    single path segment.

    'further_path' is a list of names still to be traversed.  This method
    is allowed to change the contents of 'further_path'.

    You can explicitly pass in an ITraversable as the 'traversable'
    argument. If you do not, the given object will be adapted to ITraversable.

    'request' is passed in when traversing from presentation code. This
    allows paths like @@foo to work.

    Raises TraversalError if path cannot be found and 'default' was
    not provided.

    """
    if name == ".":
        return obj

    if name == "..":
        return obj.__parent__

    if name and name[:1] in "@+":
        ns, nm = nsParse(name)
        if ns:
            return namespaceLookup(ns, nm, obj, request)
    else:
        nm = name

    if traversable is None:
        # not all objects have __class__, for example old style classes
        if getattr(obj, "__class__", None) == dict:
            # Special-case dicts
            return obj[name]

        traversable = ITraversable(obj, None)
        if traversable is None:
            raise TraversalError("No traversable adapter found", obj)

    try:
        return traversable.traverse(nm, further_path)
    except TraversalError:
        if default is not _marker:
            return default
        else:
            raise
    except NotFoundError, v:  # BBB Backward Compatibility
        warnings.warn(
            "A %s instance raised a NotFoundError in "
            "traverse.  Raising NotFoundError in this "
            "method is deprecated and will no-longer be supported "
            "starting in Zope 3.3.  TraversalError should "
            "be raised instead." % traversable.__class__.__name__,
            DeprecationWarning,
        )
        if default is not _marker:
            return default
        else:
            raise
Ejemplo n.º 4
0
    def traverse(self, name, remaining):
        """Acquire a name

           Let's set up some example data:

             >>> class testcontent(object):
             ...     zope.interface.implements(ITraversable)
             ...     def traverse(self, name, remaining):
             ...         v = getattr(self, name, None)
             ...         if v is None:
             ...             raise TraversalError(name)
             ...         return v
             ...     def __repr__(self):
             ...         return 'splat'

             >>> ob = testcontent()
             >>> ob.a = 1
             >>> ob.__parent__ = testcontent()
             >>> ob.__parent__.b = 2
             >>> ob.__parent__.__parent__ = testcontent()
             >>> ob.__parent__.__parent__.c = 3

           And acquire some names:

             >>> adapter = acquire(ob)

             >>> adapter.traverse('a', ())
             1

             >>> adapter.traverse('b', ())
             2

             >>> adapter.traverse('c', ())
             3

             >>> adapter.traverse('d', ())
             Traceback (most recent call last):
             ...
             TraversalError: (splat, 'd')
           """
        i = 0
        ob = self.context
        while i < 200:
            i += 1
            traversable = ITraversable(ob, None)
            if traversable is not None:
                try:
                    # ??? what do we do if the path gets bigger?
                    path = []
                    next = traversable.traverse(name, path)
                    if path:
                        continue
                except TraversalError:
                    pass

                except NotFoundError, v: # BBB Backward Compatibility
                    warnings.warn(
                        "A %s instance raised a NotFoundError in "
                        "traverse.  Raising NotFoundError in this "
                        "method is deprecated and will no-longer be supported "
                        "staring in Zope 3.3.  TraversalError should "
                        "be raised instead."
                        % traversable.__class__.__name__,
                        DeprecationWarning)
                else:
                    return next

            ob = getattr(ob, '__parent__', None)
            if ob is None:
                raise TraversalError(self.context, name)