Site Manager APIs

zope.component.getGlobalSiteManager()[source]

Return the global site manager.

This function should never fail and always return an object that provides zope.interface.interfaces.IComponents.

See also

Function getGlobalSiteManager for notes, and IComponentArchitecture for the defining interface.

The API returns the module-scope global registry:

>>> from zope.interface.interfaces import IComponentLookup
>>> from zope.component.globalregistry import base
>>> from zope.component import getGlobalSiteManager
>>> gsm = getGlobalSiteManager()
>>> gsm is base
True

The registry implements the IComponentLookup interface:

>>> IComponentLookup.providedBy(gsm)
True

The same registry is returned each time we call the function:

>>> getGlobalSiteManager() is gsm
True
zope.component.getSiteManager(context=None)

Get the nearest site manager in the given context.

If context is None, return the global site manager.

If the context is not None, it is expected that an adapter from the context to zope.interface.interfaces.IComponentLookup can be found. If no adapter is found, a ComponentLookupError is raised.

See also

Function getSiteManager for notes, and IComponentArchitecture for the defining interface.

We don’t know anything about the default service manager, except that it is an IComponentLookup.

>>> from zope.component import getSiteManager
>>> from zope.interface.interfaces import IComponentLookup
>>> IComponentLookup.providedBy(getSiteManager())
True

Calling getSiteManager() with no args is equivalent to calling it with a context of None.

>>> getSiteManager() is getSiteManager(None)
True

If the context passed to getSiteManager() is not None, it is adapted to IComponentLookup and this adapter returned. So, we create a context that can be adapted to IComponentLookup using the __conform__ API.

Let’s create the simplest stub-implementation of a site manager possible:

>>> sitemanager = object()

Now create a context that knows how to adapt to our newly created site manager.

>>> from zope.component.tests.examples import ConformsToIComponentLookup
>>> context = ConformsToIComponentLookup(sitemanager)

Now make sure that the getSiteManager() API call returns the correct site manager.

>>> getSiteManager(context) is sitemanager
True

Using a context that is not adaptable to IComponentLookup should fail.

>>> getSiteManager(sitemanager)
Traceback (most recent call last):
...
ComponentLookupError: ('Could not adapt', <instance Ob>,
<InterfaceClass zope...interfaces.IComponentLookup>)