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

Jim Fulton jim@zope.com
Mon, 18 Nov 2002 15:41:19 -0500


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

Modified Files:
	_InterfaceClass.py 
Log Message:
Made interfaces orderable (so that lists of interfaces would be sortable).


=== Zope3/lib/python/Interface/_InterfaceClass.py 1.9 => 1.10 ===
--- Zope3/lib/python/Interface/_InterfaceClass.py:1.9	Wed Oct  9 18:35:09 2002
+++ Zope3/lib/python/Interface/_InterfaceClass.py	Mon Nov 18 15:41:18 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