Reference

Traversal

Generic traversal functions (and adapters).

class nti.traversal.traversal.ContainerAdapterTraversable(context, request=None)[source]

Bases: ContainerTraversable

A ITraversable implementation for containers that also automatically looks for named path adapters if the container has no matching key.

You may subclass this traversable or register it in ZCML directly. It is usable both with and without a request.

property context
traverse(key, remaining_path)[source]
class nti.traversal.traversal.DefaultAdapterTraversable(context, request=None)[source]

Bases: DefaultTraversable

A ITraversable implementation for ordinary objects that also automatically looks for named path adapters if the traversal found no matching path.

You may subclass this traversable or register it in ZCML directly. It is usable both with and without a request.

traverse(key, remaining_path)[source]
class nti.traversal.traversal.adapter_request(context, request=None)[source]

Bases: adapter

Implementation of the adapter namespace that attempts to pass the request along when getting an adapter.

traverse(name, ignored)[source]

To demonstrate this, we need to register some adapters:

>>> def adapter1(ob):
...     return 1
>>> def adapter2(ob):
...     return 2
>>> zope.component.provideAdapter(
...     adapter1, (None,), IPathAdapter, 'a1')
>>> zope.component.provideAdapter(
...     adapter2, (None,), IPathAdapter, 'a2')

Now, with these adapters in place, we can use the traversal adapter:

>>> ob = object()
>>> adapter = adapter(ob)
>>> adapter.traverse('a1', ())
1
>>> adapter.traverse('a2', ())
2
>>> try:
...     adapter.traverse('bob', ())
... except LocationError:
...     print('no adapter')
no adapter

Clean up:

>>> from zope.testing.cleanup import cleanUp
>>> cleanUp()
nti.traversal.traversal.find_interface(resource, interface, strict=True)[source]

Given an object, find the first object in its lineage providing the given interface.

This is similar to nti.traversal.location.find_interface(), but, as with resource_path() requires the strict adherence to the resource tree, unless strict is set to False.

Parameters

strict (bool) – Deprecated. Do not use. Non-strict lineage is broken lineage.

nti.traversal.traversal.find_nearest_site(context, root=None, ignore=None)[source]

Find the nearest ISite in the lineage of context.

Parameters
  • context – The object whose lineage to search. If this object cannot be adapted to ILocationInfo, then we attempt to adapt context.target and get its site; failing that, we return the root.

  • ignore – If the ILocationInfo of the context can be retrieved, but the ILocationInfo.getNearestSite() cannot, then, if ignore is given, and context provides that interface, return the root. This makes no sense and is deprecated.

Returns

The nearest site. Possibly the root site.

Deprecated since version 1.0: Relying on the fallback to context.target and root is deprecated; the ignore parameter is deprecated. All of these things signal a broken resource tree.

nti.traversal.traversal.is_valid_resource_path(target)[source]
nti.traversal.traversal.normal_resource_path(res)[source]
Returns

The result of traversing the containers of res,

but normalized by removing double slashes. This is useful when elements in the containment hierarchy do not have a name; however, it can hide bugs when all elements are expected to have names.

nti.traversal.traversal.path_adapter(context, request, name='')[source]
nti.traversal.traversal.resource_path(res)[source]

Location

This module is deprecated. It contains functions that ignore errors in the location hierarchy. That can be a serious problem and must not be ignored.

Prefer the functions in nti.traversal.traversal.

nti.traversal.location.find_interface(resource, class_or_interface)[source]

Search for an object implementing class_or_interface in the lineage() of the resource.

Parameters

class_or_interface – Can be an interface or a concrete class to check with isinstance().

Deprecated, do not use.

Deprecated since version 1.0: This doesn’t use the zope.location.interfaces.ILocationInfo interface, and hence doesn’t let the resource have customizations and ignores problems in the resource tree.

nti.traversal.location.lineage(resource)[source]

Return all the parents of resource.

Deprecated, do not use.

Deprecated since version 1.0: This doesn’t use the zope.location.interfaces.ILocationInfo interface, and hence doesn’t let the resource have customizations and ignores problems in the resource tree.