[Zope3-checkins] SVN: Zope3/branches/jim-adapter/src/zope/ renamed
zope.component.components to zope.component.registry
Jim Fulton
jim at zope.com
Sun Mar 12 12:52:23 EST 2006
Log message for revision 65920:
renamed zope.component.components to zope.component.registry
Changed:
U Zope3/branches/jim-adapter/src/zope/app/apidoc/component.py
U Zope3/branches/jim-adapter/src/zope/app/apidoc/component.txt
U Zope3/branches/jim-adapter/src/zope/app/apidoc/presentation.py
U Zope3/branches/jim-adapter/src/zope/app/apidoc/presentation.txt
U Zope3/branches/jim-adapter/src/zope/app/apidoc/utilitymodule/utilitymodule.py
U Zope3/branches/jim-adapter/src/zope/app/component/site.py
U Zope3/branches/jim-adapter/src/zope/app/component/tests/test_directives.py
U Zope3/branches/jim-adapter/src/zope/component/bbb/adapter.py
U Zope3/branches/jim-adapter/src/zope/component/bbb/utility.py
D Zope3/branches/jim-adapter/src/zope/component/components.py
D Zope3/branches/jim-adapter/src/zope/component/components.txt
U Zope3/branches/jim-adapter/src/zope/component/globalregistry.py
U Zope3/branches/jim-adapter/src/zope/component/persistentregistry.py
A Zope3/branches/jim-adapter/src/zope/component/registry.py
A Zope3/branches/jim-adapter/src/zope/component/registry.txt
U Zope3/branches/jim-adapter/src/zope/component/site.py
U Zope3/branches/jim-adapter/src/zope/component/tests.py
-=-
Modified: Zope3/branches/jim-adapter/src/zope/app/apidoc/component.py
===================================================================
--- Zope3/branches/jim-adapter/src/zope/app/apidoc/component.py 2006-03-12 14:20:53 UTC (rev 65919)
+++ Zope3/branches/jim-adapter/src/zope/app/apidoc/component.py 2006-03-12 17:52:22 UTC (rev 65920)
@@ -19,13 +19,13 @@
import types
from zope.component.interfaces import IFactory
-from zope.component.components import (
+from zope.component.registry import (
AdapterRegistration,
SubscriptionRegistration,
HandlerRegistration,
)
-from zope.component.components import UtilityRegistration
+from zope.component.registry import UtilityRegistration
from zope.interface import Interface
from zope.interface.interface import InterfaceClass
import zope.interface.declarations
Modified: Zope3/branches/jim-adapter/src/zope/app/apidoc/component.txt
===================================================================
--- Zope3/branches/jim-adapter/src/zope/app/apidoc/component.txt 2006-03-12 14:20:53 UTC (rev 65919)
+++ Zope3/branches/jim-adapter/src/zope/app/apidoc/component.txt 2006-03-12 17:52:22 UTC (rev 65920)
@@ -304,7 +304,7 @@
>>> class MyResult(object):
... implements(IResult)
- >>> from zope.component.components import AdapterRegistration
+ >>> from zope.component.registry import AdapterRegistration
>>> reg = AdapterRegistration((IFoo, IBar), IResult, 'FooToResult',
... MyResult, 'doc info')
@@ -344,7 +344,7 @@
much like adapter registrations, except that they do not have a name. So let's
see how the function handles subscriptions:
- >>> from zope.component.components import HandlerRegistration
+ >>> from zope.component.registry import HandlerRegistration
>>> reg = HandlerRegistration((IFoo, IBar), '', MyResult, 'doc info')
>>> pprint(component.getAdapterInfoDictionary(reg))
Modified: Zope3/branches/jim-adapter/src/zope/app/apidoc/presentation.py
===================================================================
--- Zope3/branches/jim-adapter/src/zope/app/apidoc/presentation.py 2006-03-12 14:20:53 UTC (rev 65919)
+++ Zope3/branches/jim-adapter/src/zope/app/apidoc/presentation.py 2006-03-12 17:52:22 UTC (rev 65920)
@@ -16,7 +16,7 @@
$Id$
"""
from types import ClassType, FunctionType
-from zope.component.components import AdapterRegistration
+from zope.component.registry import AdapterRegistration
from zope.interface import Interface
from zope.app import zapi
Modified: Zope3/branches/jim-adapter/src/zope/app/apidoc/presentation.txt
===================================================================
--- Zope3/branches/jim-adapter/src/zope/app/apidoc/presentation.txt 2006-03-12 14:20:53 UTC (rev 65919)
+++ Zope3/branches/jim-adapter/src/zope/app/apidoc/presentation.txt 2006-03-12 17:52:22 UTC (rev 65920)
@@ -384,7 +384,7 @@
Let's first create a registration:
- >>> from zope.component.components import AdapterRegistration
+ >>> from zope.component.registry import AdapterRegistration
>>> reg = AdapterRegistration((IFile, Interface, IHTTPRequest),
... Interface, 'view.html', Factory, 'reg info')
Modified: Zope3/branches/jim-adapter/src/zope/app/apidoc/utilitymodule/utilitymodule.py
===================================================================
--- Zope3/branches/jim-adapter/src/zope/app/apidoc/utilitymodule/utilitymodule.py 2006-03-12 14:20:53 UTC (rev 65919)
+++ Zope3/branches/jim-adapter/src/zope/app/apidoc/utilitymodule/utilitymodule.py 2006-03-12 17:52:22 UTC (rev 65920)
@@ -19,7 +19,7 @@
import base64, binascii
-from zope.component.components import UtilityRegistration
+from zope.component.registry import UtilityRegistration
from zope.interface import implements
from zope.app import zapi
Modified: Zope3/branches/jim-adapter/src/zope/app/component/site.py
===================================================================
--- Zope3/branches/jim-adapter/src/zope/app/component/site.py 2006-03-12 14:20:53 UTC (rev 65919)
+++ Zope3/branches/jim-adapter/src/zope/app/component/site.py 2006-03-12 17:52:22 UTC (rev 65920)
@@ -27,7 +27,7 @@
import zope.event
import zope.interface
-import zope.component.components
+import zope.component.registry
from zope.component.interfaces import ComponentLookupError
from zope.security.proxy import removeSecurityProxy
@@ -181,7 +181,7 @@
class LocalSiteManager(BTreeContainer,
bbb.site.BBBSiteManager,
- zope.component.components.Components):
+ zope.component.registry.Components):
"""Local Site Manager implementation"""
zope.interface.implements(
interfaces.ILocalSiteManager,
Modified: Zope3/branches/jim-adapter/src/zope/app/component/tests/test_directives.py
===================================================================
--- Zope3/branches/jim-adapter/src/zope/app/component/tests/test_directives.py 2006-03-12 14:20:53 UTC (rev 65919)
+++ Zope3/branches/jim-adapter/src/zope/app/component/tests/test_directives.py 2006-03-12 17:52:22 UTC (rev 65920)
@@ -24,7 +24,7 @@
from zope.testing.doctestunit import DocTestSuite
from zope.component import createObject
from zope.component.interfaces import IDefaultViewName
-from zope.component.components import SubscriptionRegistration
+from zope.component.registry import SubscriptionRegistration
from zope.configuration.xmlconfig import xmlconfig, XMLConfig
from zope.configuration.exceptions import ConfigurationError
Modified: Zope3/branches/jim-adapter/src/zope/component/bbb/adapter.py
===================================================================
--- Zope3/branches/jim-adapter/src/zope/component/bbb/adapter.py 2006-03-12 14:20:53 UTC (rev 65919)
+++ Zope3/branches/jim-adapter/src/zope/component/bbb/adapter.py 2006-03-12 17:52:22 UTC (rev 65920)
@@ -24,7 +24,7 @@
from zope.component.interfaces import IRegistry, ComponentLookupError
from zope.component.bbb.interfaces import IAdapterService
from zope.component.bbb.service import GlobalService
-from zope.component.components import AdapterRegistration, SubscriptionRegistration
+from zope.component.registry import AdapterRegistration, SubscriptionRegistration
from zope.interface import implements, providedBy, Interface, implementedBy
from zope.interface.interfaces import IInterface
Modified: Zope3/branches/jim-adapter/src/zope/component/bbb/utility.py
===================================================================
--- Zope3/branches/jim-adapter/src/zope/component/bbb/utility.py 2006-03-12 14:20:53 UTC (rev 65919)
+++ Zope3/branches/jim-adapter/src/zope/component/bbb/utility.py 2006-03-12 17:52:22 UTC (rev 65920)
@@ -18,7 +18,7 @@
from zope.component.interfaces import Invalid, ComponentLookupError, IRegistry
from zope.component.bbb.interfaces import IUtilityService
from zope.component.service import GlobalService, IService, IServiceDefinition
-from zope.component.components import UtilityRegistration
+from zope.component.registry import UtilityRegistration
import zope.interface
class IGlobalUtilityService(IUtilityService, IRegistry):
Deleted: Zope3/branches/jim-adapter/src/zope/component/components.py
===================================================================
--- Zope3/branches/jim-adapter/src/zope/component/components.py 2006-03-12 14:20:53 UTC (rev 65919)
+++ Zope3/branches/jim-adapter/src/zope/component/components.py 2006-03-12 17:52:22 UTC (rev 65920)
@@ -1,395 +0,0 @@
-##############################################################################
-#
-# Copyright (c) 2006 Zope Corporation and Contributors.
-# All Rights Reserved.
-#
-# This software is subject to the provisions of the Zope Public License,
-# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
-# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
-# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
-# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
-# FOR A PARTICULAR PURPOSE.
-#
-##############################################################################
-"""Basic components support
-
-$Id$
-"""
-
-import types
-import zope.interface.adapter
-from zope import interface
-from zope.component import interfaces
-import zope.interface.interfaces
-
-class Components(object):
-
- def __init__(self, bases=()):
- self._init_registries()
- self._init_registrations()
- self.__bases__ = tuple(bases)
-
- def _init_registries(self):
- self.adapters = zope.interface.adapter.AdapterRegistry()
- self.utilities = zope.interface.adapter.AdapterRegistry()
-
- def _init_registrations(self):
- self._utility_registrations = {}
- self._adapter_registrations = {}
- self._subscription_registrations = []
- self._handler_registrations = []
-
-
- @apply
- def __bases__():
-
- def get_bases(self):
- return self.__dict__['__bases__']
- def set_bases(self, bases):
- self.adapters.__bases__ = tuple([
- base.adapters for base in bases])
- self.utilities.__bases__ = tuple([
- base.utilities for base in bases])
- self.__dict__['__bases__'] = bases
-
- return property(get_bases, set_bases)
-
- def registerUtility(self, component, provided=None, name=u'', info=u''):
- if provided is None:
- provided = _getUtilityProvided(component)
-
-
- subscribed = [
- 1
- for ((p, _), (c,_)) in self._utility_registrations.iteritems()
- if p == provided and c == component
- ]
-
- self._utility_registrations[(provided, name)] = component, info
- self.utilities.register((), provided, name, component)
-
- if not subscribed:
- self.utilities.subscribe((), provided, component)
-
- def unregisterUtility(self, component=None, provided=None, name=u''):
- if provided is None:
- if component is None:
- raise TypeError("Must specify one of component and provided")
- provided = _getUtilityProvided(component)
-
- old = self._utility_registrations.get((provided, name))
- if (old is None) or ((component is not None) and
- (component != old[0])):
- return False
-
- if component is None:
- component = old[0]
- del self._utility_registrations[(provided, name)]
- self.utilities.unregister((), provided, name)
-
- subscribed = [
- 1
- for ((p, _), (c,_)) in self._utility_registrations.iteritems()
- if p == provided and c == component
- ]
-
- if not subscribed:
- self.utilities.unsubscribe((), provided, component)
-
- return True
-
- def registeredUtilities(self):
- for ((provided, name), (component, info)
- ) in self._utility_registrations.iteritems():
- yield UtilityRegistration(provided, name, component, info)
-
- def queryUtility(self, provided, name=u'', default=None):
- return self.utilities.lookup((), provided, name, default)
-
- def getUtility(self, provided, name=u''):
- utility = self.utilities.lookup((), provided, name)
- if utility is None:
- raise interfaces.ComponentLookupError(provided, name)
- return utility
-
- #TODO needs tests
- def getUtilitiesFor(self, interface):
- for name, utility in self.utilities.lookupAll((), interface):
- yield name, utility
-
- #TODO needs tests
- def getAllUtilitiesRegisteredFor(self, interface):
- return self.utilities.subscriptions((), interface)
-
- def registerAdapter(self, factory, required=None, provided=None, name=u'',
- info=u''):
- if provided is None:
- provided = _getAdapterProvided(factory)
- required = _getAdapterRequired(factory, required)
- self._adapter_registrations[(required, provided, name)
- ] = factory, info
- self.adapters.register(required, provided, name, factory)
-
- def unregisterAdapter(self, factory=None,
- required=None, provided=None, name=u'',
- ):
- if provided is None:
- if factory is None:
- raise TypeError("Must specify one of factory and provided")
- provided = _getAdapterProvided(factory)
-
- if (required is None) and (factory is None):
- raise TypeError("Must specify one of factory and required")
-
- required = _getAdapterRequired(factory, required)
- old = self._adapter_registrations.get((required, provided, name))
- if (old is None) or ((factory is not None) and
- (factory != old[0])):
- return False
-
- del self._adapter_registrations[(required, provided, name)]
- self.adapters.unregister(required, provided, name)
- return True
-
- def registeredAdapters(self):
- for ((required, provided, name), (component, info)
- ) in self._adapter_registrations.iteritems():
- yield AdapterRegistration(required, provided, name,
- component, info)
-
- def queryAdapter(self, object, interface, name=u'', default=None):
- return self.adapters.queryAdapter(object, interface, name, default)
-
- def getAdapter(self, object, interface, name=u''):
- adapter = self.adapters.queryAdapter(object, interface, name)
- if adapter is None:
- raise interfaces.ComponentLookupError(object, interface, name)
- return adapter
-
- def queryMultiAdapter(self, objects, interface, name=u'', default=None):
- return self.adapters.queryMultiAdapter(
- objects, interface, name, default)
-
- def getMultiAdapter(self, objects, interface, name=u''):
- adapter = self.adapters.queryMultiAdapter(objects, interface, name)
- if adapter is None:
- raise interfaces.ComponentLookupError(objects, interface, name)
- return adapter
-
- def getAdapters(self, objects, provided):
- for name, factory in self.adapters.lookupAll(
- map(interface.providedBy, objects),
- provided):
- adapter = factory(*objects)
- if adapter is not None:
- yield name, adapter
-
- def registerSubscriptionAdapter(self,
- factory, required=None, provided=None,
- name=u'', info=u''):
- if name:
- raise TypeError("Named subscribers are not yet supported")
- if provided is None:
- provided = _getAdapterProvided(factory)
- required = _getAdapterRequired(factory, required)
- self._subscription_registrations.append(
- (required, provided, name, factory, info)
- )
- self.adapters.subscribe(required, provided, factory)
-
- def registeredSubscriptionAdapters(self):
- for data in self._subscription_registrations:
- yield SubscriptionRegistration(*data)
-
- def unregisterSubscriptionAdapter(self, factory=None,
- required=None, provided=None, name=u'',
- ):
- if name:
- raise TypeError("Named subscribers are not yet supported")
- if provided is None:
- if factory is None:
- raise TypeError("Must specify one of factory and provided")
- provided = _getAdapterProvided(factory)
-
- if (required is None) and (factory is None):
- raise TypeError("Must specify one of factory and required")
-
- required = _getAdapterRequired(factory, required)
-
- if factory is None:
- new = [(r, p, n, f, i)
- for (r, p, n, f, i)
- in self._subscription_registrations
- if not (r == required and p == provided)
- ]
- else:
- new = [(r, p, n, f, i)
- for (r, p, n, f, i)
- in self._subscription_registrations
- if not (r == required and p == provided and f == factory)
- ]
-
- if len(new) == len(self._subscription_registrations):
- return False
-
-
- self._subscription_registrations = new
- self.adapters.unsubscribe(required, provided)
- return True
-
- def subscribers(self, objects, provided):
- return self.adapters.subscribers(objects, provided)
-
-
-
-
- def registerHandler(self,
- factory, required=None,
- name=u'', info=u''):
- if name:
- raise TypeError("Named handlers are not yet supported")
- required = _getAdapterRequired(factory, required)
- self._handler_registrations.append(
- (required, name, factory, info)
- )
- self.adapters.subscribe(required, None, factory)
-
- def registeredHandlers(self):
- for data in self._handler_registrations:
- yield HandlerRegistration(*data)
-
- def unregisterHandler(self, factory=None, required=None, name=u''):
- if name:
- raise TypeError("Named subscribers are not yet supported")
-
- if (required is None) and (factory is None):
- raise TypeError("Must specify one of factory and required")
-
- required = _getAdapterRequired(factory, required)
-
- if factory is None:
- new = [(r, n, f, i)
- for (r, n, f, i)
- in self._handler_registrations
- if r != required
- ]
- else:
- new = [(r, n, f, i)
- for (r, n, f, i)
- in self._handler_registrations
- if not (r == required and f == factory)
- ]
-
- if len(new) == len(self._handler_registrations):
- return False
-
- self._handler_registrations = new
- self.adapters.unsubscribe(required, None)
- return True
-
- def handle(self, *objects):
- self.adapters.subscribers(objects, None)
-
-
-def _getUtilityProvided(component):
- provided = list(interface.providedBy(component))
- if len(provided) == 1:
- return provided[0]
- raise TypeError(
- "The utility doesn't provide a single interface "
- "and no provided interface was specified.")
-
-def _getAdapterProvided(factory):
- provided = list(interface.implementedBy(factory))
- if len(provided) == 1:
- return provided[0]
- raise TypeError(
- "The adapter factory doesn't implement a single interface "
- "and no provided interface was specified.")
-
-
-classTypes = type, types.ClassType
-def _getAdapterRequired(factory, required):
- if required is None:
- try:
- required = factory.__component_adapts__
- except AttributeError:
- raise TypeError(
- "The adapter factory doesn't have a __component_adapts__ "
- "attribute and no required specifications were specified"
- )
- elif zope.interface.interfaces.ISpecification.providedBy(required):
- raise TypeError("the required argument should be a list of "
- "interfaces, not a single interface")
-
- result = []
- for r in required:
- if r is None:
- r = interface.Interface
- elif not zope.interface.interfaces.ISpecification.providedBy(r):
- if isinstance(r, classTypes):
- r = interface.implementedBy(r)
- else:
- raise TypeError("Required specification must be a "
- "specification or class."
- )
- result.append(r)
- return tuple(result)
-
-
-class UtilityRegistration(object):
-
- def __init__(self, provided, name, component, doc):
- (self.provided, self.name, self.component, self.info
- ) = provided, name, component, doc
-
- def __repr__(self):
- return '%s(%r, %r, %r, %r)' % (
- self.__class__.__name__,
- getattr(self.provided, '__name__', None), self.name,
- getattr(self.component, '__name__', self.component), self.info,
- )
-
- def __cmp__(self, other):
- return cmp(self.__repr__(), other.__repr__())
-
-class AdapterRegistration(object):
-
- def __init__(self, required, provided, name, component, doc):
- (self.required, self.provided, self.name, self.factory, self.info
- ) = required, provided, name, component, doc
-
- def __repr__(self):
- return '%s(%r, %r, %r, %r, %r)' % (
- self.__class__.__name__,
- tuple([r.__name__ for r in self.required]),
- getattr(self.provided, '__name__', None), self.name,
- getattr(self.factory, '__name__', self.factory), self.info,
- )
-
- def __cmp__(self, other):
- return cmp(self.__repr__(), other.__repr__())
-
-class SubscriptionRegistration(AdapterRegistration):
- pass
-
-class HandlerRegistration(object):
-
- def __init__(self, required, name, handler, doc):
- (self.required, self.name, self.handler, self.info
- ) = required, name, handler, doc
-
- @property
- def factory(self):
- return self.handler
-
- provided = None
-
- def __repr__(self):
- return '%s(%r, %r, %r, %r)' % (
- self.__class__.__name__,
- tuple([r.__name__ for r in self.required]),
- self.name,
- getattr(self.factory, '__name__', self.factory), self.info,
- )
-
Deleted: Zope3/branches/jim-adapter/src/zope/component/components.txt
===================================================================
--- Zope3/branches/jim-adapter/src/zope/component/components.txt 2006-03-12 14:20:53 UTC (rev 65919)
+++ Zope3/branches/jim-adapter/src/zope/component/components.txt 2006-03-12 17:52:22 UTC (rev 65920)
@@ -1,799 +0,0 @@
-Component-Management objects
-============================
-
-Component-management objects provide a higher-level
-component-management API over the basic adapter-registration API
-provided by the zope.interface package. In particular, it provides:
-
-- utilities
-
-- support for computing adapters, rather than just looking up adapter
- factories.
-
-- management of registration comments
-
-The zope.component.components.Components class provides an
-implementation of zope.component.interfaces.IComponents that provides
-these features.
-
- >>> from zope.component.components import Components
- >>> from zope.component import tests
- >>> components = Components()
-
-Utilities
----------
-
-You can register Utilities using registerUtility:
-
- >>> components.registerUtility(tests.U1(1))
-
-Here we didn't specify an interface or name. An unnamed utility was
-registered for interface I1, since that is only interface implemented
-by the U1 class:
-
- >>> components.getUtility(tests.I1)
- U1(1)
-
-If a component implements other than one interface or no interface,
-then an error will be raised:
-
- >>> components.registerUtility(tests.U12(2))
- ... # doctest: +NORMALIZE_WHITESPACE
- Traceback (most recent call last):
- ...
- TypeError: The utility doesn't provide a single interface and
- no provided interface was specified.
-
- >>> components.registerUtility(tests.A)
- ... # doctest: +NORMALIZE_WHITESPACE
- Traceback (most recent call last):
- ...
- TypeError: The utility doesn't provide a single interface and
- no provided interface was specified.
-
-
-We can provide an interface if desired:
-
- >>> components.registerUtility(tests.U12(2), tests.I2)
-
-and we can specify a name:
-
- >>> components.registerUtility(tests.U12(3), tests.I2, u'three')
-
- >>> components.getUtility(tests.I2)
- U12(2)
-
- >>> components.getUtility(tests.I2, 'three')
- U12(3)
-
-If you try to get a utility that doesn't exist, you'll get a component
-lookup error:
-
- >>> components.getUtility(tests.I3)
- ... # doctest: +NORMALIZE_WHITESPACE
- Traceback (most recent call last):
- ...
- ComponentLookupError:
- (<InterfaceClass zope.component.tests.I3>, u'')
-
-Unless you use queryUtility:
-
- >>> components.queryUtility(tests.I3)
- >>> components.queryUtility(tests.I3, default=42)
- 42
-
-You can get information about registered utilities with the
-registeredUtilities method:
-
- >>> for regsitration in sorted(components.registeredUtilities()):
- ... print regsitration.provided, regsitration.name
- ... print regsitration.component, regsitration.info
- <InterfaceClass zope.component.tests.I1>
- U1(1)
- <InterfaceClass zope.component.tests.I2>
- U12(2)
- <InterfaceClass zope.component.tests.I2> three
- U12(3)
-
-Duplicate registrations replace existing ones:
-
- >>> components.registerUtility(tests.U1(4), info=u'use 4 now')
- >>> components.getUtility(tests.I1)
- U1(4)
-
- >>> for regsitration in sorted(components.registeredUtilities()):
- ... print regsitration.provided, regsitration.name
- ... print regsitration.component, regsitration.info
- <InterfaceClass zope.component.tests.I1>
- U1(4) use 4 now
- <InterfaceClass zope.component.tests.I2>
- U12(2)
- <InterfaceClass zope.component.tests.I2> three
- U12(3)
-
-As shown in the this example, you can provide an "info" argumemnt when
-registering utilities. This provides extra documentation about the
-registration itself that is shown when listing registrations.
-
-You can also unregister utilities:
-
- >>> components.unregisterUtility(provided=tests.I1)
- True
-
-A boolean is returned indicating whether anything changed:
-
- >>> components.queryUtility(tests.I1)
- >>> for regsitration in sorted(components.registeredUtilities()):
- ... print regsitration.provided, regsitration.name
- ... print regsitration.component, regsitration.info
- <InterfaceClass zope.component.tests.I2>
- U12(2)
- <InterfaceClass zope.component.tests.I2> three
- U12(3)
-
-When you unregister, you can specify a component. If the component
-doesn't match the one registered, then nothing happens:
-
- >>> u5 = tests.U1(5)
- >>> components.registerUtility(u5)
- >>> components.unregisterUtility(tests.U1(6))
- False
- >>> components.queryUtility(tests.I1)
- U1(5)
- >>> components.unregisterUtility(u5)
- True
- >>> components.queryUtility(tests.I1)
-
-Adapters
---------
-
-You can register adapters with registerAdapter:
-
- >>> components.registerAdapter(tests.A12_1)
-
-Here, we didn't specify required interfaces, a provided interface, or
-a name. The required interfaces were determined from the factory
-s __component_adapts__ attribute and the provided interface was
-determined by introspecting what the factory implements.
-
- >>> components.getMultiAdapter((tests.U1(6), tests.U12(7)), tests.IA1)
- A12_1(U1(6), U12(7))
-
-If a factory implements more than one interface, an exception will be
-raised:
-
- >>> components.registerAdapter(tests.A1_12)
- ... # doctest: +NORMALIZE_WHITESPACE
- Traceback (most recent call last):
- ...
- TypeError: The adapter factory doesn't implement a single
- interface and no provided interface was specified.
-
-Unless the provided interface is specified:
-
- >>> components.registerAdapter(tests.A1_12, provided=tests.IA2)
-
-If a factory doesn't declare an implemented interface, an exception will be
-raised:
-
- >>> components.registerAdapter(tests.A12_)
- ... # doctest: +NORMALIZE_WHITESPACE
- Traceback (most recent call last):
- ...
- TypeError: The adapter factory doesn't implement a single
- interface and no provided interface was specified.
-
-Unless the provided interface is specified:
-
- >>> components.registerAdapter(tests.A12_, provided=tests.IA2)
-
-If the required interface needs to be specified in the
-registration if the factory doesn't have a __component_adapts__
-attribute:
-
- >>> components.registerAdapter(tests.A_2)
- ... # doctest: +NORMALIZE_WHITESPACE
- Traceback (most recent call last):
- ...
- TypeError: The adapter factory doesn't have a __component_adapts__
- attribute and no required specifications were specified
-
-Unless the required specifications specified:
-
- >>> components.registerAdapter(tests.A_2, required=[tests.I3])
-
-Classes can be specified in place of specifications, in which case the
-implementedBy specification for the class is used:
-
- >>> components.registerAdapter(tests.A_3, required=[tests.U],
- ... info="Really class specific")
-
-We can see the adapters that have been registered using the
-registeredAdapters method:
-
- >>> for regsitration in sorted(components.registeredAdapters()):
- ... print regsitration.required
- ... print regsitration.provided, regsitration.name
- ... print regsitration.factory, regsitration.info
- ... # doctest: +NORMALIZE_WHITESPACE
- (<InterfaceClass zope.component.tests.I1>,
- <InterfaceClass zope.component.tests.I2>)
- <InterfaceClass zope.component.tests.IA1>
- zope.component.tests.A12_1
- (<InterfaceClass zope.component.tests.I1>,
- <InterfaceClass zope.component.tests.I2>)
- <InterfaceClass zope.component.tests.IA2>
- zope.component.tests.A12_
- (<InterfaceClass zope.component.tests.I1>,)
- <InterfaceClass zope.component.tests.IA2>
- zope.component.tests.A1_12
- (<InterfaceClass zope.component.tests.I3>,)
- <InterfaceClass zope.component.tests.IA2>
- zope.component.tests.A_2
- (<implementedBy zope.component.tests.U>,)
- <InterfaceClass zope.component.tests.IA3>
- zope.component.tests.A_3 Really class specific
-
-As with utilities, we can provide registration information when
-registering adapters.
-
-If you try to fetch an adapter that isn't registered, you'll get a
-component-lookup error:
-
- >>> components.getMultiAdapter((tests.U(8), ), tests.IA1)
- ... # doctest: +NORMALIZE_WHITESPACE
- Traceback (most recent call last):
- ...
- ComponentLookupError: ((U(8),),
- <InterfaceClass zope.component.tests.IA1>, u'')
-
-unless you use queryAdapter:
-
- >>> components.queryMultiAdapter((tests.U(8), ), tests.IA1)
- >>> components.queryMultiAdapter((tests.U(8), ), tests.IA1, default=42)
- 42
-
-When looking up an adapter for a single object, you can use the
-slightly simpler getAdapter and queryAdapter calls:
-
- >>> components.getAdapter(tests.U1(9), tests.IA2)
- A1_12(U1(9))
-
- >>> components.queryAdapter(tests.U1(9), tests.IA2)
- A1_12(U1(9))
-
- >>> components.getAdapter(tests.U(8), tests.IA1)
- ... # doctest: +NORMALIZE_WHITESPACE
- Traceback (most recent call last):
- ...
- ComponentLookupError: (U(8),
- <InterfaceClass zope.component.tests.IA1>, u'')
-
- >>> components.queryAdapter(tests.U(8), tests.IA2)
- >>> components.queryAdapter(tests.U(8), tests.IA2, default=42)
- 42
-
-You can unregister an adapter. If a factory is provided and if the
-rewuired and provided interfaces, can be infered, then they need not
-be provided:
-
- >>> components.unregisterAdapter(tests.A12_1)
- True
- >>> for regsitration in sorted(components.registeredAdapters()):
- ... print regsitration.required
- ... print regsitration.provided, regsitration.name
- ... print regsitration.factory, regsitration.info
- ... # doctest: +NORMALIZE_WHITESPACE
- (<InterfaceClass zope.component.tests.I1>,
- <InterfaceClass zope.component.tests.I2>)
- <InterfaceClass zope.component.tests.IA2>
- zope.component.tests.A12_
- (<InterfaceClass zope.component.tests.I1>,)
- <InterfaceClass zope.component.tests.IA2>
- zope.component.tests.A1_12
- (<InterfaceClass zope.component.tests.I3>,)
- <InterfaceClass zope.component.tests.IA2>
- zope.component.tests.A_2
- (<implementedBy zope.component.tests.U>,)
- <InterfaceClass zope.component.tests.IA3>
- zope.component.tests.A_3 Really class specific
-
-A boolean is returned indicating whether a change was made.
-
-If a factory implements more than one interface, an exception will be
-raised:
-
- >>> components.unregisterAdapter(tests.A1_12)
- ... # doctest: +NORMALIZE_WHITESPACE
- Traceback (most recent call last):
- ...
- TypeError: The adapter factory doesn't implement a single
- interface and no provided interface was specified.
-
-Unless the provided interface is specified:
-
- >>> components.unregisterAdapter(tests.A1_12, provided=tests.IA2)
- True
-
-If a factory doesn't declare an implemented interface, an exception will be
-raised:
-
- >>> components.unregisterAdapter(tests.A12_)
- ... # doctest: +NORMALIZE_WHITESPACE
- Traceback (most recent call last):
- ...
- TypeError: The adapter factory doesn't implement a single
- interface and no provided interface was specified.
-
-Unless the provided interface is specified:
-
- >>> components.unregisterAdapter(tests.A12_, provided=tests.IA2)
- True
-
-The required interface needs to be specified if the factory doesn't
-have a __component_adapts__ attribute:
-
- >>> components.unregisterAdapter(tests.A_2)
- ... # doctest: +NORMALIZE_WHITESPACE
- Traceback (most recent call last):
- ...
- TypeError: The adapter factory doesn't have a __component_adapts__
- attribute and no required specifications were specified
-
- >>> components.unregisterAdapter(tests.A_2, required=[tests.I3])
- True
-
- >>> for regsitration in sorted(components.registeredAdapters()):
- ... print regsitration.required
- ... print regsitration.provided, regsitration.name
- ... print regsitration.factory, regsitration.info
- ... # doctest: +NORMALIZE_WHITESPACE
- (<implementedBy zope.component.tests.U>,)
- <InterfaceClass zope.component.tests.IA3>
- zope.component.tests.A_3 Really class specific
-
-If a factory is unregistered that is not registered, False is
-returned:
-
-
- >>> components.unregisterAdapter(tests.A_2, required=[tests.I3])
- False
- >>> components.unregisterAdapter(tests.A12_1, required=[tests.U])
- False
-
-The factory can be omitted, to unregister *any* factory that matches
-specified required and provided interfaces:
-
- >>> components.unregisterAdapter(required=[tests.U], provided=tests.IA3)
- True
-
- >>> for regsitration in sorted(components.registeredAdapters()):
- ... print regsitration
-
-Adapters can be named:
-
- >>> components.registerAdapter(tests.A1_12, provided=tests.IA2,
- ... name=u'test')
-
- >>> components.queryMultiAdapter((tests.U1(9), ), tests.IA2)
- >>> components.queryMultiAdapter((tests.U1(9), ), tests.IA2, name=u'test')
- A1_12(U1(9))
-
- >>> components.queryAdapter(tests.U1(9), tests.IA2)
- >>> components.queryAdapter(tests.U1(9), tests.IA2, name=u'test')
- A1_12(U1(9))
- >>> components.getAdapter(tests.U1(9), tests.IA2, name=u'test')
- A1_12(U1(9))
-
-It is possible to look up all of the adapters that provide an
-interface:
-
- >>> components.registerAdapter(tests.A1_23, provided=tests.IA2,
- ... name=u'test 2')
-
- >>> components.registerAdapter(tests.A1_12, provided=tests.IA2)
- >>> for name, adapter in sorted(components.getAdapters((tests.U1(9), ),
- ... tests.IA2)):
- ... print name, adapter
- A1_12(U1(9))
- test A1_12(U1(9))
- test 2 A1_23(U1(9))
-
-
-getAdapters is most commonly used as the basis of menu systems.
-
-If an adapter factory returns None, it is equivalent to there being no
-factory:
-
- >>> components.registerAdapter(tests.noop,
- ... required=[tests.IA1], provided=tests.IA2,
- ... name=u'test noop')
- >>> components.queryAdapter(tests.U1(9), tests.IA2, name=u'test noop')
-
- >>> components.registerAdapter(tests.A1_12, provided=tests.IA2)
- >>> for name, adapter in sorted(components.getAdapters((tests.U1(9), ),
- ... tests.IA2)):
- ... print name, adapter
- A1_12(U1(9))
- test A1_12(U1(9))
- test 2 A1_23(U1(9))
-
-
- >>> components.unregisterAdapter(tests.A1_12, provided=tests.IA2,
- ... name=u'test')
- True
- >>> components.unregisterAdapter(tests.A1_12, provided=tests.IA2)
- True
- >>> for regsitration in sorted(components.registeredAdapters()):
- ... print regsitration.required
- ... print regsitration.provided, regsitration.name
- ... print regsitration.factory, regsitration.info
- ... # doctest: +NORMALIZE_WHITESPACE
- (<InterfaceClass zope.component.tests.I1>,)
- <InterfaceClass zope.component.tests.IA2> test 2
- zope.component.tests.A1_23
- (<InterfaceClass zope.component.tests.IA1>,)
- <InterfaceClass zope.component.tests.IA2> test noop
- <function noop at 0xb79a1064>
-
-
-Subscribers
------------
-
-Subscribers provide a way to get multiple adapters of a given type.
-In this regard, subscribers are like named adapters, except that there
-isn't any concept of the most specific adapter for a given name.
-
-Subscribers are registered by calling registerSubscriptionAdapter:
-
- >>> components.registerSubscriptionAdapter(tests.A1_2)
-
- >>> components.registerSubscriptionAdapter(
- ... tests.A1_12, provided=tests.IA2)
-
- >>> components.registerSubscriptionAdapter(
- ... tests.A, [tests.I1], tests.IA2,
- ... info='a sample comment')
-
-The same rules, with regard to when required and provided interfaces
-have to be specified apply as with adapters:
-
- >>> components.registerSubscriptionAdapter(tests.A1_12)
- ... # doctest: +NORMALIZE_WHITESPACE
- Traceback (most recent call last):
- ...
- TypeError: The adapter factory doesn't implement a single
- interface and no provided interface was specified.
-
- >>> components.registerSubscriptionAdapter(tests.A)
- ... # doctest: +NORMALIZE_WHITESPACE
- Traceback (most recent call last):
- ...
- TypeError: The adapter factory doesn't implement a single interface and
- no provided interface was specified.
-
- >>> components.registerSubscriptionAdapter(tests.A, required=[tests.IA1])
- ... # doctest: +NORMALIZE_WHITESPACE
- Traceback (most recent call last):
- ...
- TypeError: The adapter factory doesn't implement a single interface
- and no provided interface was specified.
-
-Note that we provided the info argument as a keyword argument above.
-That's because there is a name argument that's reserved for future
-use. We can give a name, as long as it is an empty string:
-
- >>> components.registerSubscriptionAdapter(
- ... tests.A, [tests.I1], tests.IA2, u'', 'a sample comment')
-
- >>> components.registerSubscriptionAdapter(
- ... tests.A, [tests.I1], tests.IA2, u'oops', 'a sample comment')
- Traceback (most recent call last):
- ...
- TypeError: Named subscribers are not yet supported
-
-Subscribers are looked up using the subscribers method:
-
- >>> for s in components.subscribers((tests.U1(1), ), tests.IA2):
- ... print s
- A1_2(U1(1))
- A1_12(U1(1))
- A(U1(1),)
- A(U1(1),)
-
-Note that, because we created multiple subscriptions for A, we got multiple
-subscriber instances.
-
-As with normal adapters, if a factory returns None, the result is skipped:
-
- >>> components.registerSubscriptionAdapter(
- ... tests.noop, [tests.I1], tests.IA2)
-
- >>> for s in components.subscribers((tests.U1(1), ), tests.IA2):
- ... print s
- A1_2(U1(1))
- A1_12(U1(1))
- A(U1(1),)
- A(U1(1),)
-
-We can get registration information for subscriptions:
-
- >>> for registration in sorted(
- ... components.registeredSubscriptionAdapters()):
- ... print registration.required
- ... print registration.provided, registration.name
- ... print registration.factory, registration.info
- (<InterfaceClass zope.component.tests.I1>,)
- <InterfaceClass zope.component.tests.IA2>
- zope.component.tests.A a sample comment
- (<InterfaceClass zope.component.tests.I1>,)
- <InterfaceClass zope.component.tests.IA2>
- zope.component.tests.A a sample comment
- (<InterfaceClass zope.component.tests.I1>,)
- <InterfaceClass zope.component.tests.IA2>
- zope.component.tests.A1_12
- (<InterfaceClass zope.component.tests.I1>,)
- <InterfaceClass zope.component.tests.IA2>
- zope.component.tests.A1_2
- (<InterfaceClass zope.component.tests.I1>,)
- <InterfaceClass zope.component.tests.IA2>
- <function noop at 0xb796ff7c>
-
-We can also unregister subscriptions in much the same way we can for adapters:
-
- >>> components.unregisterSubscriptionAdapter(tests.A1_2)
- True
-
- >>> for registration in sorted(
- ... components.registeredSubscriptionAdapters()):
- ... print registration.required
- ... print registration.provided, registration.name
- ... print registration.factory, registration.info
- (<InterfaceClass zope.component.tests.I1>,)
- <InterfaceClass zope.component.tests.IA2>
- zope.component.tests.A a sample comment
- (<InterfaceClass zope.component.tests.I1>,)
- <InterfaceClass zope.component.tests.IA2>
- zope.component.tests.A a sample comment
- (<InterfaceClass zope.component.tests.I1>,)
- <InterfaceClass zope.component.tests.IA2>
- zope.component.tests.A1_12
- (<InterfaceClass zope.component.tests.I1>,)
- <InterfaceClass zope.component.tests.IA2>
- <function noop at 0xb796ff7c>
-
- >>> components.unregisterSubscriptionAdapter(
- ... tests.A, [tests.I1], tests.IA2)
- True
-
- >>> for registration in sorted(
- ... components.registeredSubscriptionAdapters()):
- ... print registration.required
- ... print registration.provided, registration.name
- ... print registration.factory, registration.info
- (<InterfaceClass zope.component.tests.I1>,)
- <InterfaceClass zope.component.tests.IA2>
- zope.component.tests.A1_12
- (<InterfaceClass zope.component.tests.I1>,)
- <InterfaceClass zope.component.tests.IA2>
- <function noop at 0xb796ff7c>
-
-Note here that both registrations for A were removed.
-
-If we omit the factory, we must specify the required and provided interfaces:
-
- >>> components.unregisterSubscriptionAdapter(required=[tests.I1])
- Traceback (most recent call last):
- ...
- TypeError: Must specify one of factory and provided
-
- >>> components.unregisterSubscriptionAdapter(provided=tests.IA2)
- Traceback (most recent call last):
- ...
- TypeError: Must specify one of factory and required
-
- >>> components.unregisterSubscriptionAdapter(
- ... required=[tests.I1], provided=tests.IA2)
- True
-
- >>> for registration in sorted(
- ... components.registeredSubscriptionAdapters()):
- ... print registration.factory
-
-As when registering, an error is raised if the registration
-information can't be determined from the factory and isn't specified:
-
- >>> components.unregisterSubscriptionAdapter(tests.A1_12)
- ... # doctest: +NORMALIZE_WHITESPACE
- Traceback (most recent call last):
- ...
- TypeError: The adapter factory doesn't implement a single
- interface and no provided interface was specified.
-
- >>> components.unregisterSubscriptionAdapter(tests.A)
- ... # doctest: +NORMALIZE_WHITESPACE
- Traceback (most recent call last):
- ...
- TypeError: The adapter factory doesn't implement a single interface and
- no provided interface was specified.
-
- >>> components.unregisterSubscriptionAdapter(tests.A, required=[tests.IA1])
- ... # doctest: +NORMALIZE_WHITESPACE
- Traceback (most recent call last):
- ...
- TypeError: The adapter factory doesn't implement a single interface
- and no provided interface was specified.
-
-If you unregister something that's not registered, nothing will be changed and False will be returned:
-
-
- >>> components.unregisterSubscriptionAdapter(
- ... required=[tests.I1], provided=tests.IA2)
- False
-
-Handlers
---------
-
-Handlers are used when you want to perform some function in response
-to an event. Handlers aren't expected to return anything when called
-and are not registered to provide any interface.
-
- >>> from zope import component
- >>> @component.adapter(tests.I1)
- ... def handle1(x):
- ... print 'handle1', x
-
- >>> components.registerHandler(handle1, info="First handler")
- >>> components.handle(tests.U1(1))
- handle1 U1(1)
-
- >>> @component.adapter(tests.I1, tests.I2)
- ... def handle12(x, y):
- ... print 'handle12', x, y
-
- >>> components.registerHandler(handle12)
- >>> components.handle(tests.U1(1), tests.U12(2))
- handle12 U1(1) U12(2)
-
-If a handler doesn't document interfaces it handles, then
-the required interfaces must be specified:
-
- >>> def handle(*objects):
- ... print 'handle', objects
-
- >>> components.registerHandler(handle)
- ... # doctest: +NORMALIZE_WHITESPACE
- Traceback (most recent call last):
- ...
- TypeError: The adapter factory doesn't have a __component_adapts__
- attribute and no required specifications were specified
-
- >>> components.registerHandler(handle, required=[tests.I1],
- ... info="a comment")
-
-Handlers can also be registered for classes:
-
- >>> components.registerHandler(handle, required=[tests.U],
- ... info="handle a class")
-
-
- >>> components.handle(tests.U1(1))
- handle (U1(1),)
- handle1 U1(1)
- handle (U1(1),)
-
-We can list the handler registrations:
-
- >>> for registration in components.registeredHandlers():
- ... print registration.required
- ... print registration.handler, registration.info
- ... # doctest: +NORMALIZE_WHITESPACE
- (<InterfaceClass zope.component.tests.I1>,)
- <function handle1 at 0xb78f5bfc> First handler
- (<InterfaceClass zope.component.tests.I1>,
- <InterfaceClass zope.component.tests.I2>)
- <function handle12 at 0xb78f5c34>
- (<InterfaceClass zope.component.tests.I1>,)
- <function handle at 0xb78f5ca4> a comment
- (<implementedBy zope.component.tests.U>,)
- <function handle at 0xb78f5ca4> handle a class
-
-and we can unregister handlers:
-
- >>> components.unregisterHandler(required=[tests.U])
- True
-
- >>> for registration in components.registeredHandlers():
- ... print registration.required
- ... print registration.handler, registration.info
- ... # doctest: +NORMALIZE_WHITESPACE
- (<InterfaceClass zope.component.tests.I1>,)
- <function handle1 at 0xb78f5bfc> First handler
- (<InterfaceClass zope.component.tests.I1>,
- <InterfaceClass zope.component.tests.I2>)
- <function handle12 at 0xb78f5c34>
- (<InterfaceClass zope.component.tests.I1>,)
- <function handle at 0xb78f5ca4> a comment
-
- >>> components.unregisterHandler(handle12)
- True
-
- >>> for registration in components.registeredHandlers():
- ... print registration.required
- ... print registration.handler, registration.info
- (<InterfaceClass zope.component.tests.I1>,)
- <function handle1 at 0xb78f5bfc> First handler
- (<InterfaceClass zope.component.tests.I1>,)
- <function handle at 0xb78f5ca4> a comment
-
- >>> components.unregisterHandler(handle12)
- False
-
- >>> components.unregisterHandler()
- Traceback (most recent call last):
- ...
- TypeError: Must specify one of factory and required
-
- >>> components.registerHandler(handle)
- ... # doctest: +NORMALIZE_WHITESPACE
- Traceback (most recent call last):
- ...
- TypeError: The adapter factory doesn't have a __component_adapts__
- attribute and no required specifications were specified
-
-Extending
----------
-
-Component-management objects can extend other component-management
-objects.
-
- >>> c1 = Components()
- >>> c1.__bases__
- ()
-
- >>> c2 = Components((c1, ))
- >>> c2.__bases__ == (c1, )
- True
-
- >>> c1.registerUtility(tests.U1(1))
- >>> c1.queryUtility(tests.I1)
- U1(1)
- >>> c2.queryUtility(tests.I1)
- U1(1)
- >>> c1.registerUtility(tests.U1(2))
- >>> c2.queryUtility(tests.I1)
- U1(2)
-
-We can use multiple inheritence:
-
- >>> c3 = Components((c1, ))
- >>> c4 = Components((c2, c3))
- >>> c4.queryUtility(tests.I1)
- U1(2)
-
- >>> c1.registerUtility(tests.U12(1), tests.I2)
- >>> c4.queryUtility(tests.I2)
- U12(1)
-
- >>> c3.registerUtility(tests.U12(3), tests.I2)
- >>> c4.queryUtility(tests.I2)
- U12(3)
-
- >>> c1.registerHandler(handle1, info="First handler")
- >>> c2.registerHandler(handle, required=[tests.U])
-
- >>> @component.adapter(tests.I1)
- ... def handle3(x):
- ... print 'handle3', x
- >>> c3.registerHandler(handle3)
-
- >>> @component.adapter(tests.I1)
- ... def handle4(x):
- ... print 'handle4', x
- >>> c4.registerHandler(handle4)
-
- >>> c4.handle(tests.U1(1))
- handle1 U1(1)
- handle3 U1(1)
- handle (U1(1),)
- handle4 U1(1)
Modified: Zope3/branches/jim-adapter/src/zope/component/globalregistry.py
===================================================================
--- Zope3/branches/jim-adapter/src/zope/component/globalregistry.py 2006-03-12 14:20:53 UTC (rev 65919)
+++ Zope3/branches/jim-adapter/src/zope/component/globalregistry.py 2006-03-12 17:52:22 UTC (rev 65920)
@@ -19,7 +19,7 @@
from zope.interface import implements
from zope.interface.adapter import AdapterRegistry
from zope.deprecation.deprecation import deprecate, deprecated
-from zope.component.components import Components
+from zope.component.registry import Components
from zope.component.interfaces import Invalid, IComponentLookup, IRegistry
from zope.interface.interfaces import ISpecification
Modified: Zope3/branches/jim-adapter/src/zope/component/persistentregistry.py
===================================================================
--- Zope3/branches/jim-adapter/src/zope/component/persistentregistry.py 2006-03-12 14:20:53 UTC (rev 65919)
+++ Zope3/branches/jim-adapter/src/zope/component/persistentregistry.py 2006-03-12 17:52:22 UTC (rev 65920)
@@ -19,7 +19,7 @@
import persistent.list
import zope.interface.adapter
-import zope.component.components
+import zope.component.registry
class PersistentAdapterRegistry(zope.interface.adapter.AdapterRegistry,
persistent.Persistent):
@@ -28,7 +28,7 @@
self._p_changed = True
super(PersistentAdapterRegistry, self).changed()
-class PersistentComponents(zope.component.components.Components):
+class PersistentComponents(zope.component.registry.Components):
def _init_registries(self):
self.adapters = PersistentAdapterRegistry()
Copied: Zope3/branches/jim-adapter/src/zope/component/registry.py (from rev 65919, Zope3/branches/jim-adapter/src/zope/component/components.py)
Copied: Zope3/branches/jim-adapter/src/zope/component/registry.txt (from rev 65919, Zope3/branches/jim-adapter/src/zope/component/components.txt)
===================================================================
--- Zope3/branches/jim-adapter/src/zope/component/components.txt 2006-03-12 14:20:53 UTC (rev 65919)
+++ Zope3/branches/jim-adapter/src/zope/component/registry.txt 2006-03-12 17:52:22 UTC (rev 65920)
@@ -0,0 +1,799 @@
+Component-Management objects
+============================
+
+Component-management objects provide a higher-level
+component-management API over the basic adapter-registration API
+provided by the zope.interface package. In particular, it provides:
+
+- utilities
+
+- support for computing adapters, rather than just looking up adapter
+ factories.
+
+- management of registration comments
+
+The zope.component.registry.Components class provides an
+implementation of zope.component.interfaces.IComponents that provides
+these features.
+
+ >>> from zope.component.registry import Components
+ >>> from zope.component import tests
+ >>> components = Components()
+
+Utilities
+---------
+
+You can register Utilities using registerUtility:
+
+ >>> components.registerUtility(tests.U1(1))
+
+Here we didn't specify an interface or name. An unnamed utility was
+registered for interface I1, since that is only interface implemented
+by the U1 class:
+
+ >>> components.getUtility(tests.I1)
+ U1(1)
+
+If a component implements other than one interface or no interface,
+then an error will be raised:
+
+ >>> components.registerUtility(tests.U12(2))
+ ... # doctest: +NORMALIZE_WHITESPACE
+ Traceback (most recent call last):
+ ...
+ TypeError: The utility doesn't provide a single interface and
+ no provided interface was specified.
+
+ >>> components.registerUtility(tests.A)
+ ... # doctest: +NORMALIZE_WHITESPACE
+ Traceback (most recent call last):
+ ...
+ TypeError: The utility doesn't provide a single interface and
+ no provided interface was specified.
+
+
+We can provide an interface if desired:
+
+ >>> components.registerUtility(tests.U12(2), tests.I2)
+
+and we can specify a name:
+
+ >>> components.registerUtility(tests.U12(3), tests.I2, u'three')
+
+ >>> components.getUtility(tests.I2)
+ U12(2)
+
+ >>> components.getUtility(tests.I2, 'three')
+ U12(3)
+
+If you try to get a utility that doesn't exist, you'll get a component
+lookup error:
+
+ >>> components.getUtility(tests.I3)
+ ... # doctest: +NORMALIZE_WHITESPACE
+ Traceback (most recent call last):
+ ...
+ ComponentLookupError:
+ (<InterfaceClass zope.component.tests.I3>, u'')
+
+Unless you use queryUtility:
+
+ >>> components.queryUtility(tests.I3)
+ >>> components.queryUtility(tests.I3, default=42)
+ 42
+
+You can get information about registered utilities with the
+registeredUtilities method:
+
+ >>> for regsitration in sorted(components.registeredUtilities()):
+ ... print regsitration.provided, regsitration.name
+ ... print regsitration.component, regsitration.info
+ <InterfaceClass zope.component.tests.I1>
+ U1(1)
+ <InterfaceClass zope.component.tests.I2>
+ U12(2)
+ <InterfaceClass zope.component.tests.I2> three
+ U12(3)
+
+Duplicate registrations replace existing ones:
+
+ >>> components.registerUtility(tests.U1(4), info=u'use 4 now')
+ >>> components.getUtility(tests.I1)
+ U1(4)
+
+ >>> for regsitration in sorted(components.registeredUtilities()):
+ ... print regsitration.provided, regsitration.name
+ ... print regsitration.component, regsitration.info
+ <InterfaceClass zope.component.tests.I1>
+ U1(4) use 4 now
+ <InterfaceClass zope.component.tests.I2>
+ U12(2)
+ <InterfaceClass zope.component.tests.I2> three
+ U12(3)
+
+As shown in the this example, you can provide an "info" argumemnt when
+registering utilities. This provides extra documentation about the
+registration itself that is shown when listing registrations.
+
+You can also unregister utilities:
+
+ >>> components.unregisterUtility(provided=tests.I1)
+ True
+
+A boolean is returned indicating whether anything changed:
+
+ >>> components.queryUtility(tests.I1)
+ >>> for regsitration in sorted(components.registeredUtilities()):
+ ... print regsitration.provided, regsitration.name
+ ... print regsitration.component, regsitration.info
+ <InterfaceClass zope.component.tests.I2>
+ U12(2)
+ <InterfaceClass zope.component.tests.I2> three
+ U12(3)
+
+When you unregister, you can specify a component. If the component
+doesn't match the one registered, then nothing happens:
+
+ >>> u5 = tests.U1(5)
+ >>> components.registerUtility(u5)
+ >>> components.unregisterUtility(tests.U1(6))
+ False
+ >>> components.queryUtility(tests.I1)
+ U1(5)
+ >>> components.unregisterUtility(u5)
+ True
+ >>> components.queryUtility(tests.I1)
+
+Adapters
+--------
+
+You can register adapters with registerAdapter:
+
+ >>> components.registerAdapter(tests.A12_1)
+
+Here, we didn't specify required interfaces, a provided interface, or
+a name. The required interfaces were determined from the factory
+s __component_adapts__ attribute and the provided interface was
+determined by introspecting what the factory implements.
+
+ >>> components.getMultiAdapter((tests.U1(6), tests.U12(7)), tests.IA1)
+ A12_1(U1(6), U12(7))
+
+If a factory implements more than one interface, an exception will be
+raised:
+
+ >>> components.registerAdapter(tests.A1_12)
+ ... # doctest: +NORMALIZE_WHITESPACE
+ Traceback (most recent call last):
+ ...
+ TypeError: The adapter factory doesn't implement a single
+ interface and no provided interface was specified.
+
+Unless the provided interface is specified:
+
+ >>> components.registerAdapter(tests.A1_12, provided=tests.IA2)
+
+If a factory doesn't declare an implemented interface, an exception will be
+raised:
+
+ >>> components.registerAdapter(tests.A12_)
+ ... # doctest: +NORMALIZE_WHITESPACE
+ Traceback (most recent call last):
+ ...
+ TypeError: The adapter factory doesn't implement a single
+ interface and no provided interface was specified.
+
+Unless the provided interface is specified:
+
+ >>> components.registerAdapter(tests.A12_, provided=tests.IA2)
+
+If the required interface needs to be specified in the
+registration if the factory doesn't have a __component_adapts__
+attribute:
+
+ >>> components.registerAdapter(tests.A_2)
+ ... # doctest: +NORMALIZE_WHITESPACE
+ Traceback (most recent call last):
+ ...
+ TypeError: The adapter factory doesn't have a __component_adapts__
+ attribute and no required specifications were specified
+
+Unless the required specifications specified:
+
+ >>> components.registerAdapter(tests.A_2, required=[tests.I3])
+
+Classes can be specified in place of specifications, in which case the
+implementedBy specification for the class is used:
+
+ >>> components.registerAdapter(tests.A_3, required=[tests.U],
+ ... info="Really class specific")
+
+We can see the adapters that have been registered using the
+registeredAdapters method:
+
+ >>> for regsitration in sorted(components.registeredAdapters()):
+ ... print regsitration.required
+ ... print regsitration.provided, regsitration.name
+ ... print regsitration.factory, regsitration.info
+ ... # doctest: +NORMALIZE_WHITESPACE
+ (<InterfaceClass zope.component.tests.I1>,
+ <InterfaceClass zope.component.tests.I2>)
+ <InterfaceClass zope.component.tests.IA1>
+ zope.component.tests.A12_1
+ (<InterfaceClass zope.component.tests.I1>,
+ <InterfaceClass zope.component.tests.I2>)
+ <InterfaceClass zope.component.tests.IA2>
+ zope.component.tests.A12_
+ (<InterfaceClass zope.component.tests.I1>,)
+ <InterfaceClass zope.component.tests.IA2>
+ zope.component.tests.A1_12
+ (<InterfaceClass zope.component.tests.I3>,)
+ <InterfaceClass zope.component.tests.IA2>
+ zope.component.tests.A_2
+ (<implementedBy zope.component.tests.U>,)
+ <InterfaceClass zope.component.tests.IA3>
+ zope.component.tests.A_3 Really class specific
+
+As with utilities, we can provide registration information when
+registering adapters.
+
+If you try to fetch an adapter that isn't registered, you'll get a
+component-lookup error:
+
+ >>> components.getMultiAdapter((tests.U(8), ), tests.IA1)
+ ... # doctest: +NORMALIZE_WHITESPACE
+ Traceback (most recent call last):
+ ...
+ ComponentLookupError: ((U(8),),
+ <InterfaceClass zope.component.tests.IA1>, u'')
+
+unless you use queryAdapter:
+
+ >>> components.queryMultiAdapter((tests.U(8), ), tests.IA1)
+ >>> components.queryMultiAdapter((tests.U(8), ), tests.IA1, default=42)
+ 42
+
+When looking up an adapter for a single object, you can use the
+slightly simpler getAdapter and queryAdapter calls:
+
+ >>> components.getAdapter(tests.U1(9), tests.IA2)
+ A1_12(U1(9))
+
+ >>> components.queryAdapter(tests.U1(9), tests.IA2)
+ A1_12(U1(9))
+
+ >>> components.getAdapter(tests.U(8), tests.IA1)
+ ... # doctest: +NORMALIZE_WHITESPACE
+ Traceback (most recent call last):
+ ...
+ ComponentLookupError: (U(8),
+ <InterfaceClass zope.component.tests.IA1>, u'')
+
+ >>> components.queryAdapter(tests.U(8), tests.IA2)
+ >>> components.queryAdapter(tests.U(8), tests.IA2, default=42)
+ 42
+
+You can unregister an adapter. If a factory is provided and if the
+rewuired and provided interfaces, can be infered, then they need not
+be provided:
+
+ >>> components.unregisterAdapter(tests.A12_1)
+ True
+ >>> for regsitration in sorted(components.registeredAdapters()):
+ ... print regsitration.required
+ ... print regsitration.provided, regsitration.name
+ ... print regsitration.factory, regsitration.info
+ ... # doctest: +NORMALIZE_WHITESPACE
+ (<InterfaceClass zope.component.tests.I1>,
+ <InterfaceClass zope.component.tests.I2>)
+ <InterfaceClass zope.component.tests.IA2>
+ zope.component.tests.A12_
+ (<InterfaceClass zope.component.tests.I1>,)
+ <InterfaceClass zope.component.tests.IA2>
+ zope.component.tests.A1_12
+ (<InterfaceClass zope.component.tests.I3>,)
+ <InterfaceClass zope.component.tests.IA2>
+ zope.component.tests.A_2
+ (<implementedBy zope.component.tests.U>,)
+ <InterfaceClass zope.component.tests.IA3>
+ zope.component.tests.A_3 Really class specific
+
+A boolean is returned indicating whether a change was made.
+
+If a factory implements more than one interface, an exception will be
+raised:
+
+ >>> components.unregisterAdapter(tests.A1_12)
+ ... # doctest: +NORMALIZE_WHITESPACE
+ Traceback (most recent call last):
+ ...
+ TypeError: The adapter factory doesn't implement a single
+ interface and no provided interface was specified.
+
+Unless the provided interface is specified:
+
+ >>> components.unregisterAdapter(tests.A1_12, provided=tests.IA2)
+ True
+
+If a factory doesn't declare an implemented interface, an exception will be
+raised:
+
+ >>> components.unregisterAdapter(tests.A12_)
+ ... # doctest: +NORMALIZE_WHITESPACE
+ Traceback (most recent call last):
+ ...
+ TypeError: The adapter factory doesn't implement a single
+ interface and no provided interface was specified.
+
+Unless the provided interface is specified:
+
+ >>> components.unregisterAdapter(tests.A12_, provided=tests.IA2)
+ True
+
+The required interface needs to be specified if the factory doesn't
+have a __component_adapts__ attribute:
+
+ >>> components.unregisterAdapter(tests.A_2)
+ ... # doctest: +NORMALIZE_WHITESPACE
+ Traceback (most recent call last):
+ ...
+ TypeError: The adapter factory doesn't have a __component_adapts__
+ attribute and no required specifications were specified
+
+ >>> components.unregisterAdapter(tests.A_2, required=[tests.I3])
+ True
+
+ >>> for regsitration in sorted(components.registeredAdapters()):
+ ... print regsitration.required
+ ... print regsitration.provided, regsitration.name
+ ... print regsitration.factory, regsitration.info
+ ... # doctest: +NORMALIZE_WHITESPACE
+ (<implementedBy zope.component.tests.U>,)
+ <InterfaceClass zope.component.tests.IA3>
+ zope.component.tests.A_3 Really class specific
+
+If a factory is unregistered that is not registered, False is
+returned:
+
+
+ >>> components.unregisterAdapter(tests.A_2, required=[tests.I3])
+ False
+ >>> components.unregisterAdapter(tests.A12_1, required=[tests.U])
+ False
+
+The factory can be omitted, to unregister *any* factory that matches
+specified required and provided interfaces:
+
+ >>> components.unregisterAdapter(required=[tests.U], provided=tests.IA3)
+ True
+
+ >>> for regsitration in sorted(components.registeredAdapters()):
+ ... print regsitration
+
+Adapters can be named:
+
+ >>> components.registerAdapter(tests.A1_12, provided=tests.IA2,
+ ... name=u'test')
+
+ >>> components.queryMultiAdapter((tests.U1(9), ), tests.IA2)
+ >>> components.queryMultiAdapter((tests.U1(9), ), tests.IA2, name=u'test')
+ A1_12(U1(9))
+
+ >>> components.queryAdapter(tests.U1(9), tests.IA2)
+ >>> components.queryAdapter(tests.U1(9), tests.IA2, name=u'test')
+ A1_12(U1(9))
+ >>> components.getAdapter(tests.U1(9), tests.IA2, name=u'test')
+ A1_12(U1(9))
+
+It is possible to look up all of the adapters that provide an
+interface:
+
+ >>> components.registerAdapter(tests.A1_23, provided=tests.IA2,
+ ... name=u'test 2')
+
+ >>> components.registerAdapter(tests.A1_12, provided=tests.IA2)
+ >>> for name, adapter in sorted(components.getAdapters((tests.U1(9), ),
+ ... tests.IA2)):
+ ... print name, adapter
+ A1_12(U1(9))
+ test A1_12(U1(9))
+ test 2 A1_23(U1(9))
+
+
+getAdapters is most commonly used as the basis of menu systems.
+
+If an adapter factory returns None, it is equivalent to there being no
+factory:
+
+ >>> components.registerAdapter(tests.noop,
+ ... required=[tests.IA1], provided=tests.IA2,
+ ... name=u'test noop')
+ >>> components.queryAdapter(tests.U1(9), tests.IA2, name=u'test noop')
+
+ >>> components.registerAdapter(tests.A1_12, provided=tests.IA2)
+ >>> for name, adapter in sorted(components.getAdapters((tests.U1(9), ),
+ ... tests.IA2)):
+ ... print name, adapter
+ A1_12(U1(9))
+ test A1_12(U1(9))
+ test 2 A1_23(U1(9))
+
+
+ >>> components.unregisterAdapter(tests.A1_12, provided=tests.IA2,
+ ... name=u'test')
+ True
+ >>> components.unregisterAdapter(tests.A1_12, provided=tests.IA2)
+ True
+ >>> for regsitration in sorted(components.registeredAdapters()):
+ ... print regsitration.required
+ ... print regsitration.provided, regsitration.name
+ ... print regsitration.factory, regsitration.info
+ ... # doctest: +NORMALIZE_WHITESPACE
+ (<InterfaceClass zope.component.tests.I1>,)
+ <InterfaceClass zope.component.tests.IA2> test 2
+ zope.component.tests.A1_23
+ (<InterfaceClass zope.component.tests.IA1>,)
+ <InterfaceClass zope.component.tests.IA2> test noop
+ <function noop at 0xb79a1064>
+
+
+Subscribers
+-----------
+
+Subscribers provide a way to get multiple adapters of a given type.
+In this regard, subscribers are like named adapters, except that there
+isn't any concept of the most specific adapter for a given name.
+
+Subscribers are registered by calling registerSubscriptionAdapter:
+
+ >>> components.registerSubscriptionAdapter(tests.A1_2)
+
+ >>> components.registerSubscriptionAdapter(
+ ... tests.A1_12, provided=tests.IA2)
+
+ >>> components.registerSubscriptionAdapter(
+ ... tests.A, [tests.I1], tests.IA2,
+ ... info='a sample comment')
+
+The same rules, with regard to when required and provided interfaces
+have to be specified apply as with adapters:
+
+ >>> components.registerSubscriptionAdapter(tests.A1_12)
+ ... # doctest: +NORMALIZE_WHITESPACE
+ Traceback (most recent call last):
+ ...
+ TypeError: The adapter factory doesn't implement a single
+ interface and no provided interface was specified.
+
+ >>> components.registerSubscriptionAdapter(tests.A)
+ ... # doctest: +NORMALIZE_WHITESPACE
+ Traceback (most recent call last):
+ ...
+ TypeError: The adapter factory doesn't implement a single interface and
+ no provided interface was specified.
+
+ >>> components.registerSubscriptionAdapter(tests.A, required=[tests.IA1])
+ ... # doctest: +NORMALIZE_WHITESPACE
+ Traceback (most recent call last):
+ ...
+ TypeError: The adapter factory doesn't implement a single interface
+ and no provided interface was specified.
+
+Note that we provided the info argument as a keyword argument above.
+That's because there is a name argument that's reserved for future
+use. We can give a name, as long as it is an empty string:
+
+ >>> components.registerSubscriptionAdapter(
+ ... tests.A, [tests.I1], tests.IA2, u'', 'a sample comment')
+
+ >>> components.registerSubscriptionAdapter(
+ ... tests.A, [tests.I1], tests.IA2, u'oops', 'a sample comment')
+ Traceback (most recent call last):
+ ...
+ TypeError: Named subscribers are not yet supported
+
+Subscribers are looked up using the subscribers method:
+
+ >>> for s in components.subscribers((tests.U1(1), ), tests.IA2):
+ ... print s
+ A1_2(U1(1))
+ A1_12(U1(1))
+ A(U1(1),)
+ A(U1(1),)
+
+Note that, because we created multiple subscriptions for A, we got multiple
+subscriber instances.
+
+As with normal adapters, if a factory returns None, the result is skipped:
+
+ >>> components.registerSubscriptionAdapter(
+ ... tests.noop, [tests.I1], tests.IA2)
+
+ >>> for s in components.subscribers((tests.U1(1), ), tests.IA2):
+ ... print s
+ A1_2(U1(1))
+ A1_12(U1(1))
+ A(U1(1),)
+ A(U1(1),)
+
+We can get registration information for subscriptions:
+
+ >>> for registration in sorted(
+ ... components.registeredSubscriptionAdapters()):
+ ... print registration.required
+ ... print registration.provided, registration.name
+ ... print registration.factory, registration.info
+ (<InterfaceClass zope.component.tests.I1>,)
+ <InterfaceClass zope.component.tests.IA2>
+ zope.component.tests.A a sample comment
+ (<InterfaceClass zope.component.tests.I1>,)
+ <InterfaceClass zope.component.tests.IA2>
+ zope.component.tests.A a sample comment
+ (<InterfaceClass zope.component.tests.I1>,)
+ <InterfaceClass zope.component.tests.IA2>
+ zope.component.tests.A1_12
+ (<InterfaceClass zope.component.tests.I1>,)
+ <InterfaceClass zope.component.tests.IA2>
+ zope.component.tests.A1_2
+ (<InterfaceClass zope.component.tests.I1>,)
+ <InterfaceClass zope.component.tests.IA2>
+ <function noop at 0xb796ff7c>
+
+We can also unregister subscriptions in much the same way we can for adapters:
+
+ >>> components.unregisterSubscriptionAdapter(tests.A1_2)
+ True
+
+ >>> for registration in sorted(
+ ... components.registeredSubscriptionAdapters()):
+ ... print registration.required
+ ... print registration.provided, registration.name
+ ... print registration.factory, registration.info
+ (<InterfaceClass zope.component.tests.I1>,)
+ <InterfaceClass zope.component.tests.IA2>
+ zope.component.tests.A a sample comment
+ (<InterfaceClass zope.component.tests.I1>,)
+ <InterfaceClass zope.component.tests.IA2>
+ zope.component.tests.A a sample comment
+ (<InterfaceClass zope.component.tests.I1>,)
+ <InterfaceClass zope.component.tests.IA2>
+ zope.component.tests.A1_12
+ (<InterfaceClass zope.component.tests.I1>,)
+ <InterfaceClass zope.component.tests.IA2>
+ <function noop at 0xb796ff7c>
+
+ >>> components.unregisterSubscriptionAdapter(
+ ... tests.A, [tests.I1], tests.IA2)
+ True
+
+ >>> for registration in sorted(
+ ... components.registeredSubscriptionAdapters()):
+ ... print registration.required
+ ... print registration.provided, registration.name
+ ... print registration.factory, registration.info
+ (<InterfaceClass zope.component.tests.I1>,)
+ <InterfaceClass zope.component.tests.IA2>
+ zope.component.tests.A1_12
+ (<InterfaceClass zope.component.tests.I1>,)
+ <InterfaceClass zope.component.tests.IA2>
+ <function noop at 0xb796ff7c>
+
+Note here that both registrations for A were removed.
+
+If we omit the factory, we must specify the required and provided interfaces:
+
+ >>> components.unregisterSubscriptionAdapter(required=[tests.I1])
+ Traceback (most recent call last):
+ ...
+ TypeError: Must specify one of factory and provided
+
+ >>> components.unregisterSubscriptionAdapter(provided=tests.IA2)
+ Traceback (most recent call last):
+ ...
+ TypeError: Must specify one of factory and required
+
+ >>> components.unregisterSubscriptionAdapter(
+ ... required=[tests.I1], provided=tests.IA2)
+ True
+
+ >>> for registration in sorted(
+ ... components.registeredSubscriptionAdapters()):
+ ... print registration.factory
+
+As when registering, an error is raised if the registration
+information can't be determined from the factory and isn't specified:
+
+ >>> components.unregisterSubscriptionAdapter(tests.A1_12)
+ ... # doctest: +NORMALIZE_WHITESPACE
+ Traceback (most recent call last):
+ ...
+ TypeError: The adapter factory doesn't implement a single
+ interface and no provided interface was specified.
+
+ >>> components.unregisterSubscriptionAdapter(tests.A)
+ ... # doctest: +NORMALIZE_WHITESPACE
+ Traceback (most recent call last):
+ ...
+ TypeError: The adapter factory doesn't implement a single interface and
+ no provided interface was specified.
+
+ >>> components.unregisterSubscriptionAdapter(tests.A, required=[tests.IA1])
+ ... # doctest: +NORMALIZE_WHITESPACE
+ Traceback (most recent call last):
+ ...
+ TypeError: The adapter factory doesn't implement a single interface
+ and no provided interface was specified.
+
+If you unregister something that's not registered, nothing will be changed and False will be returned:
+
+
+ >>> components.unregisterSubscriptionAdapter(
+ ... required=[tests.I1], provided=tests.IA2)
+ False
+
+Handlers
+--------
+
+Handlers are used when you want to perform some function in response
+to an event. Handlers aren't expected to return anything when called
+and are not registered to provide any interface.
+
+ >>> from zope import component
+ >>> @component.adapter(tests.I1)
+ ... def handle1(x):
+ ... print 'handle1', x
+
+ >>> components.registerHandler(handle1, info="First handler")
+ >>> components.handle(tests.U1(1))
+ handle1 U1(1)
+
+ >>> @component.adapter(tests.I1, tests.I2)
+ ... def handle12(x, y):
+ ... print 'handle12', x, y
+
+ >>> components.registerHandler(handle12)
+ >>> components.handle(tests.U1(1), tests.U12(2))
+ handle12 U1(1) U12(2)
+
+If a handler doesn't document interfaces it handles, then
+the required interfaces must be specified:
+
+ >>> def handle(*objects):
+ ... print 'handle', objects
+
+ >>> components.registerHandler(handle)
+ ... # doctest: +NORMALIZE_WHITESPACE
+ Traceback (most recent call last):
+ ...
+ TypeError: The adapter factory doesn't have a __component_adapts__
+ attribute and no required specifications were specified
+
+ >>> components.registerHandler(handle, required=[tests.I1],
+ ... info="a comment")
+
+Handlers can also be registered for classes:
+
+ >>> components.registerHandler(handle, required=[tests.U],
+ ... info="handle a class")
+
+
+ >>> components.handle(tests.U1(1))
+ handle (U1(1),)
+ handle1 U1(1)
+ handle (U1(1),)
+
+We can list the handler registrations:
+
+ >>> for registration in components.registeredHandlers():
+ ... print registration.required
+ ... print registration.handler, registration.info
+ ... # doctest: +NORMALIZE_WHITESPACE
+ (<InterfaceClass zope.component.tests.I1>,)
+ <function handle1 at 0xb78f5bfc> First handler
+ (<InterfaceClass zope.component.tests.I1>,
+ <InterfaceClass zope.component.tests.I2>)
+ <function handle12 at 0xb78f5c34>
+ (<InterfaceClass zope.component.tests.I1>,)
+ <function handle at 0xb78f5ca4> a comment
+ (<implementedBy zope.component.tests.U>,)
+ <function handle at 0xb78f5ca4> handle a class
+
+and we can unregister handlers:
+
+ >>> components.unregisterHandler(required=[tests.U])
+ True
+
+ >>> for registration in components.registeredHandlers():
+ ... print registration.required
+ ... print registration.handler, registration.info
+ ... # doctest: +NORMALIZE_WHITESPACE
+ (<InterfaceClass zope.component.tests.I1>,)
+ <function handle1 at 0xb78f5bfc> First handler
+ (<InterfaceClass zope.component.tests.I1>,
+ <InterfaceClass zope.component.tests.I2>)
+ <function handle12 at 0xb78f5c34>
+ (<InterfaceClass zope.component.tests.I1>,)
+ <function handle at 0xb78f5ca4> a comment
+
+ >>> components.unregisterHandler(handle12)
+ True
+
+ >>> for registration in components.registeredHandlers():
+ ... print registration.required
+ ... print registration.handler, registration.info
+ (<InterfaceClass zope.component.tests.I1>,)
+ <function handle1 at 0xb78f5bfc> First handler
+ (<InterfaceClass zope.component.tests.I1>,)
+ <function handle at 0xb78f5ca4> a comment
+
+ >>> components.unregisterHandler(handle12)
+ False
+
+ >>> components.unregisterHandler()
+ Traceback (most recent call last):
+ ...
+ TypeError: Must specify one of factory and required
+
+ >>> components.registerHandler(handle)
+ ... # doctest: +NORMALIZE_WHITESPACE
+ Traceback (most recent call last):
+ ...
+ TypeError: The adapter factory doesn't have a __component_adapts__
+ attribute and no required specifications were specified
+
+Extending
+---------
+
+Component-management objects can extend other component-management
+objects.
+
+ >>> c1 = Components()
+ >>> c1.__bases__
+ ()
+
+ >>> c2 = Components((c1, ))
+ >>> c2.__bases__ == (c1, )
+ True
+
+ >>> c1.registerUtility(tests.U1(1))
+ >>> c1.queryUtility(tests.I1)
+ U1(1)
+ >>> c2.queryUtility(tests.I1)
+ U1(1)
+ >>> c1.registerUtility(tests.U1(2))
+ >>> c2.queryUtility(tests.I1)
+ U1(2)
+
+We can use multiple inheritence:
+
+ >>> c3 = Components((c1, ))
+ >>> c4 = Components((c2, c3))
+ >>> c4.queryUtility(tests.I1)
+ U1(2)
+
+ >>> c1.registerUtility(tests.U12(1), tests.I2)
+ >>> c4.queryUtility(tests.I2)
+ U12(1)
+
+ >>> c3.registerUtility(tests.U12(3), tests.I2)
+ >>> c4.queryUtility(tests.I2)
+ U12(3)
+
+ >>> c1.registerHandler(handle1, info="First handler")
+ >>> c2.registerHandler(handle, required=[tests.U])
+
+ >>> @component.adapter(tests.I1)
+ ... def handle3(x):
+ ... print 'handle3', x
+ >>> c3.registerHandler(handle3)
+
+ >>> @component.adapter(tests.I1)
+ ... def handle4(x):
+ ... print 'handle4', x
+ >>> c4.registerHandler(handle4)
+
+ >>> c4.handle(tests.U1(1))
+ handle1 U1(1)
+ handle3 U1(1)
+ handle (U1(1),)
+ handle4 U1(1)
Modified: Zope3/branches/jim-adapter/src/zope/component/site.py
===================================================================
--- Zope3/branches/jim-adapter/src/zope/component/site.py 2006-03-12 14:20:53 UTC (rev 65919)
+++ Zope3/branches/jim-adapter/src/zope/component/site.py 2006-03-12 17:52:22 UTC (rev 65920)
@@ -26,10 +26,10 @@
"in the zope.component.globalregistry module.",
DeprecationWarning, stacklevel=1)
-from zope.component.components import Components as SiteManager
-from zope.component.components import AdapterRegistration
-from zope.component.components import SubscriptionRegistration
-from zope.component.components import UtilityRegistration
+from zope.component.registry import Components as SiteManager
+from zope.component.registry import AdapterRegistration
+from zope.component.registry import SubscriptionRegistration
+from zope.component.registry import UtilityRegistration
from zope.component.globalregistry import BaseGlobalComponents \
as GlobalSiteManager
from zope.component.globalregistry import base as globalSiteManager
Modified: Zope3/branches/jim-adapter/src/zope/component/tests.py
===================================================================
--- Zope3/branches/jim-adapter/src/zope/component/tests.py 2006-03-12 14:20:53 UTC (rev 65919)
+++ Zope3/branches/jim-adapter/src/zope/component/tests.py 2006-03-12 17:52:22 UTC (rev 65920)
@@ -801,7 +801,7 @@
setUp=setUp, tearDown=tearDown),
doctest.DocFileSuite('factory.txt',
setUp=setUp, tearDown=tearDown),
- doctest.DocFileSuite('components.txt', checker=checker),
+ doctest.DocFileSuite('registry.txt', checker=checker),
))
if __name__ == "__main__":
More information about the Zope3-Checkins
mailing list