[Zope3-checkins] SVN: Zope3/trunk/src/zope/app/container/ Add convinience object filter: filter objects on provided interfaces

Martijn Pieters mj at zopatista.com
Thu Jun 1 08:54:43 EDT 2006


Log message for revision 68442:
  Add convinience object filter: filter objects on provided interfaces

Changed:
  U   Zope3/trunk/src/zope/app/container/find.py
  U   Zope3/trunk/src/zope/app/container/tests/test_find.py

-=-
Modified: Zope3/trunk/src/zope/app/container/find.py
===================================================================
--- Zope3/trunk/src/zope/app/container/find.py	2006-06-01 12:18:11 UTC (rev 68441)
+++ Zope3/trunk/src/zope/app/container/find.py	2006-06-01 12:54:41 UTC (rev 68442)
@@ -18,7 +18,7 @@
 __docformat__ = 'restructuredtext'
 
 from zope.interface import implements
-from interfaces import IFind, IIdFindFilter
+from interfaces import IFind, IIdFindFilter, IObjectFindFilter
 from interfaces import IReadContainer
 
 class FindAdapter(object):
@@ -74,3 +74,16 @@
     def matches(self, id):
         'See INameFindFilter'
         return id in self._ids
+    
+class SimpleInterfacesFindFilter(object):
+    """Filter objects on the provided interfaces"""
+    implements(IObjectFindFilter)
+    
+    def __init__(self, *interfaces):
+        self.interfaces = interfaces
+    
+    def matches(self, object):
+        for iface in self.interfaces:
+            if iface.providedBy(object):
+                return True
+        return False

Modified: Zope3/trunk/src/zope/app/container/tests/test_find.py
===================================================================
--- Zope3/trunk/src/zope/app/container/tests/test_find.py	2006-06-01 12:18:11 UTC (rev 68441)
+++ Zope3/trunk/src/zope/app/container/tests/test_find.py	2006-06-01 12:54:41 UTC (rev 68442)
@@ -19,7 +19,8 @@
 from zope.app.container.interfaces import IReadContainer
 from zope.app.container.interfaces import IObjectFindFilter
 from zope.app.container.find import FindAdapter, SimpleIdFindFilter
-from zope.interface import implements
+from zope.app.container.find import SimpleInterfacesFindFilter
+from zope.interface import implements, Interface, directlyProvides
 
 class FakeContainer(object):
     implements(IReadContainer)
@@ -58,6 +59,15 @@
 
     def __len__(self):
         return len(self._objects)
+    
+class FakeInterfaceFoo(Interface):
+    """Test interface Foo"""
+    
+class FakeInterfaceBar(Interface):
+    """Test interface Bar"""
+    
+class FakeInterfaceSpam(Interface):
+    """Test interface Spam"""
 
 class TestObjectFindFilter(object):
     implements(IObjectFindFilter)
@@ -140,6 +150,22 @@
         result = find.find(id_filters=[SimpleIdFindFilter(['alpha'])],
                            object_filters=[TestObjectFindFilter(1)])
         self.assertEquals([], result)
+        
+    def test_interfaceFind(self):
+        alpha = FakeContainer('alpha', [])
+        directlyProvides(alpha, FakeInterfaceBar)
+        delta = FakeContainer('delta', [])
+        directlyProvides(delta, FakeInterfaceFoo)
+        beta = FakeContainer('beta', [delta])
+        directlyProvides(beta, FakeInterfaceSpam)
+        gamma = FakeContainer('gamma', [])
+        tree = FakeContainer(
+            'tree',
+            [alpha, beta, gamma])
+        find = FindAdapter(tree)
+        result = find.find(object_filters=[
+            SimpleInterfacesFindFilter(FakeInterfaceFoo, FakeInterfaceSpam)])
+        self.assertEqual([beta, delta], result)
 
 def test_suite():
     return makeSuite(Test)



More information about the Zope3-Checkins mailing list