aboutsummaryrefslogtreecommitdiffstats
path: root/lib/python2.7/site-packages/zope.interface-3.6.4-py2.7-linux-x86_64.egg/zope/interface/adapter.txt
blob: 298a862220c525442aea823c10a9a110a5d0ae15 (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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
================
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::

  >>> 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::

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

Given the registration, we can look it up again::

  >>> 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::

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

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

  >>> 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::

  >>> 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::

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

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

  >>> 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::

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

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

  >>> 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::

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

If we register an object that provides IP1::

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

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

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

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

  >>> 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.

  >>> 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 is a specialized
version of lookup that takes a single required interface::

  >>> registry.lookup1(IR2, IP1, '')
  21
  >>> registry.lookup1(IR2, IP1)
  21

Actual Adaptation
-----------------

The adapter registry is intended to support adaptation, where one
object that implements an interface is adapted to another object that
supports a different interface.  The adapter registry supports the
computation of adapters. In this case, we have to register adapter
factories::

   >>> class IR(zope.interface.Interface):
   ...     pass

   >>> class X:
   ...     zope.interface.implements(IR)
           
   >>> class Y:
   ...     zope.interface.implements(IP1)
   ...     def __init__(self, context):
   ...         self.context = context

  >>> registry.register([IR], IP1, '', Y)

In this case, we registered a class as the factory. Now we can call
`queryAdapter` to get the adapted object::

  >>> x = X()
  >>> y = registry.queryAdapter(x, IP1)
  >>> y.__class__.__name__
  'Y'
  >>> y.context is x
  True

We can register and lookup by name too::

  >>> class Y2(Y):
  ...     pass

  >>> registry.register([IR], IP1, 'bob', Y2)
  >>> y = registry.queryAdapter(x, IP1, 'bob')
  >>> y.__class__.__name__
  'Y2'
  >>> y.context is x
  True

When the adapter factory produces `None`, then this is treated as if no
adapter has been found. This allows us to prevent adaptation (when desired)
and let the adapter factory determine whether adaptation is possible based on
the state of the object being adapted.

  >>> def factory(context):
  ...     if context.name == 'object':
  ...         return 'adapter'
  ...     return None

  >>> class Object(object):
  ...     zope.interface.implements(IR)
  ...     name = 'object'

  >>> registry.register([IR], IP1, 'conditional', factory) 
  >>> obj = Object()
  >>> registry.queryAdapter(obj, IP1, 'conditional')
  'adapter'
  >>> obj.name = 'no object'
  >>> registry.queryAdapter(obj, IP1, 'conditional') is None
  True
  >>> registry.queryAdapter(obj, IP1, 'conditional', 'default')
  'default'

An alternate method that provides the same function as `queryAdapter()` is
`adapter_hook()`::

  >>> y = registry.adapter_hook(IP1, x)
  >>> y.__class__.__name__
  'Y'
  >>> y.context is x
  True
  >>> y = registry.adapter_hook(IP1, x, 'bob')
  >>> y.__class__.__name__
  'Y2'
  >>> y.context is x
  True

The `adapter_hook()` simply switches the order of the object and
interface arguments.  It is used to hook into the interface call
mechanism.


Default Adapters
----------------
  
Sometimes, you want to provide an adapter that will adapt anything.
For that, provide None as the required interface::

  >>> registry.register([None], IP1, '', 1)
  
then we can use that adapter for interfaces we don't have specific
adapters for::

  >>> class IQ(zope.interface.Interface):
  ...     pass
  >>> registry.lookup([IQ], IP1, '')
  1

Of course, specific adapters are still used when applicable::

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

Class adapters
--------------

You can register adapters for class declarations, which is almost the
same as registering them for a class::

  >>> registry.register([zope.interface.implementedBy(C2)], IP1, '', 'C21')
  >>> registry.lookup([zope.interface.implementedBy(C2)], IP1, '')
  'C21'

Dict adapters
-------------

At some point it was impossible to register dictionary-based adapters due a
bug. Let's make sure this works now:

  >>> adapter = {}
  >>> registry.register((), IQ, '', adapter)
  >>> registry.lookup((), IQ, '') is adapter
  True

Unregistering
-------------

You can unregister by registering None, rather than an object::

  >>> registry.register([zope.interface.implementedBy(C2)], IP1, '', None)
  >>> registry.lookup([zope.interface.implementedBy(C2)], IP1, '')
  21

Of course, this means that None can't be registered. This is an
exception to the statement, made earlier, that the registry doesn't
care what gets registered.

Multi-adapters
==============

You can adapt multiple specifications::

  >>> registry.register([IR1, IQ], IP2, '', '1q2')
  >>> registry.lookup([IR1, IQ], IP2, '')
  '1q2'
  >>> registry.lookup([IR2, IQ], IP1, '')
  '1q2'

  >>> class IS(zope.interface.Interface):
  ...     pass
  >>> registry.lookup([IR2, IS], IP1, '')

  >>> class IQ2(IQ):
  ...     pass

  >>> registry.lookup([IR2, IQ2], IP1, '')
  '1q2'

  >>> registry.register([IR1, IQ2], IP2, '', '1q22')
  >>> registry.lookup([IR2, IQ2], IP1, '')
  '1q22'

Multi-adaptation
----------------

You can adapt multiple objects::

  >>> class Q:
  ...     zope.interface.implements(IQ)

As with single adapters, we register a factory, which is often a class::

  >>> class IM(zope.interface.Interface):
  ...     pass
  >>> class M:
  ...     zope.interface.implements(IM)
  ...     def __init__(self, x, q):
  ...         self.x, self.q = x, q
  >>> registry.register([IR, IQ], IM, '', M)

And then we can call `queryMultiAdapter` to compute an adapter::

  >>> q = Q()
  >>> m = registry.queryMultiAdapter((x, q), IM)
  >>> m.__class__.__name__
  'M'
  >>> m.x is x and m.q is q
  True

and, of course, we can use names::

  >>> class M2(M):
  ...     pass
  >>> registry.register([IR, IQ], IM, 'bob', M2)
  >>> m = registry.queryMultiAdapter((x, q), IM, 'bob')
  >>> m.__class__.__name__
  'M2'
  >>> m.x is x and m.q is q
  True
  
Default Adapters
----------------

As with single adapters, you can define default adapters by specifying
None for the *first* specification::

  >>> registry.register([None, IQ], IP2, '', 'q2')
  >>> registry.lookup([IS, IQ], IP2, '')
  'q2'

Null Adapters
=============

You can also adapt no specification::

  >>> registry.register([], IP2, '', 2)
  >>> registry.lookup([], IP2, '')
  2
  >>> registry.lookup([], IP1, '')
  2

Listing named adapters
----------------------

Adapters are named. Sometimes, it's useful to get all of the named
adapters for given interfaces::

  >>> adapters = list(registry.lookupAll([IR1], IP1))
  >>> adapters.sort()
  >>> assert adapters == [(u'', 11), (u'bob', "Bob's 12")]

This works for multi-adapters too::

  >>> registry.register([IR1, IQ2], IP2, 'bob', '1q2 for bob')
  >>> adapters = list(registry.lookupAll([IR2, IQ2], IP1))
  >>> adapters.sort()
  >>> assert adapters == [(u'', '1q22'), (u'bob', '1q2 for bob')]

And even null adapters::

  >>> registry.register([], IP2, 'bob', 3)
  >>> adapters = list(registry.lookupAll([], IP1))
  >>> adapters.sort()
  >>> assert adapters == [(u'', 2), (u'bob', 3)]

Subscriptions
=============

Normally, we want to look up an object that most-closely matches a
specification.  Sometimes, we want to get all of the objects that
match some specification.  We use subscriptions for this.  We
subscribe objects against specifications and then later find all of
the subscribed objects::

  >>> registry.subscribe([IR1], IP2, 'sub12 1')
  >>> registry.subscriptions([IR1], IP2)
  ['sub12 1']

Note that, unlike regular adapters, subscriptions are unnamed.

You can have multiple subscribers for the same specification::

  >>> registry.subscribe([IR1], IP2, 'sub12 2')
  >>> registry.subscriptions([IR1], IP2)
  ['sub12 1', 'sub12 2']

If subscribers are registered for the same required interfaces, they
are returned in the order of definition.

You can register subscribers for all specifications using None::

  >>> registry.subscribe([None], IP1, 'sub_1')
  >>> registry.subscriptions([IR2], IP1)
  ['sub_1', 'sub12 1', 'sub12 2']

Note that the new subscriber is returned first.  Subscribers defined
for less general required interfaces are returned before subscribers
for more general interfaces.

Subscriptions may be combined over multiple compatible specifications::

  >>> registry.subscriptions([IR2], IP1)
  ['sub_1', 'sub12 1', 'sub12 2']
  >>> registry.subscribe([IR1], IP1, 'sub11')
  >>> registry.subscriptions([IR2], IP1)
  ['sub_1', 'sub12 1', 'sub12 2', 'sub11']
  >>> registry.subscribe([IR2], IP2, 'sub22')
  >>> registry.subscriptions([IR2], IP1)
  ['sub_1', 'sub12 1', 'sub12 2', 'sub11', 'sub22']
  >>> registry.subscriptions([IR2], IP2)
  ['sub12 1', 'sub12 2', 'sub22']

Subscriptions can be on multiple specifications::

  >>> registry.subscribe([IR1, IQ], IP2, 'sub1q2')
  >>> registry.subscriptions([IR1, IQ], IP2)
  ['sub1q2']
  
As with single subscriptions and non-subscription adapters, you can
specify None for the first required interface, to specify a default::

  >>> registry.subscribe([None, IQ], IP2, 'sub_q2')
  >>> registry.subscriptions([IS, IQ], IP2)
  ['sub_q2']
  >>> registry.subscriptions([IR1, IQ], IP2)
  ['sub_q2', 'sub1q2']

You can have subscriptions that are indepenent of any specifications::
  
  >>> list(registry.subscriptions([], IP1))
  []

  >>> registry.subscribe([], IP2, 'sub2')
  >>> registry.subscriptions([], IP1)
  ['sub2']
  >>> registry.subscribe([], IP1, 'sub1')
  >>> registry.subscriptions([], IP1)
  ['sub2', 'sub1']
  >>> registry.subscriptions([], IP2)
  ['sub2']

Unregistering subscribers
-------------------------

We can unregister subscribers.  When unregistering a subscriber, we
can unregister a specific subscriber::

  >>> registry.unsubscribe([IR1], IP1, 'sub11')
  >>> registry.subscriptions([IR1], IP1)
  ['sub_1', 'sub12 1', 'sub12 2']

If we don't specify a value, then all subscribers matching the given
interfaces will be unsubscribed:

  >>> registry.unsubscribe([IR1], IP2)
  >>> registry.subscriptions([IR1], IP1)
  ['sub_1']


Subscription adapters
---------------------

We normally register adapter factories, which then allow us to compute
adapters, but with subscriptions, we get multiple adapters.  Here's an
example of multiple-object subscribers::

  >>> registry.subscribe([IR, IQ], IM, M)
  >>> registry.subscribe([IR, IQ], IM, M2)

  >>> subscribers = registry.subscribers((x, q), IM)
  >>> len(subscribers)
  2
  >>> class_names = [s.__class__.__name__ for s in subscribers]
  >>> class_names.sort()
  >>> class_names
  ['M', 'M2']
  >>> [(s.x is x and s.q is q) for s in subscribers]
  [True, True]

adapter factory subcribers can't return None values::

  >>> def M3(x, y):
  ...     return None

  >>> registry.subscribe([IR, IQ], IM, M3)
  >>> subscribers = registry.subscribers((x, q), IM)
  >>> len(subscribers)
  2

Handlers
--------

A handler is a subscriber factory that doesn't produce any normal
output.  It returns None.  A handler is unlike adapters in that it does
all of its work when the factory is called.

To register a handler, simply provide None as the provided interface::

  >>> def handler(event):
  ...     print 'handler', event

  >>> registry.subscribe([IR1], None, handler)
  >>> registry.subscriptions([IR1], None) == [handler]
  True