summaryrefslogtreecommitdiff
path: root/docs
diff options
context:
space:
mode:
authorJean Jordaan <jean.jordaan@gmail.com>2016-10-16 12:50:27 -0400
committerJean Jordaan <jean.jordaan@gmail.com>2016-10-16 12:50:27 -0400
commit6a083e856ab871efb0f8b5b72329f996a4fe3114 (patch)
tree0b8c2266249df54a59f0ac41b0f9263e684fd5db /docs
parent02f377100123e98763127d92a7b456c65b159322 (diff)
downloadzope-interface-6a083e856ab871efb0f8b5b72329f996a4fe3114.tar.gz
Editing while reading, and spell out Require and Provide
Changing IR to IRequire (or IRequired?) feels much more readable to me ..
Diffstat (limited to 'docs')
-rw-r--r--docs/adapter.rst242
1 files changed, 121 insertions, 121 deletions
diff --git a/docs/adapter.rst b/docs/adapter.rst
index b948705..c5c65e5 100644
--- a/docs/adapter.rst
+++ b/docs/adapter.rst
@@ -22,33 +22,33 @@ 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):
+ >>> class IRequire1(zope.interface.Interface):
... pass
- >>> class IP1(zope.interface.Interface):
+ >>> class IProvide1(zope.interface.Interface):
... pass
- >>> class IP2(IP1):
+ >>> class IProvide2(IProvide1):
... pass
>>> registry = AdapterRegistry()
-We'll register an object that depends on IR1 and "provides" IP2:
+We'll register an object that depends on ``IRequire1`` and "provides" ``IProvide2``:
.. doctest::
- >>> registry.register([IR1], IP2, '', 12)
+ >>> registry.register([IRequire1], IProvide2, '', 12)
Given the registration, we can look it up again:
.. doctest::
- >>> registry.lookup([IR1], IP2, '')
+ >>> registry.lookup([IRequire1], IProvide2, '')
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
+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
@@ -56,9 +56,9 @@ specification that extends the specification that it depends on:
.. doctest::
- >>> class IR2(IR1):
+ >>> class IRequire2(IRequire1):
... pass
- >>> registry.lookup([IR2], IP2, '')
+ >>> registry.lookup([IRequire2], IProvide2, '')
12
We can use a class implementation specification to look up the object:
@@ -66,9 +66,9 @@ We can use a class implementation specification to look up the object:
.. doctest::
>>> class C2:
- ... zope.interface.implements(IR2)
+ ... zope.interface.implements(IRequire2)
- >>> registry.lookup([zope.interface.implementedBy(C2)], IP2, '')
+ >>> registry.lookup([zope.interface.implementedBy(C2)], IProvide2, '')
12
@@ -77,9 +77,9 @@ extends:
.. doctest::
- >>> registry.lookup([IR1], IP1, '')
+ >>> registry.lookup([IRequire1], IProvide1, '')
12
- >>> registry.lookup([IR2], IP1, '')
+ >>> registry.lookup([IRequire2], IProvide1, '')
12
But if you require a specification that doesn't extend the specification the
@@ -87,13 +87,13 @@ object depends on, you won't get anything:
.. doctest::
- >>> registry.lookup([zope.interface.Interface], IP1, '')
+ >>> registry.lookup([zope.interface.Interface], IProvide1, '')
By the way, you can pass a default value to lookup:
.. doctest::
- >>> registry.lookup([zope.interface.Interface], IP1, '', 42)
+ >>> registry.lookup([zope.interface.Interface], IProvide1, '', 42)
42
If you try to get an interface the object doesn't provide, you also
@@ -101,46 +101,46 @@ won't get anything:
.. doctest::
- >>> class IP3(IP2):
+ >>> class IProvide3(IProvide2):
... pass
- >>> registry.lookup([IR1], IP3, '')
+ >>> registry.lookup([IRequire1], IProvide3, '')
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')
+ >>> registry.lookup([IRequire1], IProvide1, 'bob')
+ >>> registry.register([IRequire1], IProvide2, 'bob', "Bob's 12")
+ >>> registry.lookup([IRequire1], IProvide1, 'bob')
"Bob's 12"
You can leave the name off when doing a lookup:
.. doctest::
- >>> registry.lookup([IR1], IP1)
+ >>> registry.lookup([IRequire1], IProvide1)
12
-If we register an object that provides IP1:
+If we register an object that provides ``IProvide1``:
.. doctest::
- >>> registry.register([IR1], IP1, '', 11)
+ >>> registry.register([IRequire1], IProvide1, '', 11)
-then that object will be prefered over O(12):
+then that object will be prefered over ``O(12)``:
.. doctest::
- >>> registry.lookup([IR1], IP1, '')
+ >>> registry.lookup([IRequire1], IProvide1, '')
11
-Also, if we register an object for IR2, then that will be prefered
-when using IR2:
+Also, if we register an object for ``IRequire2``, then that will be preferred
+when using ``IRequire2``:
.. doctest::
- >>> registry.register([IR2], IP1, '', 21)
- >>> registry.lookup([IR2], IP1, '')
+ >>> registry.register([IRequire2], IProvide1, '', 21)
+ >>> registry.lookup([IRequire2], IProvide1, '')
21
Finding out what, if anything, is registered
@@ -152,23 +152,23 @@ exact match:
.. doctest::
- >>> print registry.registered([IR1], IP1)
+ >>> print registry.registered([IRequire1], IProvide1)
11
- >>> print registry.registered([IR1], IP2)
+ >>> print registry.registered([IRequire1], IProvide2)
12
- >>> print registry.registered([IR1], IP2, 'bob')
+ >>> print registry.registered([IRequire1], IProvide2, 'bob')
Bob's 12
- >>> print registry.registered([IR2], IP1)
+ >>> print registry.registered([IRequire2], IProvide1)
21
- >>> print registry.registered([IR2], IP2)
+ >>> print registry.registered([IRequire2], IProvide2)
None
-In the last example, None was returned because nothing was registered
+In the last example, ``None`` was returned because nothing was registered
exactly for the given interfaces.
lookup1
@@ -179,9 +179,9 @@ version of lookup that takes a single required interface:
.. doctest::
- >>> registry.lookup1(IR2, IP1, '')
+ >>> registry.lookup1(IRequire2, IProvide1, '')
21
- >>> registry.lookup1(IR2, IP1)
+ >>> registry.lookup1(IRequire2, IProvide1)
21
Actual Adaptation
@@ -202,19 +202,19 @@ factories:
... zope.interface.implements(IR)
>>> class Y:
- ... zope.interface.implements(IP1)
+ ... zope.interface.implements(IProvide1)
... def __init__(self, context):
... self.context = context
- >>> registry.register([IR], IP1, '', Y)
+ >>> registry.register([IR], IProvide1, '', Y)
In this case, we registered a class as the factory. Now we can call
-`queryAdapter` to get the adapted object:
+``queryAdapter`` to get the adapted object:
.. doctest::
>>> x = X()
- >>> y = registry.queryAdapter(x, IP1)
+ >>> y = registry.queryAdapter(x, IProvide1)
>>> y.__class__.__name__
'Y'
>>> y.context is x
@@ -227,14 +227,14 @@ We can register and lookup by name too:
>>> class Y2(Y):
... pass
- >>> registry.register([IR], IP1, 'bob', Y2)
- >>> y = registry.queryAdapter(x, IP1, 'bob')
+ >>> registry.register([IR], IProvide1, 'bob', Y2)
+ >>> y = registry.queryAdapter(x, IProvide1, 'bob')
>>> y.__class__.__name__
'Y2'
>>> y.context is x
True
-When the adapter factory produces `None`, then this is treated as if no
+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:
@@ -250,33 +250,33 @@ the state of the object being adapted:
... zope.interface.implements(IR)
... name = 'object'
- >>> registry.register([IR], IP1, 'conditional', factory)
+ >>> registry.register([IR], IProvide1, 'conditional', factory)
>>> obj = Object()
- >>> registry.queryAdapter(obj, IP1, 'conditional')
+ >>> registry.queryAdapter(obj, IProvide1, 'conditional')
'adapter'
>>> obj.name = 'no object'
- >>> registry.queryAdapter(obj, IP1, 'conditional') is None
+ >>> registry.queryAdapter(obj, IProvide1, 'conditional') is None
True
- >>> registry.queryAdapter(obj, IP1, 'conditional', 'default')
+ >>> registry.queryAdapter(obj, IProvide1, 'conditional', 'default')
'default'
-An alternate method that provides the same function as `queryAdapter()` is
+An alternate method that provides the same function as ``queryAdapter()`` is
`adapter_hook()`:
.. doctest::
- >>> y = registry.adapter_hook(IP1, x)
+ >>> y = registry.adapter_hook(IProvide1, x)
>>> y.__class__.__name__
'Y'
>>> y.context is x
True
- >>> y = registry.adapter_hook(IP1, x, 'bob')
+ >>> y = registry.adapter_hook(IProvide1, x, 'bob')
>>> y.__class__.__name__
'Y2'
>>> y.context is x
True
-The `adapter_hook()` simply switches the order of the object and
+The ``adapter_hook()`` simply switches the order of the object and
interface arguments. It is used to hook into the interface call
mechanism.
@@ -285,11 +285,11 @@ Default Adapters
----------------
Sometimes, you want to provide an adapter that will adapt anything.
-For that, provide None as the required interface:
+For that, provide ``None`` as the required interface:
.. doctest::
- >>> registry.register([None], IP1, '', 1)
+ >>> registry.register([None], IProvide1, '', 1)
then we can use that adapter for interfaces we don't have specific
adapters for:
@@ -298,14 +298,14 @@ adapters for:
>>> class IQ(zope.interface.Interface):
... pass
- >>> registry.lookup([IQ], IP1, '')
+ >>> registry.lookup([IQ], IProvide1, '')
1
Of course, specific adapters are still used when applicable:
.. doctest::
- >>> registry.lookup([IR2], IP1, '')
+ >>> registry.lookup([IRequire2], IProvide1, '')
21
@@ -317,8 +317,8 @@ same as registering them for a class:
.. doctest::
- >>> registry.register([zope.interface.implementedBy(C2)], IP1, '', 'C21')
- >>> registry.lookup([zope.interface.implementedBy(C2)], IP1, '')
+ >>> registry.register([zope.interface.implementedBy(C2)], IProvide1, '', 'C21')
+ >>> registry.lookup([zope.interface.implementedBy(C2)], IProvide1, '')
'C21'
Dict adapters
@@ -337,15 +337,15 @@ bug. Let's make sure this works now:
Unregistering
-------------
-You can unregister by registering None, rather than an object:
+You can unregister by registering ``None``, rather than an object:
.. doctest::
- >>> registry.register([zope.interface.implementedBy(C2)], IP1, '', None)
- >>> registry.lookup([zope.interface.implementedBy(C2)], IP1, '')
+ >>> registry.register([zope.interface.implementedBy(C2)], IProvide1, '', None)
+ >>> registry.lookup([zope.interface.implementedBy(C2)], IProvide1, '')
21
-Of course, this means that None can't be registered. This is an
+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.
@@ -356,24 +356,24 @@ You can adapt multiple specifications:
.. doctest::
- >>> registry.register([IR1, IQ], IP2, '', '1q2')
- >>> registry.lookup([IR1, IQ], IP2, '')
+ >>> registry.register([IRequire1, IQ], IProvide2, '', '1q2')
+ >>> registry.lookup([IRequire1, IQ], IProvide2, '')
'1q2'
- >>> registry.lookup([IR2, IQ], IP1, '')
+ >>> registry.lookup([IRequire2, IQ], IProvide1, '')
'1q2'
>>> class IS(zope.interface.Interface):
... pass
- >>> registry.lookup([IR2, IS], IP1, '')
+ >>> registry.lookup([IRequire2, IS], IProvide1, '')
>>> class IQ2(IQ):
... pass
- >>> registry.lookup([IR2, IQ2], IP1, '')
+ >>> registry.lookup([IRequire2, IQ2], IProvide1, '')
'1q2'
- >>> registry.register([IR1, IQ2], IP2, '', '1q22')
- >>> registry.lookup([IR2, IQ2], IP1, '')
+ >>> registry.register([IRequire1, IQ2], IProvide2, '', '1q22')
+ >>> registry.lookup([IRequire2, IQ2], IProvide1, '')
'1q22'
Multi-adaptation
@@ -398,7 +398,7 @@ As with single adapters, we register a factory, which is often a class:
... self.x, self.q = x, q
>>> registry.register([IR, IQ], IM, '', M)
-And then we can call `queryMultiAdapter` to compute an adapter:
+And then we can call ``queryMultiAdapter`` to compute an adapter:
.. doctest::
@@ -426,25 +426,25 @@ Default Adapters
----------------
As with single adapters, you can define default adapters by specifying
-None for the *first* specification:
+``None`` for the *first* specification:
.. doctest::
- >>> registry.register([None, IQ], IP2, '', 'q2')
- >>> registry.lookup([IS, IQ], IP2, '')
+ >>> registry.register([None, IQ], IProvide2, '', 'q2')
+ >>> registry.lookup([IS, IQ], IProvide2, '')
'q2'
Null Adapters
=============
-You can also adapt no specification:
+You can also adapt **no** specification:
.. doctest::
- >>> registry.register([], IP2, '', 2)
- >>> registry.lookup([], IP2, '')
+ >>> registry.register([], IProvide2, '', 2)
+ >>> registry.lookup([], IProvide2, '')
2
- >>> registry.lookup([], IP1, '')
+ >>> registry.lookup([], IProvide1, '')
2
Listing named adapters
@@ -455,7 +455,7 @@ adapters for given interfaces:
.. doctest::
- >>> adapters = list(registry.lookupAll([IR1], IP1))
+ >>> adapters = list(registry.lookupAll([IRequire1], IProvide1))
>>> adapters.sort()
>>> assert adapters == [(u'', 11), (u'bob', "Bob's 12")]
@@ -463,8 +463,8 @@ This works for multi-adapters too:
.. doctest::
- >>> registry.register([IR1, IQ2], IP2, 'bob', '1q2 for bob')
- >>> adapters = list(registry.lookupAll([IR2, IQ2], IP1))
+ >>> registry.register([IRequire1, IQ2], IProvide2, 'bob', '1q2 for bob')
+ >>> adapters = list(registry.lookupAll([IRequire2, IQ2], IProvide1))
>>> adapters.sort()
>>> assert adapters == [(u'', '1q22'), (u'bob', '1q2 for bob')]
@@ -472,24 +472,24 @@ And even null adapters:
.. doctest::
- >>> registry.register([], IP2, 'bob', 3)
- >>> adapters = list(registry.lookupAll([], IP1))
+ >>> registry.register([], IProvide2, 'bob', 3)
+ >>> adapters = list(registry.lookupAll([], IProvide1))
>>> adapters.sort()
>>> assert adapters == [(u'', 2), (u'bob', 3)]
Subscriptions
=============
-Normally, we want to look up an object that most-closely matches a
+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
+match some specification. We use *subscriptions* for this. We
subscribe objects against specifications and then later find all of
the subscribed objects:
.. doctest::
- >>> registry.subscribe([IR1], IP2, 'sub12 1')
- >>> registry.subscriptions([IR1], IP2)
+ >>> registry.subscribe([IRequire1], IProvide2, 'sub12 1')
+ >>> registry.subscriptions([IRequire1], IProvide2)
['sub12 1']
Note that, unlike regular adapters, subscriptions are unnamed.
@@ -498,19 +498,19 @@ You can have multiple subscribers for the same specification:
.. doctest::
- >>> registry.subscribe([IR1], IP2, 'sub12 2')
- >>> registry.subscriptions([IR1], IP2)
+ >>> registry.subscribe([IRequire1], IProvide2, 'sub12 2')
+ >>> registry.subscriptions([IRequire1], IProvide2)
['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:
+You can register subscribers for all specifications using ``None``:
.. doctest::
- >>> registry.subscribe([None], IP1, 'sub_1')
- >>> registry.subscriptions([IR2], IP1)
+ >>> registry.subscribe([None], IProvide1, 'sub_1')
+ >>> registry.subscriptions([IRequire2], IProvide1)
['sub_1', 'sub12 1', 'sub12 2']
Note that the new subscriber is returned first. Subscribers defined
@@ -521,71 +521,71 @@ Subscriptions may be combined over multiple compatible specifications:
.. doctest::
- >>> registry.subscriptions([IR2], IP1)
+ >>> registry.subscriptions([IRequire2], IProvide1)
['sub_1', 'sub12 1', 'sub12 2']
- >>> registry.subscribe([IR1], IP1, 'sub11')
- >>> registry.subscriptions([IR2], IP1)
+ >>> registry.subscribe([IRequire1], IProvide1, 'sub11')
+ >>> registry.subscriptions([IRequire2], IProvide1)
['sub_1', 'sub12 1', 'sub12 2', 'sub11']
- >>> registry.subscribe([IR2], IP2, 'sub22')
- >>> registry.subscriptions([IR2], IP1)
+ >>> registry.subscribe([IRequire2], IProvide2, 'sub22')
+ >>> registry.subscriptions([IRequire2], IProvide1)
['sub_1', 'sub12 1', 'sub12 2', 'sub11', 'sub22']
- >>> registry.subscriptions([IR2], IP2)
+ >>> registry.subscriptions([IRequire2], IProvide2)
['sub12 1', 'sub12 2', 'sub22']
Subscriptions can be on multiple specifications:
.. doctest::
- >>> registry.subscribe([IR1, IQ], IP2, 'sub1q2')
- >>> registry.subscriptions([IR1, IQ], IP2)
+ >>> registry.subscribe([IRequire1, IQ], IProvide2, 'sub1q2')
+ >>> registry.subscriptions([IRequire1, IQ], IProvide2)
['sub1q2']
As with single subscriptions and non-subscription adapters, you can
-specify None for the first required interface, to specify a default:
+specify ``None`` for the first required interface, to specify a default:
.. doctest::
- >>> registry.subscribe([None, IQ], IP2, 'sub_q2')
- >>> registry.subscriptions([IS, IQ], IP2)
+ >>> registry.subscribe([None, IQ], IProvide2, 'sub_q2')
+ >>> registry.subscriptions([IS, IQ], IProvide2)
['sub_q2']
- >>> registry.subscriptions([IR1, IQ], IP2)
+ >>> registry.subscriptions([IRequire1, IQ], IProvide2)
['sub_q2', 'sub1q2']
-You can have subscriptions that are indepenent of any specifications:
+You can have subscriptions that are independent of any specifications:
.. doctest::
- >>> list(registry.subscriptions([], IP1))
+ >>> list(registry.subscriptions([], IProvide1))
[]
- >>> registry.subscribe([], IP2, 'sub2')
- >>> registry.subscriptions([], IP1)
+ >>> registry.subscribe([], IProvide2, 'sub2')
+ >>> registry.subscriptions([], IProvide1)
['sub2']
- >>> registry.subscribe([], IP1, 'sub1')
- >>> registry.subscriptions([], IP1)
+ >>> registry.subscribe([], IProvide1, 'sub1')
+ >>> registry.subscriptions([], IProvide1)
['sub2', 'sub1']
- >>> registry.subscriptions([], IP2)
+ >>> registry.subscriptions([], IProvide2)
['sub2']
Unregistering subscribers
-------------------------
We can unregister subscribers. When unregistering a subscriber, we
-can unregister a specific subscriber:
+can unregister a *specific* subscriber:
.. doctest::
- >>> registry.unsubscribe([IR1], IP1, 'sub11')
- >>> registry.subscriptions([IR1], IP1)
+ >>> registry.unsubscribe([IRequire1], IProvide1, 'sub11')
+ >>> registry.subscriptions([IRequire1], IProvide1)
['sub_1', 'sub12 1', 'sub12 2']
-If we don't specify a value, then all subscribers matching the given
+If we don't specify a value, then *all* subscribers matching the given
interfaces will be unsubscribed:
.. doctest::
- >>> registry.unsubscribe([IR1], IP2)
- >>> registry.subscriptions([IR1], IP1)
+ >>> registry.unsubscribe([IRequire1], IProvide2)
+ >>> registry.subscriptions([IRequire1], IProvide1)
['sub_1']
@@ -611,7 +611,7 @@ example of multiple-object subscribers:
>>> [(s.x is x and s.q is q) for s in subscribers]
[True, True]
-adapter factory subcribers can't return None values:
+Adapter factory subscribers can't return ``None`` values:
.. doctest::
@@ -627,16 +627,16 @@ 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
+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:
+To register a handler, simply provide ``None`` as the provided interface:
.. doctest::
>>> def handler(event):
... print 'handler', event
- >>> registry.subscribe([IR1], None, handler)
- >>> registry.subscriptions([IR1], None) == [handler]
+ >>> registry.subscribe([IRequire1], None, handler)
+ >>> registry.subscriptions([IRequire1], None) == [handler]
True