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
|
Interface Registration APIs
===========================
.. testsetup::
from zope.component.testing import setUp
setUp()
Registering Interfaces as Utilities
-----------------------------------
.. autofunction:: zope.component.interface.provideInterface
We can register a given interface with the global site manager as a utility.
First, declare a new interface, which itself provides only the core API,
:class:`zope.interface.interfaces.IInterface`:
.. doctest::
>>> from zope.interface import Interface
>>> from zope.interface.interfaces import IInterface
>>> from zope.component.tests.examples import ITestType
>>> from zope.component import getGlobalSiteManager
>>> gsm = getGlobalSiteManager()
>>> class IDemo(Interface):
... pass
>>> IInterface.providedBy(IDemo)
True
>>> ITestType.providedBy(IDemo)
False
>>> list(gsm.getUtilitiesFor(ITestType))
[]
Now, register ``IDemo`` as providing ``ITestType``
.. doctest::
>>> from zope.component.interface import provideInterface
>>> provideInterface('', IDemo, ITestType)
>>> ITestType.providedBy(IDemo)
True
>>> interfaces = list(gsm.getUtilitiesFor(ITestType))
>>> [iface.__name__ for (name, iface) in interfaces]
['IDemo']
We can register ``IDemo`` as providing more than one interface:
.. doctest::
>>> class IOtherType(IInterface):
... pass
>>> provideInterface('', IDemo, IOtherType)
>>> ITestType.providedBy(IDemo)
True
>>> IOtherType.providedBy(IDemo)
True
>>> interfaces = list(gsm.getUtilitiesFor(ITestType))
>>> [iface.__name__ for (name, iface) in interfaces]
['IDemo']
>>> interfaces = list(gsm.getUtilitiesFor(IOtherType))
>>> [iface.__name__ for (name, iface) in interfaces]
['IDemo']
.. testcleanup::
from zope.component.testing import tearDown
tearDown()
.. autofunction:: zope.component.interface.getInterface
.. doctest::
>>> from zope.interface import Interface
>>> from zope.component.interface import getInterface
>>> from zope.component.tests.examples import ITestType
>>> from zope.component.tests.examples import IGI
>>> IInterface.providedBy(IGI)
True
>>> ITestType.providedBy(IGI)
False
>>> getInterface(None, 'zope.component.tests.examples.IGI')
Traceback (most recent call last):
...
ComponentLookupError: zope.component.tests.examples.interface.IGI
>>> provideInterface('', IGI, ITestType)
>>> ITestType.providedBy(IGI)
True
>>> iface = getInterface(None,
... 'zope.component.tests.examples.IGI')
>>> iface.__name__
'IGI'
.. testcleanup::
from zope.component.testing import tearDown
tearDown()
.. autofunction:: zope.component.interface.queryInterface
.. doctest::
>>> from zope.interface import Interface
>>> from zope.interface.interfaces import IInterface
>>> from zope.component.interface import queryInterface
>>> from zope.component.tests.examples import ITestType
>>> from zope.component.tests.examples import IQI
>>> IInterface.providedBy(IQI)
True
>>> ITestType.providedBy(IQI)
False
>>> queryInterface('zope.component.tests.examples.IQI') is None
True
>>> provideInterface('', IQI, ITestType)
>>> ITestType.providedBy(IQI)
True
>>> iface = queryInterface('zope.component.tests.examples.IQI')
>>> iface.__name__
'IQI'
.. testcleanup::
from zope.component.testing import tearDown
tearDown()
.. autofunction:: zope.component.interface.searchInterface
.. doctest::
>>> from zope.interface import Interface
>>> from zope.interface.interfaces import IInterface
>>> from zope.component.interface import searchInterface
>>> from zope.component.tests.examples import ITestType
>>> from zope.component.tests.examples import ISI
>>> IInterface.providedBy(ISI)
True
>>> ITestType.providedBy(ISI)
False
>>> searchInterface(None, 'zope.component.tests.examples.ISI')
[]
>>> provideInterface('', ISI, ITestType)
>>> ITestType.providedBy(ISI)
True
>>> searchInterface(None, 'zope.component.tests.examples.ISI') == [ISI]
True
.. testcleanup::
from zope.component.testing import tearDown
tearDown()
.. autofunction:: zope.component.interface.searchInterfaceIds
.. doctest::
>>> from zope.interface import Interface
>>> from zope.interface.interfaces import IInterface
>>> from zope.component.interface import searchInterfaceIds
>>> from zope.component.tests.examples import ITestType
>>> from zope.component.tests.examples import ISII
>>> IInterface.providedBy(ISII)
True
>>> ITestType.providedBy(ISII)
False
>>> searchInterfaceIds(None, 'zope.component.tests.examples.ISII')
[]
>>> provideInterface('', ISII, ITestType)
>>> ITestType.providedBy(ISII)
True
>>> [str(x) for x in searchInterfaceIds(None, 'zope.component.tests.examples.ISII')]
['zope.component.tests.examples.ISII']
.. testcleanup::
from zope.component.testing import tearDown
tearDown()
|