Interface Registration APIs — zope.configuration 4.0 documentation

### Navigation

- index
- modules |
- next |
- previous |
- zope.configuration 4.0 documentation »
- `zope.component` API Reference »

# Interface Registration APIs

## Registering Interfaces as Utilities

`zope.component.interface.` `provideInterface`(id, interface, iface_type=None, info='')

  Mark ‘interface’ as a named utilty providing ‘iface_type’.

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, `zope.interface.interfaces.IInterface`:

    >>> 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`

    >>> 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:

    >>> 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']

`zope.component.interface.` `getInterface`(context, id)

  Return interface or raise ComponentLookupError

    >>> 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'

`zope.component.interface.` `queryInterface`(id, default=None)

  Return an interface or `None`

    >>> 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'

`zope.component.interface.` `searchInterface`(context, search_string=None, base=None)

  Interfaces search

    >>> 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

`zope.component.interface.` `searchInterfaceIds`(context, search_string=None, base=None)

  Interfaces search

    >>> 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
    >>> searchInterfaceIds(None, 'zope.component.tests.examples.ISII')
    [u'zope.component.tests.examples.ISII']

### Navigation

- index
- modules |
- next |
- previous |
- zope.configuration 4.0 documentation »
- `zope.component` API Reference »