4:34
      4:23
      4:38
  #   ˆl–ß=¿J	å0–5 ² ^¸ØnÅ1hßÃ„al?ÂÁ ;/    ================
Adapter Registry
================

Adapter registries provide a way to register objects that depend on
one or more interface specifications and provide (perhaps indirectly)
some interface.  In addition, the registrations have names. (You can
think of the names as qualifiers of the provided interfaces.)

The term "interface specification" refers both to interfaces and to
interface declarations, such as declarations of interfaces implemented
by a class.


Single Adapters
===============

Let's look at a simple example, using a single required specification:

.. doctest::

  >>> from zope.interface.adapter import AdapterRegistry
  >>> import zope.interface

  >>> class IR1(zope.interface.Interface):
  ...     pass
  >>> class IP1(zope.interface.Interface):
  ...     pass
  >>> class IP2(IP1):
  ...     pass

  >>> registry = AdapterRegistry()

We'll register an object that depends on IR1 and "provides" IP2:

.. doctest::

  >>> registry.register([IR1], IP2, '', 12)

Given the registration, we can look it up again:

.. doctest::

  >>> registry.lookup([IR1], IP2, '')
  12

Note that we used an integer in the example.  In real applications,
one would use some objects that actually depend on or provide
interfaces. The registry doesn't care about what gets registered, so
we'll use integers and strings to keep the examples simple. There is
one exception.  Registering a value of None unregisters any
previously-registered value.

If an object depends on a specification, it can be looked up with a
specification that extends the specification that it depends on:

.. doctest::

  >>> class IR2(IR1):
  ...     pass
  >>> registry.lookup([IR2], IP2, '')
  12

We can use a class implementation specification to look up the object:

.. doctest::

  >>> class C2:
  ...     zope.interface.implements(IR2)

  >>> registry.lookup([zope.interface.implementedBy(C2)], IP2, '')
  12


and it can be looked up for interfaces that its provided interface
extends:

.. doctest::

  >>> registry.lookup([IR1], IP1, '')
  12
  >>> registry.lookup([IR2], IP1, '')
  12

But if you require a specification that doesn't extend the specification the
object depends on, you won't get anything:

.. doctest::

  >>> registry.lookup([zope.interface.Interface], IP1, '')

By the way, you can pass a default value to lookup:

.. doctest::

  >>> registry.lookup([zope.interface.Interface], IP1, '', 42)
  42

If you try to get an interface the object doesn't provide, you also
won't get anything:

.. doctest::

  >>> class IP3(IP2):
  ...     pass
  >>> registry.lookup([IR1], IP3, '')

You also won't get anything if you use the wrong name:

.. doctest::

  >>> registry.lookup([IR1], IP1, 'bob')
  >>> registry.register([IR1], IP2, 'bob', "Bob's 12")
  >>> registry.lookup([IR1], IP1, 'bob')
  "Bob's 12"

You can leave the name off when doing a lookup:

.. doctest::

  >>> registry.lookup([IR1], IP1)
  12

If we register an object that provides IP1:

.. doctest::

  >>> registry.register([IR1], IP1, '', 11)

then that object will be prefered over O(12):

.. doctest::

  >>> registry.lookup([IR1], IP1, '')
  11

Also, if we register an object for IR2, then that will be prefered
when using IR2:

.. doctest::

  >>> registry.register([IR2], IP1, '', 21)
  >>> registry.lookup([IR2], IP1, '')
  21

Finding out what, if anything, is registered
--------------------------------------------

We can ask if there is an adapter registered for a collection of
interfaces. This is different than lookup, because it looks for an
exact match:

.. doctest::

  >>> print registry.registered([IR1], IP1)
  11

  >>> print registry.registered([IR1], IP2)
  12

  >>> print registry.registered([IR1], IP2, 'bob')
  Bob's 12
  

  >>> print registry.registered([IR2], IP1)
  21

  >>> print registry.registered([IR2], IP2)
  None

In the last example, None was returned because nothing was registered
exactly for the given interfaces.

lookup1
-------

Lookup of single adapters is common enough that there 