The current component registry

There can be any number of component registries in an application. One of them is the global component registry, and there is also the concept of a currently used component registry. Component registries other than the global one are associated with objects called sites. The zope.component.hooks module provides an API to set and access the current site as well as manipulate the adapter hook associated with it.

As long as we haven’t set a site, none is being considered current:

We can also ask for the current component registry (aka site manager historically); it will return the global one if no current site is set:

Let’s set a site now. A site has to be an object that provides the getSiteManager method, which is specified by zope.component.interfaces.IPossibleSite:

After this, the newly set site is considered the currently active one:

If we set another site, that one will be considered current:

Finally we can unset the site and the global component registry is used again:

Context manager

There also is a context manager for setting the site, which is especially useful when writing tests:

The site is properly restored even if the body of the with statement raises an exception: