zope.component: Module-level functions

This document provides a summary of the APIs available directly from zope.component. For more details, see the remaining documentation.

Zope 3 Component Architecture

This module provides an implementation of IComponentArchitecture, using the current site.

This module also provides an implementation of IComponentRegistrationConvenience using the global site manager.

class zope.component.adapter(*interfaces)[source]

Bases: object

Decorator that declares that the decorated object adapts the given interfaces.

This is commonly used in conjunction with zope.interface.implementer to declare what adapting the interfaces will provide.

zope.component.queryUtility(interface, name='', default=None, context=None)[source]

Look for the utility that provides interface

Returns the nearest utility to the context that implements the specified interface. If one is not found, returns default.

See also

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

zope.component.getAdapterInContext(object, interface, context)[source]

Get a special adapter to an interface for an object

Note

This method should only be used if a custom context needs to be provided to provide custom component lookup. Otherwise, call the interface, as in:

interface(object)

Returns an adapter that can adapt object to interface. If a matching adapter cannot be found, raises ComponentLookupError.

If the object has a __conform__ method, this method will be called with the requested interface. If the method returns a non-None value, that value will be returned. Otherwise, if the object already implements the interface, the object will be returned.

See also

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

zope.component.handle(*objects)[source]

Call all of the handlers for the given objects

Handlers are subscription adapter factories that don’t produce anything. They do all of their work when called. Handlers are typically used to handle events.

See also

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

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.

zope.component.getAdapters(objects, provided, context=None)[source]

Look for all matching adapters to a provided interface for objects

Return a list of adapters that match. If an adapter is named, only the most specific adapter of a given name is returned.

See also

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

zope.component.queryAdapterInContext(object, interface, context, default=None)[source]

Look for a special adapter to an interface for an object

Note

This method should only be used if a custom context needs to be provided to provide custom component lookup. Otherwise, call the interface, as in:

interface(object, default)

Returns an adapter that can adapt object to interface. If a matching adapter cannot be found, returns the default.

If the object has a __conform__ method, this method will be called with the requested interface. If the method returns a non-None value, that value will be returned. Otherwise, if the object already implements the interface, the object will be returned.

See also

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

zope.component.queryNextUtility(context, interface, name='', default=None)[source]

Query for the next available utility.

Find the next available utility providing interface and having the specified name. If no utility was found, return the specified default value.

See also

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

zope.component.getUtility(interface, name='', context=None)[source]

Get the utility that provides interface

Returns the nearest utility to the context that implements the specified interface. If one is not found, raises ComponentLookupError.

See also

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

zope.component.getUtilitiesFor(interface, context=None)[source]

Return the utilities that provide an interface

An iterable of utility name-value pairs is returned.

See also

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

zope.component.subscribers(objects, interface, context=None)[source]

Get subscribers

Subscribers are returned that provide the provided interface and that depend on and are computed from the sequence of required objects.

See also

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

zope.component.getAdapter(object, interface=<InterfaceClass zope.interface.Interface>, name=u'', context=None)[source]

Get a named adapter to an interface for an object

Returns an adapter that can adapt object to interface. If a matching adapter cannot be found, raises ComponentLookupError.

See also

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

zope.component.getFactoriesFor(interface, context=None)[source]

Return a tuple (name, factory) of registered factories that create objects which implement the given interface.

See also

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

zope.component.queryMultiAdapter(objects, interface=<InterfaceClass zope.interface.Interface>, name=u'', default=None, context=None)[source]

Look for a multi-adapter to an interface for objects

Returns a multi-adapter that can adapt objects to interface. If a matching adapter cannot be found, returns the default.

The name consisting of an empty string is reserved for unnamed adapters. The unnamed adapter methods will often call the named adapter methods with an empty string for a name.

See also

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

zope.component.getNextUtility(context, interface, name='')[source]

Get the next available utility.

If no utility was found, a ComponentLookupError is raised.

See also

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

zope.component.getFactoryInterfaces(name, context=None)[source]

Get interfaces implemented by a factory

Finds the factory of the given name that is nearest to the context, and returns the interface or interface tuple that object instances created by the named factory will implement.

See also

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

zope.component.getMultiAdapter(objects, interface=<InterfaceClass zope.interface.Interface>, name=u'', context=None)[source]

Look for a multi-adapter to an interface for an objects

Returns a multi-adapter that can adapt objects to interface. If a matching adapter cannot be found, raises ComponentLookupError.

The name consisting of an empty string is reserved for unnamed adapters. The unnamed adapter methods will often call the named adapter methods with an empty string for a name.

See also

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

zope.component.getAllUtilitiesRegisteredFor(interface, context=None)[source]

Return all registered utilities for an interface

This includes overridden utilities.

An iterable of utility instances is returned. No names are returned.

See also

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

zope.component.createObject(__factory_name, *args, **kwargs)[source]

Create an object using a factory

Finds the named factory in the current site and calls it with the given arguments. If a matching factory cannot be found raises ComponentLookupError. Returns the created object.

A context keyword argument can be provided to cause the factory to be looked up in a location other than the current site. (Of course, this means that it is impossible to pass a keyword argument named “context” to the factory.

See also

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

zope.component.queryAdapter(object, interface=<InterfaceClass zope.interface.Interface>, name=u'', default=None, context=None)[source]

Look for a named adapter to an interface for an object

Returns an adapter that can adapt object to interface. If a matching adapter cannot be found, returns the default.

See also

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

zope.component.provideAdapter(factory, adapts=None, provides=None, name=u'')[source]

Register an adapter globally

An adapter is registered to provide an interface with a name for some number of object types. If a factory implements only one interface, then the provides argument can be omitted and the provided interface will be used. (In this case, a provides argument can still be provided to provide a less specific interface.)

If the factory has an adapts declaration, then the adapts argument can be omitted and the declaration will be used. (An adapts argument can be provided to override the declaration.)

See also

Function provideAdapter for notes, and IComponentRegistrationConvenience for the defining interface.

zope.component.provideUtility(component, provides=None, name=u'')[source]

Register a utility globally

A utility is registered to provide an interface with a name. If a component provides only one interface, then the provides argument can be omitted and the provided interface will be used. (In this case, provides argument can still be provided to provide a less specific interface.)

See also

Function provideUtility for notes, and IComponentRegistrationConvenience for the defining interface.

zope.component.provideSubscriptionAdapter(factory, adapts=None, provides=None)[source]

Register a subscription adapter

A subscription adapter is registered to provide an interface for some number of object types. If a factory implements only one interface, then the provides argument can be omitted and the provided interface will be used. (In this case, a provides argument can still be provided to provide a less specific interface.)

If the factory has an adapts declaration, then the adapts argument can be omitted and the declaration will be used. (An adapts argument can be provided to override the declaration.)

See also

Function provideSubscriptionAdapter for notes, and IComponentRegistrationConvenience for the defining interface.

zope.component.provideHandler(factory, adapts=None)[source]

Register a handler

Handlers are subscription adapter factories that don’t produce anything. They do all of their work when called. Handlers are typically used to handle events.

If the handler has an adapts declaration, then the adapts argument can be omitted and the declaration will be used. (An adapts argument can be provided to override the declaration.)

See also

Function provideHandler for notes, and IComponentRegistrationConvenience for the defining interface.

zope.component.adaptedBy(ob)[source]

Return the interfaces that ob will adapt, as declared by adapter.