Utility Registration APIs¶
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.
Otherwise, you get the default:
Now we declare ob to be the utility providing I1:
Now the component is available:
Named Utilities¶
Registering a utility without a name does not mean that it is available when looking for the utility with a name:
Registering the utility under the correct name makes it available:
Querying Multiple Utilities¶
Sometimes it may be useful to query all utilities, both anonymous and named
for a given interface. The getUtilitiesFor()
API
returns a sequence of (name, utility)
tuples, where name
is the
empty string for the anonymous utility:
The getAllUtilitiesRegisteredFor()
API returns
utilities that have been registered for a particular interface. Utilities
providing a derived interface are also listed.
Now we register the new utilities:
We can now get all the utilities that provide interface I1:
Note that getAllUtilitiesRegisteredFor() does not return the names of the utilities.
Delegated Utility Lookup¶
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:
Now, let’s create two registries and set up the bases hierarchy:
Now we create two utilities and insert them in our folder hierarchy:
Now, if we ask util1_1 for its next available utility we get the
one
utility:
Next we ask util1 for its next utility and we should get the global version:
However, if we ask the global utility for the next one, an error is raised
You can also use queryNextUtility and specify a default:
Let’s now ensure that the function also works with multiple registries. First we create another base registry:
We now set up another utility into that registry:
We add it as a base to the local site manager:
Both the myregistry
and global utilities should be available:
Note, if the context cannot be converted to a site manager, the default is retruned: