[Zodb-checkins] CVS: Zope3/src/zope/interface - adapter.py:1.6.24.3
declarations.py:1.17.10.8 implementor.py:1.6.12.1
interfaces.py:1.15.8.4 surrogate.py:1.1.2.12 type.py:1.10.30.2
Jim Fulton
cvs-admin at zope.org
Tue Nov 18 17:27:21 EST 2003
Update of /cvs-repository/Zope3/src/zope/interface
In directory cvs.zope.org:/tmp/cvs-serv19243/src/zope/interface
Modified Files:
Tag: adaptergeddon-branch
adapter.py declarations.py implementor.py interfaces.py
surrogate.py type.py
Log Message:
Implemented local presentation services.
=== Zope3/src/zope/interface/adapter.py 1.6.24.2 => 1.6.24.3 ===
--- Zope3/src/zope/interface/adapter.py:1.6.24.2 Sat Oct 11 11:14:24 2003
+++ Zope3/src/zope/interface/adapter.py Tue Nov 18 17:26:41 2003
@@ -33,8 +33,7 @@
# The implementation uses a mapping:
#
- # { (required_interface, provided_interface) ->
- # (registered_provides, component) }
+ # { (required, provided) -> (registered_provided, component) }
#
# Where the registered provides is what was registered and
# provided may be some base interface
@@ -153,9 +152,12 @@
return None
def getRegisteredMatching(self,
- required_interfaces=None,
- provided_interfaces=None):
+ required=None,
+ provided=None):
+
+ required_interfaces = required
+ provided_interfaces = provided
if IInterface.isImplementedBy(required_interfaces):
required_interfaces = (required_interfaces, )
=== Zope3/src/zope/interface/declarations.py 1.17.10.7 => 1.17.10.8 ===
--- Zope3/src/zope/interface/declarations.py:1.17.10.7 Thu Nov 6 07:35:10 2003
+++ Zope3/src/zope/interface/declarations.py Tue Nov 18 17:26:41 2003
@@ -258,6 +258,12 @@
class Implements(Declaration):
inherit = None
declared = ()
+ __name__ = '?'
+
+ def __repr__(self):
+ return '<implementedBy %s>' % (self.__name__)
+
+
def implementedByFallback(cls):
"""Return the interfaces implemented for a class' instances
@@ -329,6 +335,8 @@
spec = Implements(
*[implementedBy(c) for c in cls.__bases__])
spec.inherit = cls
+
+ spec.__name__ = getattr(cls, '__module__', '?') + '.' + cls.__name__
try:
cls.__implements__ = spec
=== Zope3/src/zope/interface/implementor.py 1.6 => 1.6.12.1 ===
--- Zope3/src/zope/interface/implementor.py:1.6 Wed Aug 6 17:16:58 2003
+++ Zope3/src/zope/interface/implementor.py Tue Nov 18 17:26:41 2003
@@ -31,7 +31,7 @@
# The implementation uses a mapping:
#
- # { provided_interface -> (registered_provides, component) }
+ # { provided -> (registered_provided, component) }
#
# Where the registered provides is what was registered and
# provided may be some base interface
=== Zope3/src/zope/interface/interfaces.py 1.15.8.3 => 1.15.8.4 ===
--- Zope3/src/zope/interface/interfaces.py:1.15.8.3 Sun Nov 9 11:08:33 2003
+++ Zope3/src/zope/interface/interfaces.py Tue Nov 18 17:26:41 2003
@@ -377,8 +377,8 @@
None is returned if nothing is registered.
"""
- def getRegisteredMatching(required_interfaces=None,
- provided_interfaces=None):
+ def getRegisteredMatching(required=None,
+ provided=None):
"""Return information about registered data
Zero or more required and provided interfaces may be
=== Zope3/src/zope/interface/surrogate.py 1.1.2.11 => 1.1.2.12 ===
--- Zope3/src/zope/interface/surrogate.py:1.1.2.11 Thu Nov 13 12:33:00 2003
+++ Zope3/src/zope/interface/surrogate.py Tue Nov 18 17:26:41 2003
@@ -167,7 +167,7 @@
except KeyError:
pass
else:
- self.adapters[with, name, specification] = factories
+ self.adapters[tuple(with), name, specification] = factories
self.dirty()
@@ -189,8 +189,10 @@
_surrogateClass = Surrogate
def __init__(self):
- self._surrogates = surrogates = {}
- self._default = self._surrogateClass(Default, self)
+ default = self._surrogateClass(Default, self)
+ self._default = default
+ surrogates = {Default.weakref(): default}
+ self._surrogates = surrogates
def _remove(k):
try:
@@ -354,9 +356,10 @@
declaration = providedBy(ob)
s = self.get(declaration)
- factories = s.get((interface, name))
+ key = name and (interface, name) or interface
+ factories = s.get(key)
if factories is None:
- factories = self._default.get((interface, name))
+ factories = self._default.get(key)
if factories is not None:
if raw:
return factories
@@ -479,12 +482,26 @@
surrogate = self._default
def getRegisteredMatching(self,
- required_interfaces=None,
- provided_interfaces=None,
- name = None,
+ required=None,
+ provided=None,
+ name=None,
+ with=None,
):
"""Search for registered adapters
+ Return a 5-tuple with:
+
+ - (first) required interface
+
+ - provided interface
+
+ - a tuple of additional required interfaces (for multi-adapters)
+
+ - name, and
+
+ - a sequence of factories. (Note that this could be arbitrary data).
+
+
Note, this is usually slow!
>>> from zope.interface import Interface
@@ -535,65 +552,65 @@
('Interface', 'P3', (), u'', 'any P3'),
('R2', 'P3', (), u'', 'R2 P3')]
- >>> sorted(registry.getRegisteredMatching(required_interfaces=[R1]))
+ >>> sorted(registry.getRegisteredMatching(required=[R1]))
[(None, 'P3', (), u'', 'default P3'),
('Interface', 'P3', (), u'', 'any P3')]
- >>> sorted(registry.getRegisteredMatching(required_interfaces=R1))
+ >>> sorted(registry.getRegisteredMatching(required=R1))
[(None, 'P3', (), u'', 'default P3'),
('Interface', 'P3', (), u'', 'any P3')]
- >>> sorted(registry.getRegisteredMatching(provided_interfaces=[P1]))
+ >>> sorted(registry.getRegisteredMatching(provided=[P1]))
[(None, 'P3', (), u'', 'default P3'),
('Interface', 'P3', (), u'', 'any P3'),
('R2', 'P3', (), u'', 'R2 P3'),
('R2', 'P3', (), u'bob', 'bobs R2 P3')]
- >>> sorted(registry.getRegisteredMatching(provided_interfaces=P1))
+ >>> sorted(registry.getRegisteredMatching(provided=P1))
[(None, 'P3', (), u'', 'default P3'),
('Interface', 'P3', (), u'', 'any P3'),
('R2', 'P3', (), u'', 'R2 P3'),
('R2', 'P3', (), u'bob', 'bobs R2 P3')]
- >>> sorted(registry.getRegisteredMatching(provided_interfaces=P3))
+ >>> sorted(registry.getRegisteredMatching(provided=P3))
[(None, 'P3', (), u'', 'default P3'),
('Interface', 'P3', (), u'', 'any P3'),
('R2', 'P3', (), u'', 'R2 P3'),
('R2', 'P3', (), u'bob', 'bobs R2 P3')]
>>> sorted(registry.getRegisteredMatching(
- ... required_interfaces = (R4, R12),
- ... provided_interfaces = (P1, )))
+ ... required = (R4, R12),
+ ... provided = (P1, )))
[(None, 'P3', (), u'', 'default P3'),
('Interface', 'P3', (), u'', 'any P3'),
('R2', 'P3', (), u'', 'R2 P3'),
('R2', 'P3', (), u'bob', 'bobs R2 P3')]
>>> sorted(registry.getRegisteredMatching(
- ... required_interfaces = (R4, R12),
- ... provided_interfaces = (P3, )))
+ ... required = (R4, R12),
+ ... provided = (P3, )))
[(None, 'P3', (), u'', 'default P3'),
('Interface', 'P3', (), u'', 'any P3'),
('R2', 'P3', (), u'', 'R2 P3'),
('R2', 'P3', (), u'bob', 'bobs R2 P3')]
>>> sorted(registry.getRegisteredMatching(
- ... required_interfaces = (R2, ),
- ... provided_interfaces = (P3, )))
+ ... required = (R2, ),
+ ... provided = (P3, )))
[(None, 'P3', (), u'', 'default P3'),
('Interface', 'P3', (), u'', 'any P3'),
('R2', 'P3', (), u'', 'R2 P3'),
('R2', 'P3', (), u'bob', 'bobs R2 P3')]
>>> sorted(registry.getRegisteredMatching(
- ... required_interfaces = (R2, ),
- ... provided_interfaces = (P3, ),
+ ... required = (R2, ),
+ ... provided = (P3, ),
... name='bob'))
[('R2', 'P3', (), u'bob', 'bobs R2 P3')]
>>> sorted(registry.getRegisteredMatching(
- ... required_interfaces = (R3, ),
- ... provided_interfaces = (P1, ),
+ ... required = (R3, ),
+ ... provided = (P1, ),
... name='bob'))
[('R2', 'P3', (), u'bob', 'bobs R2 P3')]
@@ -602,21 +619,21 @@
if name is not None:
name = unicode(name)
- if isinstance(required_interfaces, InterfaceClass):
- required_interfaces = (required_interfaces, )
- elif required_interfaces is None:
- required_interfaces = [ref() for ref in self._surrogates.keys()
+ if isinstance(required, InterfaceClass):
+ required = (required, )
+ elif required is None:
+ required = [ref() for ref in self._surrogates.keys()
if ref() is not None]
- required_interfaces = tuple(required_interfaces)+(None,)
+ required = tuple(required)+(None,)
- if isinstance(provided_interfaces, InterfaceClass):
- provided_interfaces = (provided_interfaces, )
+ if isinstance(provided, InterfaceClass):
+ provided = (provided, )
seen = {}
- for required in required_interfaces:
+ for required in required:
s = self.get(required)
for ancestor in ro(s):
if ancestor in seen:
@@ -628,23 +645,31 @@
ancestor = ancestor.spec()
if ancestor is Default:
ancestor = None
- for (with, aname, target), factories in items:
- if with:
- continue # don't do multi-adapters yet
+ for (rwith, aname, target), factories in items:
+ if with is not None and not mextends(with, rwith):
+ continue
if name is not None and aname != name:
continue
- if provided_interfaces:
- for p in provided_interfaces:
+ if provided:
+ for p in provided:
if target.extends(p, False):
break
else:
# None matching
continue
- yield (ancestor, target, with, aname, factories)
-
+ yield (ancestor, target, rwith, aname, factories)
+def mextends(with, rwith):
+ if len(with) == len(rwith):
+ for w, r in zip(with, rwith):
+ if not w.isOrExtends(r):
+ break
+ else:
+ return True
+ return False
+
def adapterImplied(adapters):
implied = {}
=== Zope3/src/zope/interface/type.py 1.10.30.1 => 1.10.30.2 ===
--- Zope3/src/zope/interface/type.py:1.10.30.1 Sun Nov 9 11:08:33 2003
+++ Zope3/src/zope/interface/type.py Tue Nov 18 17:26:41 2003
@@ -19,6 +19,8 @@
"""
__metaclass__ = type # All classes are new style when run with Python 2.2+
+import zope.interface
+import types
from zope.interface import providedBy, implements
from zope.interface.interfaces import IInterface
from zope.interface.interfaces import ITypeRegistry
@@ -33,8 +35,7 @@
# The implementation uses a mapping:
#
- # { (required_interface, provided_interface) ->
- # (registered_provides, component) }
+ # { (required, provided) -> (registered_provided, component) }
#
# Where the registered provides is what was registered and
# provided may be some base interface
@@ -46,12 +47,15 @@
self._reg = data
def register(self, interface, object):
- if interface is None or IInterface.isImplementedBy(interface):
- self._reg[interface] = object
- else:
- raise TypeError(
- "The interface argument must be an interface (or None)")
-
+ if not (interface is None or IInterface.isImplementedBy(interface)):
+ if isinstance(interface, (type, types.ClassType)):
+ interface = zope.interface.implementedBy(interface)
+ else:
+ raise TypeError(
+ "The interface argument must be an interface (or None)")
+
+ self._reg[interface] = object
+
def unregister(self, interface):
if interface is None or IInterface.isImplementedBy(interface):
if interface in self._reg:
More information about the Zodb-checkins
mailing list