[Zope3-checkins] SVN: Zope3/branches/ZopeX3-3.0/src/z Merged from
trunk r26049:
Jim Fulton
jim at zope.com
Tue Jul 6 14:48:49 EDT 2004
Log message for revision 26118:
Merged from trunk r26049:
Updated adapter API
as described in:
http://mail.zope.org/pipermail/zope3-dev/2004-July/011537.html
- query/getAdapter now require a name argument and ignore
whether the object being adapted provides or conforms to the
interface.
- New functions 'getAdapterInContext' and 'queryAdapterInContext'
can be used to do content-dependent simple adapation. They
will return the object being adapted if it provides the desired
interface and will return the result of calling __conform__ if the
object conforms to the interface.
IMPORTANT
To do simple adaptation, just call the desired interface:
adapter = iface(ob) or iface(ob, default)
-=-
Modified: Zope3/branches/ZopeX3-3.0/src/z3checkins/message.py
===================================================================
--- Zope3/branches/ZopeX3-3.0/src/z3checkins/message.py 2004-07-06 18:43:12 UTC (rev 26117)
+++ Zope3/branches/ZopeX3-3.0/src/z3checkins/message.py 2004-07-06 18:48:48 UTC (rev 26118)
@@ -22,7 +22,7 @@
from zope.app.datetimeutils import parseDatetimetz, DateTimeError
from zope.app.dublincore.interfaces import IZopeDublinCore
from zope.app.pagetemplate import ViewPageTemplateFile
-from zope.component import getUtility, getAdapter, queryAdapter
+from zope.component import getUtility
from zope.component import getView
from zope.exceptions import DuplicationError
from zope.interface import implements
@@ -361,7 +361,7 @@
for message in messages:
try:
self.add(message)
- dc = queryAdapter(message, IZopeDublinCore)
+ dc = IZopeDublinCore(message, None)
if dc is not None:
# XXX should handle RFC-2047
dc.title = unicode(message.subject)
@@ -384,7 +384,7 @@
Title is obtained from Dublin Core metadata of the folder. If it is
empty, "Zope 3 Checkins" is used.
"""
- dc = queryAdapter(self.context, IZopeDublinCore)
+ dc = IZopeDublinCore(self.context, None)
if dc is not None:
title = dc.title
else:
@@ -420,7 +420,7 @@
if int(self.request.get('start', 0)) > 0:
return # The user can't see the newest checkins
if not hasattr(self, '_archive'):
- self._archive = getAdapter(self.context, IMessageArchive)
+ self._archive = IMessageArchive(self.context)
if not self._archive:
return # No messages -- no bookmarks
bookmarks = self.bookmarks()
@@ -443,7 +443,7 @@
if start is None: start = int(self.request.get('start', 0))
if size is None: size = int(self.request.get('size', 20))
if not hasattr(self, '_archive'):
- self._archive = getAdapter(self.context, IMessageArchive)
+ self._archive = IMessageArchive(self.context)
idx = len(self._archive) - start
items = self._archive[max(0, idx-size):idx]
items = removeAllProxies(items)
@@ -491,7 +491,7 @@
def count(self):
"""Returns the number of checkin messages in the archive."""
if not hasattr(self, '_archive'):
- self._archive = getAdapter(self.context, IMessageArchive)
+ self._archive = IMessageArchive(self.context)
return len(self._archive)
@@ -510,8 +510,7 @@
def _calc_index(self):
if not hasattr(self, '_archive'):
container = self.context.__parent__
- self._archive = container and queryAdapter(container,
- IMessageArchive)
+ self._archive = container and IMessageArchive(container, None)
if not self._archive:
self._index = None
elif not hasattr(self, '_index'):
Modified: Zope3/branches/ZopeX3-3.0/src/zope/app/apidoc/viewmodule/__init__.py
===================================================================
--- Zope3/branches/ZopeX3-3.0/src/zope/app/apidoc/viewmodule/__init__.py 2004-07-06 18:43:12 UTC (rev 26117)
+++ Zope3/branches/ZopeX3-3.0/src/zope/app/apidoc/viewmodule/__init__.py 2004-07-06 18:48:48 UTC (rev 26118)
@@ -108,7 +108,7 @@
"""
# Only the global presentation service defines skins
service = zapi.getGlobalService('Presentation')
- skins = [zapi.getAdapter(reg, ISkinDocumentation)
+ skins = [ISkinDocumentation(reg)
for reg in service.registrations()
if isinstance(reg, SkinRegistration)]
skins.sort(lambda x, y: cmp(x.name, y.name))
@@ -174,7 +174,7 @@
Each element of the list is a LayerDocumentation component.
"""
service = zapi.getService('Presentation')
- layers = [zapi.getAdapter(reg, ILayerDocumentation)
+ layers = [ILayerDocumentation(reg)
for reg in service.registrations()
if (isinstance(reg, LayerRegistration) and
reg.layer in self.context.layers)]
@@ -183,7 +183,7 @@
if 'default' in self.context.layers:
default = LayerRegistration('default',
'This is a predefined skin.')
- layers.append(zapi.getAdapter(default, ILayerDocumentation))
+ layers.append(ILayerDocumentation(default))
return layers
# See ISkinDocumentation
Modified: Zope3/branches/ZopeX3-3.0/src/zope/app/component/hooks.py
===================================================================
--- Zope3/branches/ZopeX3-3.0/src/zope/app/component/hooks.py 2004-07-06 18:43:12 UTC (rev 26117)
+++ Zope3/branches/ZopeX3-3.0/src/zope/app/component/hooks.py 2004-07-06 18:48:48 UTC (rev 26118)
@@ -16,7 +16,7 @@
$Id$
"""
-from zope.component import getService, getAdapter
+from zope.component import getService
from zope.component.interfaces import IServiceService
from zope.app.site.interfaces import ISite
from zope.component.service import serviceManager
@@ -64,18 +64,15 @@
return services
+ # Deprecated support for a context that isn't adaptable to
+ # IServiceService. Return the default service manager.
try:
- # This try-except is just backward compatibility really
- return trustedRemoveSecurityProxy(getAdapter(context, IServiceService))
+ return trustedRemoveSecurityProxy(IServiceService(context,
+ serviceManager))
except ComponentLookupError:
- # Deprecated support for a context that isn't adaptable to
- # IServiceService. Return the default service manager.
- ## warnings.warn("getServices' context arg must be None or"
- ## " adaptable to IServiceService.",
- ## DeprecationWarning, warningLevel())
return serviceManager
+
-
def queryView(object, name, request, default=None,
providing=Interface, context=None):
# XXX test
Modified: Zope3/branches/ZopeX3-3.0/src/zope/app/file/browser/image.py
===================================================================
--- Zope3/branches/ZopeX3-3.0/src/zope/app/file/browser/image.py 2004-07-06 18:43:12 UTC (rev 26117)
+++ Zope3/branches/ZopeX3-3.0/src/zope/app/file/browser/image.py 2004-07-06 18:48:48 UTC (rev 26118)
@@ -15,7 +15,6 @@
$Id$
"""
-from zope.app import zapi
from zope.app.size import byteDisplay
from zope.app.size.interfaces import ISized
@@ -87,5 +86,5 @@
"""Image edit view mix-in that provides access to image size info"""
def size(self):
- sized = zapi.getAdapter(self.context, ISized)
+ sized = ISized(self.context)
return sized.sizeForDisplay()
Modified: Zope3/branches/ZopeX3-3.0/src/zope/app/form/browser/add.py
===================================================================
--- Zope3/branches/ZopeX3-3.0/src/zope/app/form/browser/add.py 2004-07-06 18:43:12 UTC (rev 26117)
+++ Zope3/branches/ZopeX3-3.0/src/zope/app/form/browser/add.py 2004-07-06 18:48:48 UTC (rev 26118)
@@ -25,7 +25,6 @@
from zope.app.form.interfaces import IInputWidget, WidgetsError
from zope.app.pagetemplate.simpleviewclass import SimpleViewClass
from zope.app.pagetemplate.viewpagetemplatefile import ViewPageTemplateFile
-from zope.component import getAdapter
from zope.publisher.interfaces.browser import IBrowserRequest
from zope.schema.interfaces import ValidationError
from zope.security.checker import defineChecker, NamesChecker
@@ -87,7 +86,7 @@
kw[str(name)] = data[name]
content = self.create(*args, **kw)
- adapted = getAdapter(content, self.schema)
+ adapted = self.schema(content)
errors = []
@@ -107,7 +106,7 @@
content = self.add(content)
- adapted = getAdapter(content, self.schema)
+ adapted = self.schema(content)
if self._set_after_add:
for name in self._set_after_add:
Modified: Zope3/branches/ZopeX3-3.0/src/zope/app/form/browser/addwizard.py
===================================================================
--- Zope3/branches/ZopeX3-3.0/src/zope/app/form/browser/addwizard.py 2004-07-06 18:43:12 UTC (rev 26117)
+++ Zope3/branches/ZopeX3-3.0/src/zope/app/form/browser/addwizard.py 2004-07-06 18:48:48 UTC (rev 26118)
@@ -24,7 +24,6 @@
from zope.app.form.interfaces import WidgetsError, IInputWidget
from zope.app.pagetemplate.simpleviewclass import SimpleViewClass
from zope.app.pagetemplate.viewpagetemplatefile import ViewPageTemplateFile
-from zope.component import getAdapter
from zope.publisher.interfaces.browser import IBrowserRequest
from zope.schema.interfaces import ValidationError
from zope.security.checker import defineChecker, NamesChecker
@@ -72,7 +71,7 @@
kw[str(name)] = data[name]
content = self.create(*args, **kw)
- adapted = getAdapter(content, self.schema, context=self.context)
+ adapted = self.schema(content)
errors = []
@@ -91,7 +90,7 @@
content = self.context.add(content)
- adapted = getAdapter(content, self.schema)
+ adapted = self.schema(content)
for name in self._set_after_add:
if name in data:
Modified: Zope3/branches/ZopeX3-3.0/src/zope/app/form/browser/editview.py
===================================================================
--- Zope3/branches/ZopeX3-3.0/src/zope/app/form/browser/editview.py 2004-07-06 18:43:12 UTC (rev 26117)
+++ Zope3/branches/ZopeX3-3.0/src/zope/app/form/browser/editview.py 2004-07-06 18:48:48 UTC (rev 26118)
@@ -20,7 +20,6 @@
from zope.schema import getFieldNamesInOrder
from zope.publisher.interfaces.browser import IBrowserRequest
from zope.security.checker import defineChecker, NamesChecker
-from zope.component import getAdapter
from zope.app import zapi
from zope.event import notify
@@ -60,7 +59,7 @@
self._setUpWidgets()
def _setUpWidgets(self):
- adapted = getAdapter(self.context, self.schema)
+ adapted = self.schema(self.context)
if adapted is not self.context:
if not ILocation.providedBy(adapted):
adapted = LocationProxy(adapted)
Modified: Zope3/branches/ZopeX3-3.0/src/zope/app/form/browser/editwizard.py
===================================================================
--- Zope3/branches/ZopeX3-3.0/src/zope/app/form/browser/editwizard.py 2004-07-06 18:43:12 UTC (rev 26117)
+++ Zope3/branches/ZopeX3-3.0/src/zope/app/form/browser/editwizard.py 2004-07-06 18:48:48 UTC (rev 26118)
@@ -16,7 +16,6 @@
$Id$
"""
-from zope.component import getAdapter
from zope.publisher.interfaces.browser import IBrowserRequest
from zope.security.checker import defineChecker, NamesChecker
@@ -63,7 +62,7 @@
class EditWizardView(EditView):
def _setUpWidgets(self):
- adapted = getAdapter(self.context, self.schema)
+ adapted = self.schema(self.context)
if adapted is not self.context:
if not ILocation.providedBy(adapted):
adapted = LocationProxy(adapted)
Modified: Zope3/branches/ZopeX3-3.0/src/zope/app/form/browser/schemadisplay.py
===================================================================
--- Zope3/branches/ZopeX3-3.0/src/zope/app/form/browser/schemadisplay.py 2004-07-06 18:43:12 UTC (rev 26117)
+++ Zope3/branches/ZopeX3-3.0/src/zope/app/form/browser/schemadisplay.py 2004-07-06 18:48:48 UTC (rev 26118)
@@ -24,7 +24,6 @@
from zope.app.location import LocationProxy
from zope.app.publisher.browser import BrowserView
from zope.security.checker import defineChecker, NamesChecker
-from zope.component import getAdapter
from zope.app.form.utility import setUpDisplayWidgets
from zope.app.pagetemplate.viewpagetemplatefile import ViewPageTemplateFile
@@ -49,7 +48,7 @@
self._setUpWidgets()
def _setUpWidgets(self):
- adapted = getAdapter(self.context, self.schema)
+ adapted = self.schema(self.context)
if adapted is not self.context:
if not ILocation.providedBy(adapted):
adapted = LocationProxy(adapted)
Modified: Zope3/branches/ZopeX3-3.0/src/zope/app/form/browser/tests/test_editview.py
===================================================================
--- Zope3/branches/ZopeX3-3.0/src/zope/app/form/browser/tests/test_editview.py 2004-07-06 18:43:12 UTC (rev 26117)
+++ Zope3/branches/ZopeX3-3.0/src/zope/app/form/browser/tests/test_editview.py 2004-07-06 18:48:48 UTC (rev 26118)
@@ -117,7 +117,7 @@
def test_fail_wo_adapter(self):
c = Foo()
request = TestRequest()
- self.assertRaises(ComponentLookupError, EV, c, request)
+ self.assertRaises(TypeError, EV, c, request)
def test_update_no_update(self):
c = C()
Modified: Zope3/branches/ZopeX3-3.0/src/zope/app/fssync/committer.py
===================================================================
--- Zope3/branches/ZopeX3-3.0/src/zope/app/fssync/committer.py 2004-07-06 18:43:12 UTC (rev 26117)
+++ Zope3/branches/ZopeX3-3.0/src/zope/app/fssync/committer.py 2004-07-06 18:48:48 UTC (rev 26118)
@@ -18,7 +18,6 @@
import os
-from zope.component import getService
from zope.configuration.name import resolve
from zope.fssync import fsutil
from zope.fssync.metadata import Metadata
@@ -26,6 +25,7 @@
from zope.proxy import removeAllProxies
from zope.xmlpickle import fromxml
+from zope.app import zapi
from zope.app.fssync import fspickle
from zope.app.container.interfaces import IContainer
from zope.app.container.interfaces import IContainer
@@ -381,8 +381,8 @@
else:
location = context
parent = None
+
# No factory; try using IFileFactory or IDirectoryFactory
- as = getService("Adapters")
isuffix = name.rfind(".")
if isuffix >= 0:
suffix = name[isuffix:]
@@ -394,9 +394,9 @@
else:
iface = IFileFactory
- factory = as.queryAdapter(location, iface, suffix)
+ factory = zapi.queryAdapter(location, iface, suffix)
if factory is None:
- factory = as.queryAdapter(location, iface)
+ factory = iface(location, None)
if iface is IDirectoryFactory:
if factory:
Modified: Zope3/branches/ZopeX3-3.0/src/zope/app/pagetemplate/engine.py
===================================================================
--- Zope3/branches/ZopeX3-3.0/src/zope/app/pagetemplate/engine.py 2004-07-06 18:43:12 UTC (rev 26117)
+++ Zope3/branches/ZopeX3-3.0/src/zope/app/pagetemplate/engine.py 2004-07-06 18:48:48 UTC (rev 26118)
@@ -155,7 +155,7 @@
if namespace is None:
def namespace(object):
try:
- return zapi.getAdapter(object, IPathAdapter, name=name)
+ return zapi.getAdapter(object, IPathAdapter, name)
except ComponentLookupError:
raise KeyError, name
Modified: Zope3/branches/ZopeX3-3.0/src/zope/app/presentation/tests/test_presentation.py
===================================================================
--- Zope3/branches/ZopeX3-3.0/src/zope/app/presentation/tests/test_presentation.py 2004-07-06 18:43:12 UTC (rev 26117)
+++ Zope3/branches/ZopeX3-3.0/src/zope/app/presentation/tests/test_presentation.py 2004-07-06 18:48:48 UTC (rev 26118)
@@ -468,7 +468,7 @@
# Test add event
self.folder['test'] = registration
- dependents = zapi.getAdapter(self.__template, IDependable)
+ dependents = IDependable(self.__template)
self.assert_('test' in dependents.dependents())
def test_registerRemoveSubscriber_template(self):
@@ -482,7 +482,7 @@
# Test remove event
self.folder['test'] = registration
uncontained(registration, self.folder, 'test')
- dependents = zapi.getAdapter(self.__template, IDependable)
+ dependents = IDependable(self.__template)
self.assert_('test' not in dependents.dependents())
def test_addremoveNotify_attribute(self):
Modified: Zope3/branches/ZopeX3-3.0/src/zope/app/security/principalregistry.py
===================================================================
--- Zope3/branches/ZopeX3-3.0/src/zope/app/security/principalregistry.py 2004-07-06 18:43:12 UTC (rev 26117)
+++ Zope3/branches/ZopeX3-3.0/src/zope/app/security/principalregistry.py 2004-07-06 18:48:48 UTC (rev 26118)
@@ -36,7 +36,7 @@
# Methods implementing IAuthenticationService
def authenticate(self, request):
- a = zapi.queryAdapter(request, ILoginPassword)
+ a = ILoginPassword(request, None)
if a is not None:
login = a.getLogin()
if login is not None:
@@ -63,9 +63,8 @@
return self.__defaultObject
def unauthorized(self, id, request):
- # XXX This is a mess. request has no place here!
if id is None or id is self.__defaultid:
- a = zapi.getAdapter(request, ILoginPassword)
+ a = ILoginPassword(request)
a.needLogin(realm="zope")
def getPrincipal(self, id):
Modified: Zope3/branches/ZopeX3-3.0/src/zope/app/traversing/namespace.py
===================================================================
--- Zope3/branches/ZopeX3-3.0/src/zope/app/traversing/namespace.py 2004-07-06 18:43:12 UTC (rev 26117)
+++ Zope3/branches/ZopeX3-3.0/src/zope/app/traversing/namespace.py 2004-07-06 18:48:48 UTC (rev 26118)
@@ -429,6 +429,6 @@
>>> tearDown()
"""
try:
- return component.getAdapter(self.context, IPathAdapter, name=name)
+ return component.getAdapter(self.context, IPathAdapter, name)
except:
raise NotFoundError(self.context, name)
Modified: Zope3/branches/ZopeX3-3.0/src/zope/app/workflow/browser/definition.py
===================================================================
--- Zope3/branches/ZopeX3-3.0/src/zope/app/workflow/browser/definition.py 2004-07-06 18:43:12 UTC (rev 26117)
+++ Zope3/branches/ZopeX3-3.0/src/zope/app/workflow/browser/definition.py 2004-07-06 18:48:48 UTC (rev 26118)
@@ -15,7 +15,7 @@
$Id$
"""
-from zope.app import zapi
+
from zope.app.traversing.api import traverse
from zope.app.registration.interfaces import IRegistered
from zope.app.workflow.interfaces import IProcessDefinitionImportHandler
@@ -33,11 +33,8 @@
def importDefinition(self):
xml = self.request.get('definition')
if xml:
- zapi.getAdapter(
- self.context, IProcessDefinitionImportHandler, context=self
- ).doImport(xml)
+ IProcessDefinitionImportHandler(self.context).doImport(xml)
self.request.response.redirect('@@importexport.html?success=1')
def exportDefinition(self):
- return zapi.getAdapter(self.context, IProcessDefinitionExportHandler,
- context=self).doExport()
+ return IProcessDefinitionExportHandler(self.context).doExport()
Modified: Zope3/branches/ZopeX3-3.0/src/zope/component/__init__.py
===================================================================
--- Zope3/branches/ZopeX3-3.0/src/zope/component/__init__.py 2004-07-06 18:43:12 UTC (rev 26117)
+++ Zope3/branches/ZopeX3-3.0/src/zope/component/__init__.py 2004-07-06 18:48:48 UTC (rev 26118)
@@ -67,12 +67,12 @@
# to avoid the recursion implied by using a local getAdapter call.
# We should be using the line of code below.
- ## return getAdapter(context, IServiceService, context=None)
+ ## return IServiceService(context)
#
# Instead, we need to support code that has passed in an object
# as context, at least until the whole component API is fixed up.
# XXX try ripping this code out.
- sm = queryAdapter(context, IServiceService, context=None)
+ sm = IServiceService(context, None)
if sm is None:
# Deprecated support for a context that isn't adaptable to
# IServiceService. Return the default service manager.
@@ -135,13 +135,13 @@
# Adapter service
-def getAdapter(object, interface, name='', context=None):
- adapter = queryAdapter(object, interface, name, None, context)
+def getAdapterInContext(object, interface, context):
+ adapter = queryAdapterInContext(object, interface, context)
if adapter is None:
raise ComponentLookupError(object, interface)
return adapter
-def queryAdapter(object, interface, name='', default=None, context=None):
+def queryAdapterInContext(object, interface, context, default=None):
conform = getattr(object, '__conform__', None)
if conform is not None:
try:
@@ -167,6 +167,16 @@
return object
adapters = getService(Adapters, context)
+ return adapters.queryAdapter(object, interface, '', default)
+
+def getAdapter(object, interface, name, context=None):
+ adapter = queryAdapter(object, interface, name, None, context)
+ if adapter is None:
+ raise ComponentLookupError(object, interface)
+ return adapter
+
+def queryAdapter(object, interface, name, default=None, context=None):
+ adapters = getService(Adapters, context)
return adapters.queryAdapter(object, interface, name, default)
def interfaceAdapterHook(iface, ob):
Modified: Zope3/branches/ZopeX3-3.0/src/zope/component/interfaces.py
===================================================================
--- Zope3/branches/ZopeX3-3.0/src/zope/component/interfaces.py 2004-07-06 18:43:12 UTC (rev 26117)
+++ Zope3/branches/ZopeX3-3.0/src/zope/component/interfaces.py 2004-07-06 18:48:48 UTC (rev 26118)
@@ -106,8 +106,8 @@
# Adapter service
- def getAdapter(object, interface, context=None):
- """Get an adapter to an interface for an object
+ def getAdapter(object, interface, name, context=''):
+ """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.
@@ -117,7 +117,23 @@
If 'context' is not None, context is adapted to IServiceService,
and this adapter's 'Adapters' service is used.
+ """
+ def getAdapterInContext(object, interface, context):
+ """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.
+
+ Context is adapted to IServiceService, and this adapter's
+ 'Adapters' service is used.
+
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
@@ -142,8 +158,8 @@
named adapter methods with an empty string for a name.
"""
- def queryAdapter(object, interface, default=None, context=None):
- """Look for an adapter to an interface for an object
+ def queryAdapter(object, interface, name, default=None, context=None):
+ """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.
@@ -153,7 +169,23 @@
If 'context' is not None, context is adapted to IServiceService,
and this adapter's 'Adapters' service is used.
+ """
+ def queryAdapterInContext(object, interface, context, default=None):
+ """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.
+
+ Context is adapted to IServiceService, and this adapter's
+ 'Adapters' service is used.
+
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
@@ -451,7 +483,7 @@
class IAdapterService(Interface):
"""A service to manage Adapters."""
- def queryAdapter(object, interface, name='', default=None):
+ def queryAdapter(object, interface, name, default=None):
"""Look for a named adapter to an interface for an object
If a matching adapter cannot be found, returns the default.
@@ -461,7 +493,7 @@
named adapter methods with an empty string for a name.
"""
- def queryMultiAdapter(objects, interface, name='', default=None):
+ def queryMultiAdapter(objects, interface, name, default=None):
"""Look for a multi-adapter to an interface for an object
If a matching adapter cannot be found, returns the default.
Modified: Zope3/branches/ZopeX3-3.0/src/zope/component/tests/test_api.py
===================================================================
--- Zope3/branches/ZopeX3-3.0/src/zope/component/tests/test_api.py 2004-07-06 18:43:12 UTC (rev 26117)
+++ Zope3/branches/ZopeX3-3.0/src/zope/component/tests/test_api.py 2004-07-06 18:48:48 UTC (rev 26118)
@@ -17,6 +17,7 @@
from zope import component
from zope.component import servicenames
from zope.component import getAdapter, queryAdapter
+from zope.component import getAdapterInContext, queryAdapterInContext
from zope.component import getService
from zope.component import getUtility, queryUtility
from zope.component import getDefaultViewName
@@ -152,53 +153,65 @@
##self.assertRaises(ComponentLookupError,
## getService, Adapters, object())
- def testAdapter_via_conform(self):
- ob = Conforming()
+ def testAdapterInContext(self):
+ class I1(Interface):
+ pass
+ class I2(Interface):
+ pass
+ class C:
+ implements(I1)
+ def __conform__(self, iface, default=None):
+ if iface == I2:
+ return 42
+
+ ob = C()
+ servicemanager = StubServiceService()
+ context = ConformsToIServiceService(servicemanager)
+ class I3(Interface):
+ pass
+ class StubAdapterService:
+ def queryAdapter(self, ob, iface, name, default=None):
+ if iface is I3:
+ return 43
+ return default
+ servicemanager.services[Adapters] = StubAdapterService()
+
# If an object implements the interface you want to adapt to,
- # getAdapter should simply return the object.
- self.assertEquals(getAdapter(ob, I1), ob)
+ # getAdapterInContext should simply return the object.
+ self.assertEquals(getAdapterInContext(ob, I1, context), ob)
+ self.assertEquals(queryAdapterInContext(ob, I1, context), ob)
- # If an adapter isn't registered for the given object and interface,
- # and you provide no default, raise ComponentLookupError...
- self.assertRaises(ComponentLookupError, getAdapter, ob, I2)
+ # If an object conforms to the interface you want to adapt to,
+ # getAdapterInContext should simply return the conformed object.
+ self.assertEquals(getAdapterInContext(ob, I2, context), 42)
+ self.assertEquals(queryAdapterInContext(ob, I2, context), 42)
+ class I4(Interface):
+ pass
# If an adapter isn't registered for the given object and interface,
# and you provide no default, raise ComponentLookupError...
- self.assertRaises(ComponentLookupError, getAdapter, Conforming, I2)
+ self.assertRaises(ComponentLookupError,
+ getAdapterInContext, ob, I4, context)
# ...otherwise, you get the default
- self.assertEquals(queryAdapter(ob, I2, '', Test), Test)
+ self.assertEquals(queryAdapterInContext(ob, I4, context, 44), 44)
- # ...otherwise, you get the default
- self.assertEquals(queryAdapter(Conforming, I2, '', Test), Test)
+ # If you ask for an adapter for which something's registered
+ # you get the registered adapter
+ self.assertEquals(getAdapterInContext(ob, I3, context), 43)
+ self.assertEquals(queryAdapterInContext(ob, I3, context), 43)
- # ...otherwise, you get the default
- self.assertEquals(queryAdapter(Conforming, I3, '', Test), Test)
-
- getService(Adapters).register([I1], I2, '', Comp)
- c = getAdapter(ob, I2)
- self.assertEquals(c.__class__, Comp)
- self.assertEquals(c.context, ob)
-
- c = getAdapter(ob, I3)
- self.assertEquals(c.__class__, Comp)
- self.assertEquals(c.context, ob)
-
def testAdapter(self):
- # If an object implements the interface you want to adapt to,
- # getAdapter should simply return the object.
- self.assertEquals(getAdapter(ob, I1), ob)
-
# If an adapter isn't registered for the given object and interface,
# and you provide no default, raise ComponentLookupError...
- self.assertRaises(ComponentLookupError, getAdapter, ob, I2)
+ self.assertRaises(ComponentLookupError, getAdapter, ob, I2, '')
# ...otherwise, you get the default
self.assertEquals(queryAdapter(ob, I2, '', Test), Test)
getService(Adapters).register([I1], I2, '', Comp)
- c = getAdapter(ob, I2)
+ c = getAdapter(ob, I2, '')
self.assertEquals(c.__class__, Comp)
self.assertEquals(c.context, ob)
@@ -208,27 +221,6 @@
self.assertEquals(c.__class__, Comp)
self.assertEquals(c.context, ob)
- def testContextArgument(self):
- # Basically, the same tests as in testAdapter, but with the
- # 'context' argument given. As this is only testing the global
- # service, this is pretty much a no-operation.
-
- self.assertEquals(getAdapter(ob, I1, context=None), ob)
- self.assertEquals(getAdapter(ob, I1, context=ob), ob)
-
- # If an adapter isn't registered for the given object and interface,
- # and you provide no default, raise ComponentLookupError...
- self.assertRaises(ComponentLookupError, getAdapter, ob, I2,
- context=ob)
-
- # ...otherwise, you get the default
- self.assertEquals(queryAdapter(ob, I2, '', Test, context=ob), Test)
-
- getService(Adapters).register([I1], I2, '', Comp)
- c = getAdapter(ob, I2, context=ob)
- self.assertEquals(c.__class__, Comp)
- self.assertEquals(c.context, ob)
-
def testNamedAdapter(self):
self.testAdapter()
@@ -268,7 +260,7 @@
# providing an adapter for None says that your adapter can
# adapt anything to I2.
getService(Adapters).register([None], I2, '', Comp)
- c = getAdapter(ob, I2)
+ c = I2(ob)
self.assertEquals(c.__class__, Comp)
self.assertEquals(c.context, ob)
More information about the Zope3-Checkins
mailing list