[Zope3-checkins] CVS: Zope3/src/zope/component -
adapter.py:1.4.28.3 interfaces.py:1.14.6.2
presentation.py:1.1.2.2 service.py:1.5.2.3
Jim Fulton
cvs-admin at zope.org
Tue Nov 18 17:27:18 EST 2003
Update of /cvs-repository/Zope3/src/zope/component
In directory cvs.zope.org:/tmp/cvs-serv19243/src/zope/component
Modified Files:
Tag: adaptergeddon-branch
adapter.py interfaces.py presentation.py service.py
Log Message:
Implemented local presentation services.
=== Zope3/src/zope/component/adapter.py 1.4.28.2 => 1.4.28.3 ===
--- Zope3/src/zope/component/adapter.py:1.4.28.2 Sun Nov 9 11:08:31 2003
+++ Zope3/src/zope/component/adapter.py Tue Nov 18 17:26:39 2003
@@ -19,118 +19,47 @@
from zope.interface import implements
from zope.interface.surrogate import SurrogateRegistry
from zope.component.exceptions import ComponentLookupError
-from zope.component.interfaces import IGlobalAdapterService
+from zope.component.interfaces import IAdapterService
+from zope.component.service import GlobalService
import warnings
-class GlobalAdapterService:
+class IGlobalAdapterService(IAdapterService):
- implements(IGlobalAdapterService)
+ def provideAdapter(required, provided, factories, name='', with=()):
+ """Provide an adapter
+
+ An adapter provides an interface for objects that have another
+ interface.
+
+ Arguments:
+
+ forInterface -- The interface the adapter provides an interface for.
+
+ providedInterface -- The provided interface
+
+ maker -- a sequence of factories that are used to create the adapter.
+ The first factory is called with the object to be adapted, subsequent
+ factories are called with the results of the previous factory.
+ """
+
+ def getRegisteredMatching(required=None,
+ provided=None,
+ name=None,
+ with=None):
+ """Return information about registered data
- def __init__(self):
- self.__adapters = SurrogateRegistry()
+ A five-tuple is returned containing:
+
+ - registered name,
+
+ - registered for interface
+
+ - registered provided interface, and
+
+ - registered data
+ """
+
+class GlobalAdapterService(SurrogateRegistry, GlobalService):
+
+ implements(IGlobalAdapterService)
- def provideAdapter(self,
- forInterface, providedInterface, maker, name=u''):
- """see IGlobalAdapterService interface"""
-
- if not isinstance(maker, (list, tuple)):
- maker = [maker]
- else:
- maker = list(maker)
-
- if not maker == filter(callable, maker):
- raise TypeError("The registered component callable is not "
- "callable")
-
- self.__adapters.provideAdapter(forInterface, providedInterface, maker,
- name)
-
- def getAdapter(self, object, interface, name=u''):
- """see IAdapterService interface"""
- result = self.queryAdapter(object, interface, name=name)
- if result is None:
- raise ComponentLookupError(object, interface)
-
- return result
-
- def getNamedAdapter(self, object, interface, name):
- """see IAdapterService interface"""
- result = self.queryNamedAdapter(object, interface, name)
- if result is None:
- raise ComponentLookupError(object, interface)
-
- return result
-
-
- def queryAdapter(self, object, interface, default=None, name=u''):
- """see IAdapterService interface"""
- if name:
- warnings.warn("The name argument to queryAdapter is deprecated",
- DeprecationWarning, 2)
- return self.queryNamedAdapter(object, interface, name, default)
-
- conform = getattr(object, '__conform__', None)
- if conform is not None:
- try:
- adapter = conform(interface)
- except TypeError:
- # We got a TypeError. It might be an error raised by
- # the __conform__ implementation, or *we* may have
- # made the TypeError by calling an unbound method
- # (object is a class). In the later case, we behave
- # as though there is no __conform__ method. We can
- # detect this case by checking whether there is more
- # than one traceback object in the traceback chain:
- if sys.exc_info()[2].tb_next is not None:
- # There is more than one entry in the chain, so
- # reraise the error:
- raise
- # This clever trick is from Phillip Eby
- else:
- if adapter is not None:
- return adapter
-
- if interface.isImplementedBy(object):
- return object
-
- return self.__adapters.queryAdapter(object, interface, default)
-
- def queryNamedAdapter(self, object, interface, name, default=None):
- """see IAdapterService interface"""
- if name:
- return self.__adapters.queryNamedAdapter(
- object, interface, name, default)
- return self.__adapters.queryAdapter(object, interface, default)
-
-
-
- def getRegisteredMatching(self,
- for_interfaces=None,
- provided_interfaces=None,
- name = None,
- ):
-
- return [(name, required, provided, factories)
- for (required, provided, with, name, factories)
- in self.__adapters.getRegisteredMatching(
- for_interfaces,
- provided_interfaces,
- name)
- ]
-
- _clear = __init__
-
-# the global adapter service instance (see component.zcml )
-adapterService = GlobalAdapterService()
-provideAdapter = adapterService.provideAdapter
-
-_clear = adapterService._clear
-
-# Register our cleanup with Testing.CleanUp to make writing unit tests simpler.
-try:
- from zope.testing.cleanup import addCleanUp
-except ImportError:
- pass
-else:
- addCleanUp(_clear)
- del addCleanUp
=== Zope3/src/zope/component/interfaces.py 1.14.6.1 => 1.14.6.2 ===
--- Zope3/src/zope/component/interfaces.py:1.14.6.1 Sun Nov 9 11:08:31 2003
+++ Zope3/src/zope/component/interfaces.py Tue Nov 18 17:26:39 2003
@@ -365,25 +365,17 @@
class IAdapterService(Interface):
- def getAdapter(object, interface):
- """Get an adapter to an interface for an object
-
- If the object has a __conform__ method, this method will be
- called with the requested interface. If the method returns a
- non-None value, that value will be returned. Otherwise, if the
- object already implements the interface, the object will be
- returned.
+ def queryAdapter(object, interface, default=None):
+ """Look for an adapter to an interface for an object
- If a matching adapter cannot be found, raises
- ComponentLookupError.
+ If a matching adapter cannot be found, returns the default.
"""
- def getNamedAdapter(object, interface, name):
- """Get a named adapter to an interface for an object
+ def queryNamedAdapter(object, interface, name, default=None):
+ """Look for a named adapter to an interface for an object
- If a matching adapter cannot be found, raises
- ComponentLookupError.
+ If a matching adapter cannot be found, returns the default.
The name consisting of an empty string is reserved for unnamed
adapters. The unnamed adapter methods will often call the
@@ -391,20 +383,7 @@
"""
- def queryAdapter(object, interface, default=None):
- """Look for an adapter to an interface for an object
-
- If the object has a __conform__ method, this method will be
- called with the requested interface. If the method returns a
- non-None value, that value will be returned. Otherwise, if the
- object already implements the interface, the object will be
- returned.
-
- If a matching adapter cannot be found, returns the default.
-
- """
-
- def queryNamedAdapter(object, interface, name, default=None):
+ def queryMultiAdapter(objects, 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.
@@ -416,7 +395,7 @@
"""
# XXX need to add name support
- def getRegisteredMatching(for_interfaces=None, provided_interfaces=None):
+ def getRegisteredMatching(required=None, provided=None):
"""Return information about registered data
Zero or more for and provided interfaces may be
@@ -434,39 +413,6 @@
- the object registered specifically for the required and
provided interfaces.
- """
-
-class IGlobalAdapterService(IAdapterService):
-
- def provideAdapter(forInterface, providedInterface, maker, name=''):
- """Provide an adapter
-
- An adapter provides an interface for objects that have another
- interface.
-
- Arguments:
-
- forInterface -- The interface the adapter provides an interface for.
-
- providedInterface -- The provided interface
-
- maker -- a sequence of factories that are used to create the adapter.
- The first factory is called with the object to be adapted, subsequent
- factories are called with the results of the previous factory.
- """
- def getRegisteredMatching(for_interface=None, provide_interface=None,
- name=None):
- """Return information about registered data
-
- A four-tuple is returned containing:
-
- - registered name,
-
- - registered for interface
-
- - registered provided interface, and
-
- - registered data
"""
class IPresentation(Interface):
=== Zope3/src/zope/component/presentation.py 1.1.2.1 => 1.1.2.2 ===
--- Zope3/src/zope/component/presentation.py:1.1.2.1 Mon Nov 10 10:00:42 2003
+++ Zope3/src/zope/component/presentation.py Tue Nov 18 17:26:39 2003
@@ -19,8 +19,13 @@
"""
from zope.component.interfaces import IPresentationService
+from zope.component.servicenames import Presentation
+from zope.component.service import GlobalService
import zope.interface
+from zope.interface.interfaces import IInterface
+import zope.schema
import zope.interface.surrogate
+from types import ClassType
class IGlobalPresentationService(zope.interface.Interface):
@@ -59,148 +64,141 @@
specified one.
"""
-class GlobalPresentationService(object):
+class GlobalPresentationService(GlobalService):
"""Global presentation service
- The global presentation service provides management of views, and
- resources arranged in skins, where skins are ordered collections
- of layers.
+ The global presentation service provides management of views, and
+ resources arranged in skins, where skins are ordered collections
+ of layers.
- Views are modeled as adapters of objects and requests.
- Resources are just request adapters.
+ Views are modeled as adapters of objects and requests.
+ Resources are just request adapters.
- The adapters are arranged in layers.
+ The adapters are arranged in layers.
- Let's look at some examples. First, we'll create a service:
+ Let's look at some examples. First, we'll create a service:
- >>> s = GlobalPresentationService()
+ >>> s = GlobalPresentationService()
- And define a custom layer and skin:
+ And define a custom layer and skin:
- >>> s.defineLayer('custom')
- >>> s.defineSkin('custom', ['custom', 'default'])
+ >>> s.defineLayer('custom')
+ >>> s.defineSkin('custom', ['custom', 'default'])
- We'll define a request type and a fake request:
+ We'll define a request type and a fake request:
- >>> class IRequest(zope.interface.Interface):
- ... "Demonstration request type"
+ >>> class IRequest(zope.interface.Interface):
+ ... "Demonstration request type"
- >>> class Request:
- ... zope.interface.implements(IRequest)
- ... def getPresentationSkin(self):
- ... return getattr(self, 'skin', None)
+ >>> class Request:
+ ... zope.interface.implements(IRequest)
+ ... def getPresentationSkin(self):
+ ... return getattr(self, 'skin', None)
-
- >>> request = Request()
- With this in place, we can start registering resources. A resource
- is just a request adapter.
+ >>> request = Request()
- >>> class MyResource:
- ... def __init__(self, request):
- ... self.request = request
+ With this in place, we can start registering resources. A resource
+ is just a request adapter.
- To register a resource, we register it as an adapter. Most
- resources are going to interface with a user, and, so, don't
- really provide a programatic interface. For this reason, we
- register them to provide the empty interface, Interface, which is
- the default provided interface:
+ >>> class MyResource:
+ ... def __init__(self, request):
+ ... self.request = request
- >>> s.provideAdapter(IRequest, [MyResource], name='foo', layer='custom')
+ To register a resource, we register it as an adapter. Most
+ resources are going to interface with a user, and, so, don't
+ really provide a programatic interface. For this reason, we
+ register them to provide the empty interface, Interface, which is
+ the default provided interface:
- Now we can try to look this up:
+ >>> s.provideAdapter(IRequest, [MyResource], name='foo', layer='custom')
- >>> s.queryResource('foo', request)
+ Now we can try to look this up:
- But we won't get anything, because our request doesn't specify a
- skin and, the default skin gets used. Our resource was registered
- in the custom layer, which isn't used by the default skin. If we
- set out request skin to 'custom':
+ >>> s.queryResource('foo', request)
- >>> request.skin = 'custom'
+ But we won't get anything, because our request doesn't specify a
+ skin and, the default skin gets used. Our resource was registered
+ in the custom layer, which isn't used by the default skin. If we
+ set out request skin to 'custom':
- Then the lookup will suceed:
+ >>> request.skin = 'custom'
- >>> r = s.queryResource('foo', request)
- >>> r.__class__.__name__
- 'MyResource'
- >>> r.request is request
- True
+ Then the lookup will suceed:
- Views are registered as "multi" adapters. Multi-adapters adapt
- multiple objects simultaneously.
+ >>> r = s.queryResource('foo', request)
+ >>> r.__class__.__name__
+ 'MyResource'
+ >>> r.request is request
+ True
- >>> class IContact(zope.interface.Interface):
- ... "Demonstration content type"
+ Views are registered as "multi" adapters. Multi-adapters adapt
+ multiple objects simultaneously.
- >>> class MyView:
- ... def __init__(self, context, request):
- ... self.context, self.request = context, request
+ >>> class IContact(zope.interface.Interface):
+ ... "Demonstration content type"
- >>> s.provideAdapter(IRequest, [MyView], contexts=[IContact], name='foo',
- ... layer='custom')
+ >>> class MyView:
+ ... def __init__(self, context, request):
+ ... self.context, self.request = context, request
- When defining views, we provide one or more (typically 1) context
- interfaces, corresponding to the contexts of the view.
+ >>> s.provideAdapter(IRequest, [MyView], contexts=[IContact],
+ ... name='foo', layer='custom')
- >>> class Contact:
- ... zope.interface.implements(IContact)
+ When defining views, we provide one or more (typically 1) context
+ interfaces, corresponding to the contexts of the view.
- >>> c = Contact()
+ >>> class Contact:
+ ... zope.interface.implements(IContact)
- We look up views with queryView:
+ >>> c = Contact()
- >>> v = s.queryView(c, 'foo', request)
- >>> v.__class__.__name__
- 'MyView'
- >>> v.request is request
- True
- >>> v.context is c
- True
+ We look up views with queryView:
- Most views and resources are unnamed and provide no interface. We
- can also have views that provide interfaces. For example, we
- might need a view to help out with finding objects:
+ >>> v = s.queryView(c, 'foo', request)
+ >>> v.__class__.__name__
+ 'MyView'
+ >>> v.request is request
+ True
+ >>> v.context is c
+ True
- >>> class ITraverse(zope.interface.Interface):
- ... "Sample traversal interface (imagine interesting methods :)"
+ Most views and resources are unnamed and provide no interface. We
+ can also have views that provide interfaces. For example, we
+ might need a view to help out with finding objects:
- >>> class Traverser:
- ... zope.interface.implements(ITraverse)
- ... def __init__(self, context, request):
- ... self.context, self.request = context, request
+ >>> class ITraverse(zope.interface.Interface):
+ ... "Sample traversal interface (imagine interesting methods :)"
- which we register using the provided interface, rather than a name.
+ >>> class Traverser:
+ ... zope.interface.implements(ITraverse)
+ ... def __init__(self, context, request):
+ ... self.context, self.request = context, request
- >>> s.provideAdapter(IRequest, [Traverser], contexts=[IContact],
- ... providing=ITraverse, layer='custom')
+ which we register using the provided interface, rather than a name.
- (We could use a name too, if we wanted to.)
+ >>> s.provideAdapter(IRequest, [Traverser], contexts=[IContact],
+ ... providing=ITraverse, layer='custom')
- Then we look up the view using the interface:
+ (We could use a name too, if we wanted to.)
- >>> v = s.queryView(c, '', request, providing=ITraverse)
- >>> v.__class__.__name__
- 'Traverser'
- >>> v.request is request
- True
- >>> v.context is c
- True
-
+ Then we look up the view using the interface:
- """
+ >>> v = s.queryView(c, '', request, providing=ITraverse)
+ >>> v.__class__.__name__
+ 'Traverser'
+ >>> v.request is request
+ True
+ >>> v.context is c
+ True
+ """
zope.interface.implements(IPresentationService, IGlobalPresentationService)
-
def __init__(self):
- self._layers = {'default':
- zope.interface.surrogate.SurrogateRegistry(),
- }
-
+ self._layers = {'default': GlobalLayer(self, 'default')}
self._skins = {'default': [self._layers['default']]}
- self.skins = [('default', ('default', ))]
-
+ self.skins = {'default': ('default', )}
self.defaultSkin = 'default'
def defineSkin(self, name, layers):
@@ -231,7 +229,9 @@
>>> s.defineLayer('custom')
>>> s.defineSkin('custom', ['custom', 'default'])
- >>> s.skins
+ >>> skins = s.skins.items()
+ >>> skins.sort()
+ >>> skins
[('custom', ('custom', 'default')), ('default', ('default',))]
"""
@@ -243,8 +243,13 @@
raise ValueError, ("Undefined layers", bad)
self._skins[name] = [self._layers[layer] for layer in layers]
- self.skins.append((name, tuple(layers)))
- self.skins.sort()
+ self.skins[name] = tuple(layers)
+
+ def querySkin(self, name):
+ return self.skins.get(name)
+
+ def queryLayer(self, name):
+ return self._layers.get(name)
def setDefaultSkin(self, name):
"""Set the default skin for a request type
@@ -292,7 +297,7 @@
if name in self._layers:
raise ValueError("Can\'t redefine layer", name)
- self._layers[name] = zope.interface.surrogate.SurrogateRegistry()
+ self._layers[name] = GlobalLayer(self, name)
def provideAdapter(self, request_type, factories, name=u'', contexts=(),
providing=zope.interface.Interface, layer='default'):
@@ -302,9 +307,19 @@
resources and views.
"""
+
+ ifaces = []
+ for context in contexts:
+ if not IInterface.isImplementedBy(context) and context is not None:
+ if not isinstance(context, (type, ClassType)):
+ raise TypeError(context, IInterface)
+ context = zope.interface.implementedBy(context)
+
+ ifaces.append(context)
+
+ ifaces.append(request_type)
reg = self._layers[layer]
- ifaces = tuple(contexts) + (request_type, )
reg.provideAdapter(ifaces[0], providing, factories, name, ifaces[1:])
def queryResource(self, name, request, default=None):
@@ -395,3 +410,24 @@
if not isinstance(factory, (list, tuple)):
factory = [factory]
return self.provideAdapter(request_type, factory, name, layer=layer)
+
+ def getRegisteredMatching(self, layers=None, **kw):
+ if layers is None:
+ layers = self._layers.keys()
+ layers.sort()
+ for layername in layers:
+ layer = self._layers[layername]
+
+
+def GL(presentation_service, layer_name):
+ return presentation_service.queryLayer(layer_name)
+
+class GlobalLayer(zope.interface.surrogate.SurrogateRegistry):
+
+ def __init__(self, parent, name):
+ zope.interface.surrogate.SurrogateRegistry.__init__(self)
+ self.__parent__ = parent
+ self.__name__ = name
+
+ def __reduce__(self):
+ return GL, (self.__parent__, self.__name__)
=== Zope3/src/zope/component/service.py 1.5.2.2 => 1.5.2.3 ===
--- Zope3/src/zope/component/service.py:1.5.2.2 Tue Nov 11 12:27:57 2003
+++ Zope3/src/zope/component/service.py Tue Nov 18 17:26:39 2003
@@ -96,6 +96,10 @@
if not self.__defs[name].isImplementedBy(component):
raise InvalidService(name, component, self.__defs[name])
+ if isinstance(component, GlobalService):
+ component.__parent__ = self
+ component.__name__ = name
+
self.__services[name] = component
def getService(self, name):
@@ -110,6 +114,15 @@
"""see IServiceService interface"""
return self.__services.get(name, default)
+
+
+def GS(service_manager, service_name):
+ return service_manager.getService(service_name)
+
+class GlobalService(object):
+
+ def __reduce__(self):
+ return GS, (self.__parent__, self.__name__)
More information about the Zope3-Checkins
mailing list