blob: 7b776999a68af06ff270dda1da72d81ffdb1a5c7 (
plain)
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
|
Site Manager APIs
=================
.. autofunction:: zope.component.getGlobalSiteManager
The API returns the module-scope global registry:
.. doctest::
>>> 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
:class:`~zope.component.interfaces.IComponentLookup` interface:
.. doctest::
>>> IComponentLookup.providedBy(gsm)
True
The same registry is returned each time we call the function:
.. doctest::
>>> getGlobalSiteManager() is gsm
True
.. autofunction:: zope.component.getSiteManager(context=None)
We don't know anything about the default service manager, except that it
is an `IComponentLookup`.
.. doctest::
>>> 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`.
.. doctest::
>>> 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:
.. doctest::
>>> sitemanager = object()
Now create a context that knows how to adapt to our newly created site
manager.
.. doctest::
>>> from zope.component.tests.examples import ConformsToIComponentLookup
>>> context = ConformsToIComponentLookup(sitemanager)
Now make sure that the `getSiteManager()` API call returns the correct
site manager.
.. doctest::
>>> getSiteManager(context) is sitemanager
True
Using a context that is not adaptable to `IComponentLookup` should fail.
.. doctest::
>>> getSiteManager(sitemanager)
Traceback (most recent call last):
...
ComponentLookupError: ('Could not adapt', <instance Ob>,
<InterfaceClass zope...interfaces.IComponentLookup>)
|