[Zodb-checkins] CVS: Zope3/lib/python/Interface - _InterfaceClass.py:1.9.4.1

Jim Fulton jim@zope.com
Thu, 24 Oct 2002 01:52:36 -0400


Update of /cvs-repository/Zope3/lib/python/Interface
In directory cvs.zope.org:/tmp/cvs-serv23537/lib/python/Interface

Modified Files:
      Tag: Zope3-Bangalore-TTW-Branch
	_InterfaceClass.py 
Log Message:
Made interfaces sortable.  I'd prefer that interfaces be sorted from
mosy specific to most general, but this was harder than I'd hoped, so,
for now, we just sort on name. :/

Added an initial adapter registery API to get registration information
that matches given interface specifications.



=== Zope3/lib/python/Interface/_InterfaceClass.py 1.9 => 1.9.4.1 ===
--- Zope3/lib/python/Interface/_InterfaceClass.py:1.9	Wed Oct  9 18:35:09 2002
+++ Zope3/lib/python/Interface/_InterfaceClass.py	Thu Oct 24 01:52:05 2002
@@ -218,6 +218,61 @@
     def __reduce__(self):
         return self.__name__
 
+    def __cmp(self, o1, o2):
+        """Make interfaces sortable
+
+        It would ne nice if:
+
+           More specific interfaces should sort before less specific ones.
+           Otherwise, sort on name and module.
+
+           But this is too complicated, and we're going to punt on it
+           for now. XXX
+
+        XXX For now, sort on interface and module name.
+
+        None is treated as a psuedo interface that implies the loosest
+        contact possible, no contract. For that reason, all interfaces
+        sort before None.
+        
+        """
+
+        if o1 == o2:
+            return 0
+
+        if o1 is None:
+            return 1
+        if o2 is None:
+            return -1
+
+# XXX first and incorrect stab at ordering more specific interfaces first
+##         if self.extends(other):
+##             return 1
+
+##         if other.extends(self):
+##             return 0
+
+
+
+        n1 = (getattr(o1, '__name__', ''),
+              getattr(getattr(o1,  '__module__', None), '__name__', ''))
+        n2 = (getattr(o2, '__name__', ''),
+              getattr(getattr(o2,  '__module__', None), '__name__', ''))
+        
+        return cmp(n1, n2)
+
+    def __lt__(self, other):
+        c = self.__cmp(self, other)
+        #print '<', self, other, c < 0, c
+        return c < 0
+
+    def __gt__(self, other):
+        c = self.__cmp(self, other)
+        #print '>', self, other, c > 0, c
+        return c > 0
+
+
+
 # We import this here to deal with module dependencies.
 from Implements import getImplementsOfInstances, visitImplements, getImplements
 from Implements import instancesOfObjectImplements