[Zope-Checkins] SVN: Zope/trunk/ removed duplicate code in ZopeFind and ZopeFindAndApply

Tom Gross cvs-admin at zope.org
Fri Oct 12 15:14:19 UTC 2012


Log message for revision 127979:
  removed duplicate code in ZopeFind and ZopeFindAndApply
  

Changed:
  U   Zope/trunk/doc/CHANGES.rst
  U   Zope/trunk/src/OFS/FindSupport.py
  U   Zope/trunk/src/OFS/tests/testFindSupport.py

-=-
Modified: Zope/trunk/doc/CHANGES.rst
===================================================================
--- Zope/trunk/doc/CHANGES.rst	2012-10-12 09:18:43 UTC (rev 127978)
+++ Zope/trunk/doc/CHANGES.rst	2012-10-12 15:14:15 UTC (rev 127979)
@@ -78,6 +78,8 @@
 Restructuring
 +++++++++++++
 
+- OFS: Removed duplicate code in ZopeFind and ZopeFindAndApply
+
 - Five: Removed obsolete metaclass.
 
 - Five: Refactored ``browser:view`` and ``browser:page`` directives.

Modified: Zope/trunk/src/OFS/FindSupport.py
===================================================================
--- Zope/trunk/src/OFS/FindSupport.py	2012-10-12 09:18:43 UTC (rev 127978)
+++ Zope/trunk/src/OFS/FindSupport.py	2012-10-12 15:14:15 UTC (rev 127979)
@@ -68,89 +68,13 @@
                  search_sub=0,
                  REQUEST=None, result=None, pre=''):
         """Zope Find interface"""
+        return self.ZopeFindAndApply(obj, obj_ids=obj_ids,
+                  obj_metatypes=obj_metatypes, obj_searchterm=obj_searchterm,
+                  obj_expr=obj_expr, obj_mtime=obj_mtime, obj_mspec=obj_mspec,
+                  obj_permission=obj_permission, obj_roles=obj_roles,
+                  search_sub=search_sub, REQUEST=REQUEST, result=result,
+                  pre=pre, apply_func=None, apply_path='')
 
-        if result is None:
-            result=[]
-
-            if obj_metatypes and 'all' in obj_metatypes:
-                obj_metatypes=None
-
-            if obj_mtime and type(obj_mtime)==type('s'):
-                obj_mtime=DateTime(obj_mtime).timeTime()
-
-            if obj_permission:
-                obj_permission=p_name(obj_permission)
-
-            if obj_roles and type(obj_roles) is type('s'):
-                obj_roles=[obj_roles]
-
-            if obj_expr:
-                # Setup expr machinations
-                md=td()
-                obj_expr=(Eval(obj_expr), md, md._push, md._pop)
-
-        base = aq_base(obj)
-
-        if hasattr(base, 'objectItems'):
-            try:    items=obj.objectItems()
-            except: return result
-        else:
-            return result
-
-        try: add_result=result.append
-        except:
-            raise AttributeError, `result`
-
-        for id, ob in items:
-            if pre: p="%s/%s" % (pre, id)
-            else:   p=id
-
-            dflag=0
-            if hasattr(ob, '_p_changed') and (ob._p_changed == None):
-                dflag=1
-
-            bs = aq_base(ob)
-            if (
-                (not obj_ids or absattr(bs.getId()) in obj_ids)
-                and
-                (not obj_metatypes or (hasattr(bs, 'meta_type') and
-                 bs.meta_type in obj_metatypes))
-                and
-                (not obj_searchterm or
-                 (hasattr(ob, 'PrincipiaSearchSource') and
-                  ob.PrincipiaSearchSource().find(str(obj_searchterm)) >= 0
-                  )
-                 or
-                 (hasattr(ob, 'SearchableText') and
-                  ob.SearchableText().find(str(obj_searchterm)) >= 0)
-                 )
-                and
-                (not obj_expr or expr_match(ob, obj_expr))
-                and
-                (not obj_mtime or mtime_match(ob, obj_mtime, obj_mspec))
-                and
-                ( (not obj_permission or not obj_roles) or \
-                   role_match(ob, obj_permission, obj_roles)
-                )
-                ):
-                add_result((p, ob))
-                dflag=0
-
-            if search_sub and (hasattr(bs, 'objectItems')):
-                subob = ob
-                sub_p = p
-                self.ZopeFind(subob, obj_ids, obj_metatypes,
-                                   obj_searchterm, obj_expr,
-                                   obj_mtime, obj_mspec,
-                                   obj_permission, obj_roles,
-                                   search_sub,
-                                   REQUEST, result, sub_p)
-            if dflag: ob._p_deactivate()
-
-        return result
-
-
-
     security.declareProtected(view_management_screens, 'PrincipiaFind')
     PrincipiaFind=ZopeFind
 
@@ -212,8 +136,11 @@
                 and
                 (not obj_searchterm or
                  (hasattr(ob, 'PrincipiaSearchSource') and
-                  ob.PrincipiaSearchSource().find(obj_searchterm) >= 0
-                  ))
+                  obj_searchterm in ob.PrincipiaSearchSource())
+                 or 
+                 (hasattr(ob, 'SearchableText') and
+                  obj_searchterm in ob.SearchableText())
+                 )
                 and
                 (not obj_expr or expr_match(ob, obj_expr))
                 and

Modified: Zope/trunk/src/OFS/tests/testFindSupport.py
===================================================================
--- Zope/trunk/src/OFS/tests/testFindSupport.py	2012-10-12 09:18:43 UTC (rev 127978)
+++ Zope/trunk/src/OFS/tests/testFindSupport.py	2012-10-12 15:14:15 UTC (rev 127979)
@@ -1,16 +1,53 @@
 import unittest
+from OFS.FindSupport import FindSupport
 
 
+class DummyItem(FindSupport):
+    """ """
+
+    def __init__(self, id):
+        self.id = id
+
+    def getId(self):
+        return self.id
+
+
+class DummyFolder(DummyItem, dict):
+
+    def objectItems(self):
+        return self.items()
+
+
 class TestFindSupport(unittest.TestCase):
 
+    def setUp(self):
+        self.base = DummyFolder('base')
+        self.base['1'] = DummyItem('1') 
+        self.base['2'] = DummyItem('2') 
+        self.base['3'] = DummyItem('3') 
+
     def test_interfaces(self):
         from OFS.interfaces import IFindSupport
-        from OFS.FindSupport import FindSupport
         from zope.interface.verify import verifyClass
 
         verifyClass(IFindSupport, FindSupport)
 
+    def test_find(self):
+        self.assertEqual(self.base.ZopeFind(
+            self.base, obj_ids=['1'])[0][0], '1')
 
+    def test_find_apply(self):
+        def func(obj, p):
+            obj.id = 'foo' + obj.id 
+        # ZopeFindAndApply does not return anything
+        # but applies a function to the objects found
+        self.assertFalse(self.base.ZopeFindAndApply(
+            self.base, obj_ids=['2'], apply_func=func))
+
+        self.assertEqual(self.base['1'].id, '1')
+        self.assertEqual(self.base['2'].id, 'foo2')
+        self.assertEqual(self.base['3'].id, '3')
+
 def test_suite():
     return unittest.TestSuite((
         unittest.makeSuite(TestFindSupport),



More information about the Zope-Checkins mailing list