Function
zope.traversing.namespace.namespaceLookup

Signature

namespaceLookup(ns, name, object, request=None)

Documentation String

Lookup a value from a namespace

We look up a value using a view or an adapter, depending on whether a request is passed.

Let's start with adapter-based transersal:

>>> class I(zope.interface.Interface):
...     'Test interface'
>>> class C(object):
...     zope.interface.implements(I)

We'll register a simple testing adapter:

>>> class Adapter(object):
...     def __init__(self, context):
...         self.context = context
...     def traverse(self, name, remaining):
...         return name+'42'
>>> zope.component.provideAdapter(Adapter, (I,), ITraversable, 'foo')

Then given an object, we can traverse it with a namespace-qualified name:

>>> namespaceLookup('foo', 'bar', C())
'bar42'

If we give an invalid namespace, we'll get a not found error:

>>> namespaceLookup('fiz', 'bar', C())    # doctest: +ELLIPSIS
Traceback (most recent call last):
...
TraversalError: (<zope.traversing.namespace.C object at 0x...>, '++fiz++bar')

We'll get the same thing if we provide a request:

>>> from zope.publisher.browser import TestRequest
>>> request = TestRequest()
>>> namespaceLookup('foo', 'bar', C(), request)    # doctest: +ELLIPSIS
Traceback (most recent call last):
...
TraversalError: (<zope.traversing.namespace.C object at 0x...>, '++foo++bar')

We need to provide a view:

>>> class View(object):
...     def __init__(self, context, request):
...         pass
...     def traverse(self, name, remaining):
...         return name+'fromview'
>>> from zope.traversing.testing import browserView
>>> browserView(I, 'foo', View, providing=ITraversable)
>>> namespaceLookup('foo', 'bar', C(), request)
'barfromview'

Clean up:

>>> from zope.testing.cleanup import cleanUp
>>> cleanUp()