[Checkins] SVN: z3c.componentdebug/trunk/src/z3c/componentdebug/ Assure ordering

Ross Patterson me at rpatterson.net
Wed May 30 22:23:30 EDT 2007


Log message for revision 76029:
  Assure ordering
  

Changed:
  U   z3c.componentdebug/trunk/src/z3c/componentdebug/README.txt
  U   z3c.componentdebug/trunk/src/z3c/componentdebug/component.py
  U   z3c.componentdebug/trunk/src/z3c/componentdebug/interfaces.py
  U   z3c.componentdebug/trunk/src/z3c/componentdebug/tests/component.txt

-=-
Modified: z3c.componentdebug/trunk/src/z3c/componentdebug/README.txt
===================================================================
--- z3c.componentdebug/trunk/src/z3c/componentdebug/README.txt	2007-05-31 00:48:20 UTC (rev 76028)
+++ z3c.componentdebug/trunk/src/z3c/componentdebug/README.txt	2007-05-31 02:23:30 UTC (rev 76029)
@@ -31,16 +31,16 @@
     >>> class IBar(Interface): pass
     >>> class IBaz(Interface): pass
 
-inspectRequiredAdapters
------------------------
-
-inspectRequiredAdapters provides inspection of registrations that
+z3c.componentdebug.inspect provides inspection of registrations that
 provide a specific interface.  Unlike
 zope.app.apidoc.getRequiredAdapters, however, it takes a list of
 objects to adapt and for each object reports which registrations
 require an interface provided by that object for the correct position
 in the list of objects.
 
+Registrations are sorted first by the number of objects that match and
+then by the specificity of the required interfaces.
+
 Start with some objects that provide some interfaces::
 
     >>> from zope.interface import alsoProvides
@@ -60,7 +60,7 @@
     >>> from z3c.componentdebug import inspect
     >>> registrations = inspect((foo, bar), IBaz)
     >>> pprint([i for i in registrations.byObjects()])
-    [(<Foo object at ...>, {}), (<Bar object at ...>, {})]
+    [(<Foo object at ...>, []), (<Bar object at ...>, [])]
 
 Register a factory for this lookup::
 
@@ -74,15 +74,18 @@
     'baz'
     
     >>> registrations = inspect((foo, bar), IBaz)
+    >>> pprint(registrations)
+    [AdapterRegistration(<BaseGlobalComponents base>, [IFoo, IBar],
+    IBaz, '', getBaz, u'')]
     >>> pprint([i for i in registrations.byObjects()])
     [(<Foo object at ...>,
-      {<InterfaceClass __builtin__.IFoo>:
-      [AdapterRegistration(<BaseGlobalComponents base>, [IFoo, IBar],
-      IBaz, '', getBaz, u'')]}),
+      [(<InterfaceClass __builtin__.IFoo>,
+        [AdapterRegistration(<BaseGlobalComponents base>, [IFoo,
+        IBar], IBaz, '', getBaz, u'')])]),
      (<Bar object at ...>,
-      {<InterfaceClass __builtin__.IBar>:
-      [AdapterRegistration(<BaseGlobalComponents base>, [IFoo, IBar],
-      IBaz, '', getBaz, u'')]})]
+      [(<InterfaceClass __builtin__.IBar>,
+        [AdapterRegistration(<BaseGlobalComponents base>, [IFoo,
+        IBar], IBaz, '', getBaz, u'')])])]
 
 When we remove one of the required interfaces, we can see what
 regisration might have otherwise fulfilled the lookup and which object
@@ -94,9 +97,12 @@
     >>> queryMultiAdapter((foo, bar), IBaz)
     
     >>> registrations = inspect((foo, bar), IBaz)
+    >>> pprint(registrations)
+    [AdapterRegistration(<BaseGlobalComponents base>, [IFoo, IBar],
+    IBaz, '', getBaz, u'')]
     >>> pprint([i for i in registrations.byObjects()])
     [(<Foo object at ...>,
-      {<InterfaceClass __builtin__.IFoo>:
-      [AdapterRegistration(<BaseGlobalComponents base>, [IFoo, IBar],
-      IBaz, '', getBaz, u'')]}),
-     (<Bar object at ...>, {})]
+      [(<InterfaceClass __builtin__.IFoo>,
+        [AdapterRegistration(<BaseGlobalComponents base>, [IFoo,
+        IBar], IBaz, '', getBaz, u'')])]),
+     (<Bar object at ...>, [])]

