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
|
===========================
Utility Registration APIs
===========================
.. autofunction:: zope.component.provideUtility
.. autofunction:: zope.component.getUtility
.. autofunction:: zope.component.queryUtility
Utilities are components that simply provide an interface. They are
instantiated at the time or before they are registered. Here we test the
simple query interface.
Before we register any utility, there is no utility available, of
course. The pure instatiation of an object does not make it a utility. If
you do not specify a default, you get a `ComponentLookupError`.
.. testsetup::
from zope.component.testing import setUp
setUp()
.. doctest::
>>> from zope.component import getUtility
>>> from zope.component import queryUtility
>>> from zope.component.tests.examples import I1
>>> getUtility(I1) #doctest: +NORMALIZE_WHITESPACE
Traceback (most recent call last):
...
ComponentLookupError: \
(<InterfaceClass zope.component.tests.examples.I1>, '')
Otherwise, you get the default:
.. doctest::
>>> queryUtility(I1, default='<default>')
'<default>'
Now we declare `ob` to be the utility providing `I1`:
.. doctest::
>>> ob = object()
>>> from zope.component import getGlobalSiteManager
>>> getGlobalSiteManager().registerUtility(ob, I1)
Now the component is available:
.. doctest::
>>> getUtility(I1) is ob
True
>>> queryUtility(I1) is ob
True
Named Utilities
===============
Registering a utility without a name does not mean that it is available
when looking for the utility with a name:
.. doctest::
>>> getUtility(I1, name='foo')
Traceback (most recent call last):
...
ComponentLookupError:
(<InterfaceClass zope.component.tests.examples.I1>, 'foo')
>>> queryUtility(I1, name='foo', default='<default>')
'<default>'
Registering the utility under the correct name makes it available:
.. doctest::
>>> ob2 = object()
>>> getGlobalSiteManager().registerUtility(ob2, I1, name='foo')
>>> getUtility(I1, 'foo') is ob2
True
>>> queryUtility(I1, 'foo') is ob2
True
Querying Multiple Utilities
===========================
.. autofunction:: zope.component.getUtilitiesFor
.. autofunction:: zope.component.getAllUtilitiesRegisteredFor
Sometimes it may be useful to query all utilities, both anonymous and named
for a given interface. The :func:`~zope.component.getUtilitiesFor` API
returns a sequence of ``(name, utility)`` tuples, where ``name`` is the
empty string for the anonymous utility:
.. doctest::
>>> from zope.component import getUtilitiesFor
>>> tuples = list(getUtilitiesFor(I1))
>>> len(tuples)
2
>>> ('', ob) in tuples
True
>>> ('foo', ob2) in tuples
True
The :func:`~zope.component.getAllUtilitiesRegisteredFor` API returns
utilities that have been registered for a particular interface. Utilities
providing a derived interface are also listed.
.. doctest::
>>> from zope.interface import implementer
>>> from zope.component.tests.examples import Comp
>>> from zope.component.tests.examples import I2
>>> from zope.component.tests.examples import Ob
>>> class I11(I1):
... pass
>>> @implementer(I11)
... class Ob11(Ob):
... pass
>>> ob11 = Ob11()
>>> ob_bob = Ob()
Now we register the new utilities:
.. doctest::
>>> from zope.component import getGlobalSiteManager
>>> gsm = getGlobalSiteManager()
>>> gsm.registerUtility(ob, I1)
>>> gsm.registerUtility(ob11, I11)
>>> gsm.registerUtility(ob_bob, I1, name='bob')
>>> gsm.registerUtility(Comp(2), I2)
We can now get all the utilities that provide interface `I1`:
.. doctest::
>>> from zope.component import getAllUtilitiesRegisteredFor
>>> uts = list(getAllUtilitiesRegisteredFor(I1))
>>> len(uts)
4
>>> ob in uts
True
>>> ob2 in uts
True
>>> ob_bob in uts
True
>>> ob11 in uts
True
Note that `getAllUtilitiesRegisteredFor()` does not return the names of
the utilities.
Delegated Utility Lookup
========================
.. autofunction:: zope.component.getNextUtility
.. autofunction:: zope.component.queryNextUtility
It is common for a utility to delegate its answer to a utility
providing the same interface in one of the component registry's
bases. Let's first create a global utility:
.. doctest::
>>> from zope.interface import Interface
>>> from zope.interface import implementer
>>> class IMyUtility(Interface):
... pass
>>> from zope.component.tests.examples import ConformsToIComponentLookup
>>> @implementer(IMyUtility)
... class MyUtility(ConformsToIComponentLookup):
... def __init__(self, id, sm):
... self.id = id
... self.sitemanager = sm
... def __repr__(self):
... return "%s('%s')" % (self.__class__.__name__, self.id)
>>> gutil = MyUtility('global', gsm)
>>> gsm.registerUtility(gutil, IMyUtility, 'myutil')
Now, let's create two registries and set up the bases hierarchy:
.. doctest::
>>> from zope.interface.registry import Components
>>> sm1 = Components('sm1', bases=(gsm, ))
>>> sm1_1 = Components('sm1_1', bases=(sm1, ))
Now we create two utilities and insert them in our folder hierarchy:
.. doctest::
>>> from zope.interface.interfaces import IComponentLookup
>>> util1 = MyUtility('one', sm1)
>>> sm1.registerUtility(util1, IMyUtility, 'myutil')
>>> IComponentLookup(util1) is sm1
True
>>> util1_1 = MyUtility('one-one', sm1_1)
>>> sm1_1.registerUtility(util1_1, IMyUtility, 'myutil')
>>> IComponentLookup(util1_1) is sm1_1
True
Now, if we ask `util1_1` for its next available utility we get the
``one`` utility:
.. doctest::
>>> from zope.component import getNextUtility
>>> getNextUtility(util1_1, IMyUtility, 'myutil')
MyUtility('one')
Next we ask `util1` for its next utility and we should get the global version:
.. doctest::
>>> getNextUtility(util1, IMyUtility, 'myutil')
MyUtility('global')
However, if we ask the global utility for the next one, an error is raised
.. doctest::
>>> getNextUtility(gutil, IMyUtility,
... 'myutil') #doctest: +NORMALIZE_WHITESPACE
Traceback (most recent call last):
...
ComponentLookupError:
No more utilities for <InterfaceClass zope.component.tests.examples.IMyUtility>,
'myutil' have been found.
You can also use `queryNextUtility` and specify a default:
.. doctest::
>>> from zope.component import queryNextUtility
>>> queryNextUtility(gutil, IMyUtility, 'myutil', 'default')
'default'
Let's now ensure that the function also works with multiple registries. First
we create another base registry:
.. doctest::
>>> myregistry = Components()
We now set up another utility into that registry:
.. doctest::
>>> custom_util = MyUtility('my_custom_util', myregistry)
>>> myregistry.registerUtility(custom_util, IMyUtility, 'my_custom_util')
We add it as a base to the local site manager:
.. doctest::
>>> sm1.__bases__ = (myregistry,) + sm1.__bases__
Both the ``myregistry`` and global utilities should be available:
.. doctest::
>>> queryNextUtility(sm1, IMyUtility, 'my_custom_util')
MyUtility('my_custom_util')
>>> queryNextUtility(sm1, IMyUtility, 'myutil')
MyUtility('global')
Note, if the context cannot be converted to a site manager, the default is
retruned:
.. doctest::
>>> queryNextUtility(object(), IMyUtility, 'myutil', 'default')
'default'
.. testcleanup::
from zope.component.testing import tearDown
tearDown()
|