Exemplo n.º 1
0
    def traverseName(self, request, ob, name):
        nm = name # the name to look up the object with

        if name and name[:1] in '@+':
            # Process URI segment parameters.
            ns, nm = nsParse(name)
            if ns:
                try:
                    ob2 = namespaceLookup(ns, nm, ob, request)
                except TraversalError:
                    raise NotFound(ob, name)

                return ProxyFactory(ob2)

        if nm == '.':
            return ob

        if IPublishTraverse.providedBy(ob):
            ob2 = ob.publishTraverse(request, nm)
        else:
            # self is marker
            adapter = zapi.queryMultiAdapter((ob, request), IPublishTraverse,
                                             default=self)
            if adapter is not self:
                ob2 = adapter.publishTraverse(request, nm)
            else:
                raise NotFound(ob, name, request)

        return ProxyFactory(ob2)
Exemplo n.º 2
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
Exemplo n.º 3
0
def trustedTraverse(
    ob,
    path,
    ignored,
):
    if not path: return self

    get = getattr
    has = hasattr
    N = None
    M = rebindFunction  # artifical marker

    if isinstance(path, str): path = path.split('/')
    else: path = list(path)

    REQUEST = get(ob, 'REQUEST', None)
    if REQUEST is None:
        REQUEST = FakeRequest()
    setDefaultSkin(REQUEST)
    REQUEST['TraversalRequestNameStack'] = path
    path.reverse()
    pop = path.pop

    if len(path) > 1 and not path[0]:
        # Remove trailing slash
        path.pop(0)

    if not path[-1]:
        # If the path starts with an empty string, go to the root first.
        pop()
        self = ob.getPhysicalRoot()

    object = ob
    while path:
        name = pop()
        __traceback_info__ = path, name

        if name == '..':
            o = getattr(object, 'aq_parent', M)
            if o is not M:
                object = o
                continue

        if name and name[:1] in '@+':
            # Process URI segment parameters.
            ns, nm = nsParse(name)
            if ns:
                try:
                    o = namespaceLookup(ns, nm, object, REQUEST).__of__(object)
                except TraversalError:
                    raise KeyError(name)
                object = o
                continue

        t = get(object, '__bobo_traverse__', M)
        if t is not M: o = t(REQUEST, name)
        else:
            o = get(object, name, M)
            if o is M:
                try:
                    o = object[name]
                except (AttributeError, TypeError):  # better exception
                    o = queryMultiAdapter((object, REQUEST), Interface, name)
                    if o is not None:
                        o = o.__of__(object)
                    else:
                        raise AttributeError(name)
        object = o

    return object
Exemplo n.º 4
0
def trustedTraverse(ob, path, ignored,):
  if not path: return self

  get = getattr
  has = hasattr
  N = None
  M = rebindFunction # artifical marker

  if isinstance(path, str): path = path.split('/')
  else: path=list(path)
  
  REQUEST = get(ob, 'REQUEST', None)
  if REQUEST is None:
    REQUEST=FakeRequest()
  setDefaultSkin(REQUEST)
  REQUEST['TraversalRequestNameStack'] = path
  path.reverse()
  pop=path.pop

  if len(path) > 1 and not path[0]:
    # Remove trailing slash
    path.pop(0)

  if not path[-1]:
    # If the path starts with an empty string, go to the root first.
    pop()
    self=ob.getPhysicalRoot()

  object = ob
  while path:
    name=pop()
    __traceback_info__ = path, name

    if name == '..':
      o=getattr(object, 'aq_parent', M)
      if o is not M:
        object=o
        continue

    if name and name[:1] in '@+':
      # Process URI segment parameters.
      ns, nm = nsParse(name)
      if ns:
        try:
          o = namespaceLookup(ns, nm, object, REQUEST).__of__(object)
        except TraversalError:
          raise KeyError(name)
        object = o
        continue
        
    t=get(object, '__bobo_traverse__', M)
    if t is not M: o=t(REQUEST, name)
    else:
      o = get(object, name, M)
      if o is M:
        try: o = object[name]
        except (AttributeError, TypeError): # better exception
            o = queryMultiAdapter((object, REQUEST), Interface, name)
            if o is not None:
                o = o.__of__(object)
            else:
                raise AttributeError(name)
    object = o

  return object