Modified: z3c.componentdebug/trunk/src/z3c/componentdebug/component.py
===================================================================
--- z3c.componentdebug/trunk/src/z3c/componentdebug/component.py	2007-05-31 00:48:20 UTC (rev 76028)
+++ z3c.componentdebug/trunk/src/z3c/componentdebug/component.py	2007-05-31 02:23:30 UTC (rev 76029)
@@ -23,6 +23,22 @@
             for reg in getattr(sm, method)():
                 yield reg
 
+def cmpInterfaces(iface_x, iface_y):
+    if iface_x is iface_y:
+        return 0
+    elif iface_x in iface_y.__iro__:
+        return -1
+    elif iface_y in iface_x.__iro__:
+        return 1
+    else:
+        return 0
+
+def cmpRegistrations(reg_x, reg_y):
+    for idx in xrange(max(len(reg.required) for reg in (reg_x, reg_y))):
+        cmp_ = cmpInterfaces(reg_x.required[idx], reg_y.required[idx])
+        if cmp_:
+            return cmp_
+
 class Registrations(list):
 
     def __init__(self, objects=False, provided=False, name=False,
@@ -45,18 +61,28 @@
             and (objects is False or len(reg.required) == self.order)
             and (name is False or reg.name == name))
 
+        self.sort(cmp=cmpRegistrations, reverse=True)
+        if objects is not False:
+            def byMatches(reg):
+                matches = [
+                    reg.required[idx].providedBy(self.objects[idx])
+                    for idx in xrange(self.order)]
+                return sum(matches), matches
+            self.sort(key=byMatches, reverse=True)
+            
     def byObjects(self):
         assert hasattr(self, 'objects')
         
         idxs = xrange(self.order)
         for idx in idxs:
             object = self.objects[idx]
-            provided = providedBy(object)
-            result = {}
+            by_required = {}
+            ordered = []
             for reg in self:
                 required = reg.required[idx]
-                for prov in provided:
-                    if prov.isOrExtends(required):
-                        result.setdefault(required, []).append(reg)
-                        break
-            yield object, result
+                if required.providedBy(object):
+                    regs = by_required.setdefault(required, [])
+                    regs.append(reg)
+                    if required not in ordered:
+                        ordered.append(required)
+            yield object, [(req, by_required[req]) for req in ordered]

Modified: z3c.componentdebug/trunk/src/z3c/componentdebug/interfaces.py
===================================================================
--- z3c.componentdebug/trunk/src/z3c/componentdebug/interfaces.py	2007-05-31 00:48:20 UTC (rev 76028)
+++ z3c.componentdebug/trunk/src/z3c/componentdebug/interfaces.py	2007-05-31 02:23:30 UTC (rev 76029)
@@ -4,4 +4,7 @@
     """Registrations that qualify for the given lookup."""
 
     def byObjects():
-        """Return the registrations grouped by the objects."""
+        """Registrations grouped by the objects."""
+
+    def byOrder():
+        """Registrations sorted by number of matched objects."""

Modified: z3c.componentdebug/trunk/src/z3c/componentdebug/tests/component.txt
===================================================================
--- z3c.componentdebug/trunk/src/z3c/componentdebug/tests/component.txt	2007-05-31 00:48:20 UTC (rev 76028)
+++ z3c.componentdebug/trunk/src/z3c/componentdebug/tests/component.txt	2007-05-31 02:23:30 UTC (rev 76029)
@@ -28,15 +28,18 @@
     >>> from pprint import pprint
     >>> from z3c.componentdebug import inspect
     >>> registrations = inspect((foo, bar), IBaz)
+    >>> pprint(registrations)
+    [AdapterRegistration(<BaseGlobalComponents base>, [IFoo, IBar],
+    IBaz, '', getBaz, u'')]
     >>> pprint([i for i in registrations.byObjects()])
     [(<Foo object at ...>,
-      {<InterfaceClass __builtin__.IFoo>:
-      [AdapterRegistration(<BaseGlobalComponents base>, [IFoo, IBar],
-      IBaz, '', getBaz, u'')]}),
+      [(<InterfaceClass __builtin__.IFoo>,
+        [AdapterRegistration(<BaseGlobalComponents base>, [IFoo,
+        IBar], IBaz, '', getBaz, u'')])]),
      (<Bar object at ...>,
-      {<InterfaceClass __builtin__.IBar>:
-      [AdapterRegistration(<BaseGlobalComponents base>, [IFoo, IBar],
-      IBaz, '', getBaz, u'')]})]
+      [(<InterfaceClass __builtin__.IBar>,
+        [AdapterRegistration(<BaseGlobalComponents base>, [IFoo,
+        IBar], IBaz, '', getBaz, u'')])])]
 
 Multiple Registrations
 ----------------------
