summaryrefslogtreecommitdiff
path: root/docs/api/adapter.rst
blob: c3245ddcd9a5e6b2271056c641960a9bd2069e61 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
===========================
 Adapter Registration APIs
===========================

This document covers a specific subset of the APIs in :mod:`zope.component`.

.. currentmodule:: zope.component

.. testsetup::

   from zope.component.testing import setUp
   setUp()

.. autofunction:: zope.component.provideAdapter

.. autofunction:: zope.component.provideHandler

.. autofunction:: zope.component.provideSubscriptionAdapter

Conforming Adapter Lookup
=========================

.. autofunction:: zope.component.getAdapterInContext

.. autofunction:: zope.component.queryAdapterInContext

The :func:`~zope.component.getAdapterInContext` and
:func:`~zope.component.queryAdapterInContext` APIs first check the
context object to see if it already conforms to the requested interface.
If so, the object is returned immediately.  Otherwise, the adapter factory
is looked up in the site manager, and called.

Let's start by creating a component that supports the ``__conform__()`` method:

.. doctest::

   >>> from zope.interface import implementer
   >>> from zope.component.tests.examples import I1
   >>> from zope.component.tests.examples import I2
   >>> @implementer(I1)
   ... class Component(object):
   ...     def __conform__(self, iface, default=None):
   ...         if iface == I2:
   ...             return 42
   >>> ob = Component()

We also gave the component a custom representation, so it will be easier
to use in these tests.

We now have to create a site manager (other than the default global one)
with which we can register adapters for ``I1``.

.. doctest::

   >>> from zope.component.globalregistry import BaseGlobalComponents
   >>> sitemanager = BaseGlobalComponents()

Now we create a new ``context`` that knows how to get to our custom site
manager.

.. doctest::

   >>> from zope.component.tests.examples import ConformsToIComponentLookup
   >>> context = ConformsToIComponentLookup(sitemanager)

If an object implements the interface you want to adapt to,
`getAdapterInContext()` should simply return the object.

.. doctest::

   >>> from zope.component import getAdapterInContext
   >>> from zope.component import queryAdapterInContext
   >>> getAdapterInContext(ob, I1, context) is ob
   True
   >>> queryAdapterInContext(ob, I1, context) is ob
   True

If an object conforms to the interface you want to adapt to,
`getAdapterInContext()` should simply return the conformed object.

.. doctest::

   >>> getAdapterInContext(ob, I2, context)
   42
   >>> queryAdapterInContext(ob, I2, context)
   42

If an adapter isn't registered for the given object and interface, and you
provide no default, the `getAdapterInContext` API raises ComponentLookupError:

.. doctest::

   >>> from zope.interface import Interface
   >>> class I4(Interface):
   ...     pass

   >>> getAdapterInContext(ob, I4, context)
   Traceback (most recent call last):
   ...
   ComponentLookupError: (<Component implementing 'I1'>,
                          <InterfaceClass ...I4>)

While the `queryAdapterInContext` API returns the default:

.. doctest::

   >>> queryAdapterInContext(ob, I4, context, 44)
   44

If you ask for an adapter for which something's registered you get the
registered adapter:

.. doctest::

   >>> from zope.component.tests.examples import I3
   >>> sitemanager.registerAdapter(lambda x: 43, (I1,), I3, '')
   >>> getAdapterInContext(ob, I3, context)
   43
   >>> queryAdapterInContext(ob, I3, context)
   43

Named Adapter Lookup
====================

.. autofunction:: zope.component.getAdapter

.. autofunction:: zope.component.queryAdapter

The ``getAdapter`` and ``queryAdapter`` API functions are similar to
``{get|query}AdapterInContext()`` functions, except that they do not care
about the ``__conform__()`` but also handle named adapters. (Actually, the
name is a required argument.)

If no adapter is registered for the given object, interface, and name,
``getAdapter`` raises ``ComponentLookupError``, while ``queryAdapter``
returns the default:

.. doctest::

   >>> from zope.component import getAdapter
   >>> from zope.component import queryAdapter
   >>> from zope.component.tests.examples import I2
   >>> from zope.component.tests.examples import ob
   >>> getAdapter(ob, I2, '')
   Traceback (most recent call last):
   ...
   ComponentLookupError: (<instance Ob>,
                          <InterfaceClass zope.component.tests.examples.I2>,
                          '')
   >>> queryAdapter(ob, I2, '', '<default>')
   '<default>'

The 'requires' argument to ``registerAdapter`` must be a sequence, rather than
a single interface:

.. doctest::

   >>> from zope.component import getGlobalSiteManager
   >>> from zope.component.tests.examples import Comp
   >>> gsm = getGlobalSiteManager()
   >>> gsm.registerAdapter(Comp, I1, I2, '')
   Traceback (most recent call last):
     ...
   TypeError: the required argument should be a list of interfaces, not a single interface

After register an adapter from ``I1`` to ``I2`` with the global site manager:

.. doctest::

   >>> from zope.component import getGlobalSiteManager
   >>> from zope.component.tests.examples import Comp
   >>> gsm = getGlobalSiteManager()
   >>> gsm.registerAdapter(Comp, (I1,), I2, '')

We can access the adapter using the `getAdapter()` API:

.. doctest::

   >>> adapted = getAdapter(ob, I2, '')
   >>> adapted.__class__ is Comp
   True
   >>> adapted.context is ob
   True
   >>> adapted = queryAdapter(ob, I2, '')
   >>> adapted.__class__ is Comp
   True
   >>> adapted.context is ob
   True

If we search using a non-anonymous name, before registering:

