[Zope3-checkins] CVS: Packages3/Interface - Implements.py:1.11

Steve Alexander steve@cat-box.net
Fri, 13 Jun 2003 08:10:13 -0400


Update of /cvs-repository/Packages3/Interface
In directory cvs.zope.org:/tmp/cvs-serv8198

Modified Files:
	Implements.py 
Log Message:
removed dependency on zope.interface.implements


=== Packages3/Interface/Implements.py 1.10 => 1.11 ===
--- Packages3/Interface/Implements.py:1.10	Fri Apr 18 12:17:05 2003
+++ Packages3/Interface/Implements.py	Fri Jun 13 08:10:12 2003
@@ -19,16 +19,67 @@
 
 # Doctor the implements module to know about ExtensionClass 
 import _object
-import zope.interface.implements as implements
+from zope.interface import implements
 implements.ClassTypes = _object.ClassTypes
 del _object, implements
 
-from zope.interface.implements import getImplements
-from zope.interface.implements import getImplementsOfInstances
-from zope.interface.implements import visitImplements
-from zope.interface.implements import assertTypeImplements
-from zope.interface.implements import objectImplements
-from zope.interface.implements import instancesOfObjectImplements
-from zope.interface.implements import _flatten
-from zope.interface.implements import flattenInterfaces
-from zope.interface.implements import implements
+from zope.interface import providedBy as getImplements
+from zope.interface import implementedBy as getImplementsOfInstances
+from zope.interface import classImplements as assertTypeImplements
+from zope.interface import providedBy as objectImplements
+from zope.interface import implementedBy as instancesOfObjectImplements
+
+from zope.interface.declarations import InterfaceSpecification
+def visitImplements(implements, object, visitor):
+    """Call visitor for each interace.
+
+    Visits the interfaces described by an __implements__ attribute,
+    invoking the visitor for each interface object.
+    If the visitor returns anything true, the loop stops.
+    This does not, and should not, visit superinterfaces.
+    """
+
+    for interface in InterfaceSpecification(implements):
+        if visitor(interface):
+            break
+
+from zope.interface.interface import InterfaceClass
+def _flatten(i, append):
+    if InterfaceClass in i.__class__.__mro__:
+        append(i)
+        bases = i.getBases()
+        if bases:
+            for b in bases:
+                _flatten(b, append)
+    else:
+        for iface in i:
+            _flatten(iface, append)
+
+def flattenInterfaces(interfaces, remove_duplicates=1):
+    res = []
+    _flatten(interfaces, res.append)
+    if remove_duplicates:
+        # Remove duplicates in reverse.
+        # Similar to Python 2.2's method resolution order.
+        seen = {}
+        index = len(res) - 1
+        while index >= 0:
+            i = res[index]
+            if i in seen:
+                del res[index]
+            else:
+                seen[i] = 1
+            index = index - 1
+    return res
+
+from zope.interface.verify import verifyClass
+def implements(klass, interface, check=1):
+    if check:
+        verifyClass(interface, klass, tentative=1)
+
+    old = getattr(klass, '__implements__', None)
+    if old is None:
+        klass.__implements__ = interface
+    else:
+        klass.__implements__ = old, interface
+