@@ -51,41 +54,103 @@
     'baz2'
     
     >>> registrations = inspect((foo, bar), IBaz)
+    >>> pprint(registrations)
+    [AdapterRegistration(<BaseGlobalComponents base>, [IFoo2, IBar],
+    IBaz, '', getBaz2, u''),
+     AdapterRegistration(<BaseGlobalComponents base>, [IFoo, IBar],
+     IBaz, '', getBaz, u'')]
     >>> pprint([i for i in registrations.byObjects()])
     [(<Foo object at ...>,
-      {<InterfaceClass __builtin__.IFoo>:
-      [AdapterRegistration(<BaseGlobalComponents base>, [IFoo, IBar],
-      IBaz, '', getBaz, u'')],
-       <InterfaceClass __builtin__.IFoo2>:
-       [AdapterRegistration(<BaseGlobalComponents base>, [IFoo2,
-       IBar], IBaz, '', getBaz2, u'')]}),
+      [(<InterfaceClass __builtin__.IFoo2>,
+        [AdapterRegistration(<BaseGlobalComponents base>, [IFoo2,
+        IBar], IBaz, '', getBaz2, u'')]),
+       (<InterfaceClass __builtin__.IFoo>,
+        [AdapterRegistration(<BaseGlobalComponents base>, [IFoo,
+        IBar], IBaz, '', getBaz, u'')])]),
      (<Bar object at ...>,
-      {<InterfaceClass __builtin__.IBar>:
-      [AdapterRegistration(<BaseGlobalComponents base>, [IFoo2, IBar],
-      IBaz, '', getBaz2, u''),
-       AdapterRegistration(<BaseGlobalComponents base>, [IFoo, IBar],
-       IBaz, '', getBaz, u'')]})]
+      [(<InterfaceClass __builtin__.IBar>,
+        [AdapterRegistration(<BaseGlobalComponents base>, [IFoo2,
+        IBar], IBaz, '', getBaz2, u''),
+         AdapterRegistration(<BaseGlobalComponents base>, [IFoo,
+         IBar], IBaz, '', getBaz, u'')])])]
 
     >>> def getBaz3(foo, bar): return 'baz3'
     >>> provideAdapter(getBaz3, (IFoo, Interface), IBaz)
     
     >>> registrations = inspect((foo, bar), IBaz)