.. doctest::

   >>> getAdapter(ob, I2, 'named')
   Traceback (most recent call last):
   ...
   ComponentLookupError: (<instance Ob>,
                          <InterfaceClass ....I2>,
                          'named')
   >>> queryAdapter(ob, I2, 'named', '<default>')
   '<default>'

After registering under that name:

.. doctest::

   >>> gsm.registerAdapter(Comp, (I1,), I2, 'named')
   >>> adapted = getAdapter(ob, I2, 'named')
   >>> adapted.__class__ is Comp
   True
   >>> adapted.context is ob
   True
   >>> adapted = queryAdapter(ob, I2, 'named')
   >>> adapted.__class__ is Comp
   True
   >>> adapted.context is ob
   True

Invoking an Interface to Perform Adapter Lookup
===============================================

:mod:`zope.component` registers an adapter hook with
`zope.interface.interface.adapter_hooks`, allowing a convenient spelling for
adapter lookup:  just "call" the interface, passing the context:

.. doctest::

   >>> adapted = I2(ob)
   >>> adapted.__class__ is Comp
   True
   >>> adapted.context is ob
   True

If the lookup fails, we get a `TypeError`:

.. doctest::

   >>> I2(object())
   Traceback (most recent call last):
   ...
   TypeError: ('Could not adapt'...

unless we pass a default:

.. doctest::

   >>> marker = object()
   >>> adapted = I2(object(), marker)
   >>> adapted is marker
   True

Registering Adapters For Arbitrary Objects
==========================================

Providing an adapter for None says that your adapter can adapt anything
to ``I2``.

.. doctest::

   >>> gsm.registerAdapter(Comp, (None,), I2, '')
   >>> adapter = I2(ob)
   >>> adapter.__class__ is Comp
   True
   >>> adapter.context is ob
   True

It can really adapt any arbitrary object:

.. doctest::

   >>> something = object()
   >>> adapter = I2(something)
   >>> adapter.__class__ is Comp
   True
   >>> adapter.context is something
   True

Looking Up Adapters Using Multiple Objects
==========================================

.. autofunction:: zope.component.getMultiAdapter

.. autofunction:: zope.component.queryMultiAdapter

Multi-adapters adapt one or more objects to another interface. To make
this demonstration non-trivial, we need to create a second object to be
adapted:

.. doctest::

   >>> from zope.component.tests.examples import Ob2
   >>> ob2 = Ob2()

As with regular adapters, if an adapter isn't registered for the given
objects and interface, the :func:`~zope.component.getMultiAdapter` API
raises `zope.interface.interfaces.ComponentLookupError`:

.. doctest::

   >>> from zope.component import getMultiAdapter
   >>> getMultiAdapter((ob, ob2), I3)
   Traceback (most recent call last):
   ...
   ComponentLookupError:
   ((<instance Ob>, <instance Ob2>),
    <InterfaceClass zope.component.tests.examples.I3>,
    u'')

while the :func:`~zope.component.queryMultiAdapter` API returns the default:

.. doctest::

   >>> from zope.component import queryMultiAdapter
   >>> queryMultiAdapter((ob, ob2), I3, default='<default>')
   '<default>'

Note that ``name`` is not a required attribute here.

To test multi-adapters, we also have to create an adapter class that
handles to context objects:

.. doctest::

   >>> from zope.interface import implementer
   >>> @implementer(I3)
   ... class DoubleAdapter(object):
   ...     def __init__(self, first, second):
   ...         self.first = first
   ...         self.second = second

Now we can register the multi-adapter:

.. doctest::

   >>> from zope.component import getGlobalSiteManager
   >>> getGlobalSiteManager().registerAdapter(DoubleAdapter, (I1, I2), I3, '')

Notice how the required interfaces are simply provided by a tuple.

Now we can get the adapter:

.. doctest::

   >>> adapter = getMultiAdapter((ob, ob2), I3)
   >>> adapter.__class__ is DoubleAdapter
   True
   >>> adapter.first is ob
   True
   >>> adapter.second is ob2
   True


Finding More Than One Adapter
=============================

.. autofunction:: zope.component.getAdapters

It is sometimes desireable to get a list of all adapters that are
registered for a particular output interface, given a set of
objects.

Let's register some adapters first:

.. doctest::

   >>> class I5(I1):
   ...     pass
   >>> gsm.registerAdapter(Comp, [I1], I5, '')
   >>> gsm.registerAdapter(Comp, [None], I5, 'foo')

Now we get all the adapters that are registered for ``ob`` that provide
``I5`` (note that the names are always text strings, meaning that on
Python 2 the names will be ``unicode``):

.. doctest::

   >>> from zope.component import getAdapters
   >>> adapters = sorted(getAdapters((ob,), I5))
   >>> [(str(name), adapter.__class__.__name__) for name, adapter in adapters]
   [('', 'Comp'), ('foo', 'Comp')]
   >>> try:
   ...    text = unicode
   ... except NameError:
   ...    text = str # Python 3
   >>> [isinstance(name, text) for name, _ in adapters]
   [True, True]

Note that the output doesn't include None values. If an adapter
factory returns None, it is as if it wasn't present.

.. doctest::

   >>> gsm.registerAdapter(lambda context: None, [I1], I5, 'nah')
   >>> adapters = sorted(getAdapters((ob,), I5))
   >>> [(str(name), adapter.__class__.__name__) for name, adapter in adapters]
   [('', 'Comp'), ('foo', 'Comp')]


Subscription Adapters
=====================

.. autofunction:: zope.component.subscribers

Event handlers
==============

.. autofunction:: zope.component.handle

Helpers for Declaring / Testing Adapters
========================================

.. autofunction:: zope.component.adapter

.. autofunction:: zope.component.adaptedBy

.. autofunction:: zope.component.adapts


.. testcleanup::

   from zope.component.testing import tearDown
   tearDown()