+    >>> pprint(registrations)
+    [AdapterRegistration(<BaseGlobalComponents base>, [IFoo2, IBar],
+     IBaz, '', getBaz2, u''),
+     AdapterRegistration(<BaseGlobalComponents base>, [IFoo, IBar],
+     IBaz, '', getBaz, u''),
+     AdapterRegistration(<BaseGlobalComponents base>, [IFoo,
+     Interface], IBaz, '', getBaz3, u'')]
     >>> pprint([i for i in registrations.byObjects()])
     [(<Foo object at ...>,
-      {<InterfaceClass __builtin__.IFoo>:
-      [AdapterRegistration(<BaseGlobalComponents base>, [IFoo, IBar],
-      IBaz, '', getBaz, u''),
-       AdapterRegistration(<BaseGlobalComponents base>, [IFoo,
-       Interface], IBaz, '', getBaz3, u'')],
-       <InterfaceClass __builtin__.IFoo2>:
-       [AdapterRegistration(<BaseGlobalComponents base>, [IFoo2,
-       IBar], IBaz, '', getBaz2, u'')]}),
+      [(<InterfaceClass __builtin__.IFoo2>,
+        [AdapterRegistration(<BaseGlobalComponents base>, [IFoo2,
+        IBar], IBaz, '', getBaz2, u'')]),
+       (<InterfaceClass __builtin__.IFoo>,
+        [AdapterRegistration(<BaseGlobalComponents base>, [IFoo,
+        IBar], IBaz, '', getBaz, u''),
+         AdapterRegistration(<BaseGlobalComponents base>, [IFoo,
+         Interface], IBaz, '', getBaz3, u'')])]),
      (<Bar object at ...>,
-      {<InterfaceClass __builtin__.IBar>:
-      [AdapterRegistration(<BaseGlobalComponents base>, [IFoo2, IBar],
-      IBaz, '', getBaz2, u''),
-       AdapterRegistration(<BaseGlobalComponents base>, [IFoo, IBar],
-       IBaz, '', getBaz, u'')],
-       <InterfaceClass zope.interface.Interface>:
-       [AdapterRegistration(<BaseGlobalComponents base>, [IFoo,
-       Interface], IBaz, '', getBaz3, u'')]})]
+      [(<InterfaceClass __builtin__.IBar>,
+        [AdapterRegistration(<BaseGlobalComponents base>, [IFoo2,
+        IBar], IBaz, '', getBaz2, u''),
+         AdapterRegistration(<BaseGlobalComponents base>, [IFoo,
+         IBar], IBaz, '', getBaz, u'')]),
+       (<InterfaceClass zope.interface.Interface>,
+        [AdapterRegistration(<BaseGlobalComponents base>, [IFoo,
+        Interface], IBaz, '', getBaz3, u'')])])]
+
+If there are registrations that don't match, the sort later::
+
+    >>> from zope.interface import noLongerProvides
+    >>> noLongerProvides(foo, IFoo2)
+    >>> registrations = inspect((foo, bar), IBaz)
+    >>> pprint(registrations)
+    [AdapterRegistration(<BaseGlobalComponents base>, [IFoo, IBar],
+    IBaz, '', getBaz, u''),
+     AdapterRegistration(<BaseGlobalComponents base>, [IFoo,
+     Interface], IBaz, '', getBaz3, u''),
+     AdapterRegistration(<BaseGlobalComponents base>, [IFoo2, IBar],
+     IBaz, '', getBaz2, u'')]
+    >>> pprint([i for i in registrations.byObjects()])
+    [(<Foo object at ...>,
+      [(<InterfaceClass __builtin__.IFoo>,
+        [AdapterRegistration(<BaseGlobalComponents base>, [IFoo,
+        IBar], IBaz, '', getBaz, u''),
+         AdapterRegistration(<BaseGlobalComponents base>, [IFoo,
+         Interface], IBaz, '', getBaz3, u'')])]),
+     (<Bar object at ...>,
+      [(<InterfaceClass __builtin__.IBar>,
+        [AdapterRegistration(<BaseGlobalComponents base>, [IFoo,
+        IBar], IBaz, '', getBaz, u''),
+         AdapterRegistration(<BaseGlobalComponents base>, [IFoo2,
+         IBar], IBaz, '', getBaz2, u'')]),
+       (<InterfaceClass zope.interface.Interface>,
+        [AdapterRegistration(<BaseGlobalComponents base>, [IFoo,
+        Interface], IBaz, '', getBaz3, u'')])])]
+
+    >>> noLongerProvides(bar, IBar)
+    >>> registrations = inspect((foo, bar), IBaz)
+    >>> pprint(registrations)
+    [AdapterRegistration(<BaseGlobalComponents base>, [IFoo,
+    Interface], IBaz, '', getBaz3, u''),
+     AdapterRegistration(<BaseGlobalComponents base>, [IFoo, IBar],
+     IBaz, '', getBaz, u''),
+     AdapterRegistration(<BaseGlobalComponents base>, [IFoo2, IBar],
+     IBaz, '', getBaz2, u'')]
+    >>> pprint([i for i in registrations.byObjects()])
+    [(<Foo object at ...>,
+      [(<InterfaceClass __builtin__.IFoo>,
+        [AdapterRegistration(<BaseGlobalComponents base>, [IFoo,
+        Interface], IBaz, '', getBaz3, u''),
+         AdapterRegistration(<BaseGlobalComponents base>, [IFoo,
+         IBar], IBaz, '', getBaz, u'')])]),
+     (<Bar object at ...>,
+      [(<InterfaceClass zope.interface.Interface>,
+        [AdapterRegistration(<BaseGlobalComponents base>, [IFoo,
+        Interface], IBaz, '', getBaz3, u'')])])]



More information about the Checkins mailing list