[Checkins] SVN: z3c.contents/trunk/src/z3c/contents/ Move existing code from search to value before refactoring that part.

Roger Ineichen roger at projekt01.ch
Sat Apr 12 18:25:52 EDT 2008


Log message for revision 85294:
  Move existing code from search to value before refactoring that part.
  It smells that this implementation will become very simple at the end.

Changed:
  U   z3c.contents/trunk/src/z3c/contents/README.txt
  U   z3c.contents/trunk/src/z3c/contents/browser.py
  U   z3c.contents/trunk/src/z3c/contents/configure.zcml
  D   z3c.contents/trunk/src/z3c/contents/search.py
  U   z3c.contents/trunk/src/z3c/contents/tests.py
  U   z3c.contents/trunk/src/z3c/contents/value.py

-=-
Modified: z3c.contents/trunk/src/z3c/contents/README.txt
===================================================================
--- z3c.contents/trunk/src/z3c/contents/README.txt	2008-04-12 22:15:24 UTC (rev 85293)
+++ z3c.contents/trunk/src/z3c/contents/README.txt	2008-04-12 22:25:52 UTC (rev 85294)
@@ -842,7 +842,7 @@
 Add an IFind adapter for the search:
 
   >>> from z3c.contents.interfaces import ISearch
-  >>> from z3c.contents.search import SearchForContainer
+  >>> from z3c.contents.value import SearchForContainer
   >>> zope.component.provideAdapter(SearchForContainer)
 
 And also register ISearchableText adapter for the contained objects, the default

Modified: z3c.contents/trunk/src/z3c/contents/browser.py
===================================================================
--- z3c.contents/trunk/src/z3c/contents/browser.py	2008-04-12 22:15:24 UTC (rev 85293)
+++ z3c.contents/trunk/src/z3c/contents/browser.py	2008-04-12 22:25:52 UTC (rev 85294)
@@ -44,24 +44,8 @@
 _ = zope.i18nmessageid.MessageFactory('z3c')
 
 
-def queryPrincipalClipboard(request):
-    """Return the clipboard based on the request."""
-    user = request.principal
-    annotations = IAnnotations(user, None)
-    if annotations is None:
-        return None
-    return IPrincipalClipboard(annotations, None)
-
-
-def safeGetAttr(obj, attr, default):
-    """Attempts to read the attr, returning default if Unauthorized."""
-    try:
-        return getattr(obj, attr, default)
-    except Unauthorized:
-        return default
-
-
 class ContentsSearchForm(form.Form):
+    """Search form for IContentsPage."""
 
     template = getPageTemplate()
     fields = field.Fields(field.Field(
@@ -82,6 +66,23 @@
         self.table.searchterm = data.get('searchterm', '')
 
 
+def queryPrincipalClipboard(request):
+    """Return the clipboard based on the request."""
+    user = request.principal
+    annotations = IAnnotations(user, None)
+    if annotations is None:
+        return None
+    return IPrincipalClipboard(annotations, None)
+
+
+def safeGetAttr(obj, attr, default):
+    """Attempts to read the attr, returning default if Unauthorized."""
+    try:
+        return getattr(obj, attr, default)
+    except Unauthorized:
+        return default
+
+
 # conditions
 def canCut(form):
     return form.supportsCut

Modified: z3c.contents/trunk/src/z3c/contents/configure.zcml
===================================================================
--- z3c.contents/trunk/src/z3c/contents/configure.zcml	2008-04-12 22:15:24 UTC (rev 85293)
+++ z3c.contents/trunk/src/z3c/contents/configure.zcml	2008-04-12 22:25:52 UTC (rev 85294)
@@ -44,7 +44,7 @@
       />
 
   <adapter
-      factory="z3c.contents.search.SearchForContainer"
+      factory="z3c.contents.value.SearchForContainer"
       />
 
   <!-- include also sorting column headers for some columns

Deleted: z3c.contents/trunk/src/z3c/contents/search.py
===================================================================
--- z3c.contents/trunk/src/z3c/contents/search.py	2008-04-12 22:15:24 UTC (rev 85293)
+++ z3c.contents/trunk/src/z3c/contents/search.py	2008-04-12 22:25:52 UTC (rev 85294)
@@ -1,90 +0,0 @@
-##############################################################################
-#
-# Copyright (c) 2008 Zope Foundation and Contributors.
-# All Rights Reserved.
-#
-# This software is subject to the provisions of the Zope Public License,
-# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
-# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
-# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
-# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
-# FOR A PARTICULAR PURPOSE.
-#
-##############################################################################
-"""
-$Id$
-"""
-__docformat__ = "reStructuredText"
-
-import zope.interface
-import zope.component
-from zope.app.container.interfaces import IObjectFindFilter
-from zope.app.container.interfaces import IReadContainer
-from zope.security.proxy import removeSecurityProxy
-from zope.index.text.interfaces import ISearchableText
-
-from z3c.contents import interfaces
-
-
-class SearchForContainer(object):
-
-    zope.interface.implements(interfaces.ISearch)
-    zope.component.adapts(IReadContainer)
-
-    def __init__(self, context):
-        self.context = context
-
-    def search(self, id_filters=None, object_filters=None):
-        'See ISearch'
-        id_filters = id_filters or []
-        object_filters = object_filters or []
-        result = []
-        for id, object in self.context.items():
-            _search_helper(id, object, self.context, id_filters, object_filters,
-                result)
-        return result
-
-
-def _search_helper(id, object, container, id_filters, object_filters, result):
-    # check id filters if we get a match then return immediately
-    for id_filter in id_filters:
-        if id_filter.matches(id):
-            result.append(object)
-            return
-
-    # now check all object filters
-    for object_filter in object_filters:
-        if object_filter.matches(object):
-            result.append(object)
-            return
-
-    # do we need to check sub containers?
-    if not IReadContainer.providedBy(object):
-        return
-
-    container = object
-    for id, object in container.items():
-        _search_helper(id, object, container, id_filters, object_filters, result)
-
-
-class SearchableTextFindFilter(object):
-    """Filter objects on the ISearchableText adapters to the object."""
-
-    zope.interface.implements(IObjectFindFilter)
-    
-    def __init__(self, terms):
-        self.terms = terms
-    
-    def matches(self, object):
-        """Check if one of the search terms is found in the searchable text
-        interface.
-        """
-
-        adapter = zope.component.queryAdapter(object, ISearchableText)
-        if not adapter:
-            return False
-        searchable = adapter.getSearchableText().lower()
-        for term in [t.lower() for t in self.terms]:
-            if term in searchable:
-                return True
-        return False

Modified: z3c.contents/trunk/src/z3c/contents/tests.py
===================================================================
--- z3c.contents/trunk/src/z3c/contents/tests.py	2008-04-12 22:15:24 UTC (rev 85293)
+++ z3c.contents/trunk/src/z3c/contents/tests.py	2008-04-12 22:25:52 UTC (rev 85294)
@@ -35,7 +35,6 @@
 
 from z3c.macro import tales
 import z3c.table.testing
-import z3c.contents.search
 import z3c.contents.value
 
 

Modified: z3c.contents/trunk/src/z3c/contents/value.py
===================================================================
--- z3c.contents/trunk/src/z3c/contents/value.py	2008-04-12 22:15:24 UTC (rev 85293)
+++ z3c.contents/trunk/src/z3c/contents/value.py	2008-04-12 22:25:52 UTC (rev 85294)
@@ -16,21 +16,88 @@
 """
 __docformat__ = "reStructuredText"
 
+import zope.interface
 import zope.component
+from zope.index.text.interfaces import ISearchableText
 from zope.publisher.interfaces.browser import IBrowserRequest
 from zope.app.container.interfaces import IReadContainer
+from zope.app.container.interfaces import IObjectFindFilter
 from zope.app.container.find import SimpleIdFindFilter
 
 import z3c.table.value
 from z3c.contents import interfaces
 from z3c.contents import browser
-from z3c.contents.search import SearchableTextFindFilter
 
 
+def _search_helper(id, object, container, id_filters, object_filters, result):
+    # check id filters if we get a match then return immediately
+    for id_filter in id_filters:
+        if id_filter.matches(id):
+            result.append(object)
+            return
+
+    # now check all object filters
+    for object_filter in object_filters:
+        if object_filter.matches(object):
+            result.append(object)
+            return
+
+    # do we need to check sub containers?
+    if not IReadContainer.providedBy(object):
+        return
+
+    container = object
+    for id, object in container.items():
+        _search_helper(id, object, container, id_filters, object_filters, result)
+
+
+class SearchableTextFindFilter(object):
+    """Filter objects on the ISearchableText adapters to the object."""
+
+    zope.interface.implements(IObjectFindFilter)
+    
+    def __init__(self, terms):
+        self.terms = terms
+    
+    def matches(self, object):
+        """Check if one of the search terms is found in the searchable text
+        interface.
+        """
+        adapter = zope.component.queryAdapter(object, ISearchableText)
+        if not adapter:
+            return False
+        searchable = adapter.getSearchableText().lower()
+        for term in [t.lower() for t in self.terms]:
+            if term in searchable:
+                return True
+        return False
+
+
+class SearchForContainer(object):
+    """ISearch for IReadContainer."""
+
+    zope.interface.implements(interfaces.ISearch)
+    zope.component.adapts(IReadContainer)
+
+    def __init__(self, context):
+        self.context = context
+
+    def search(self, id_filters=None, object_filters=None):
+        'See ISearch'
+        id_filters = id_filters or []
+        object_filters = object_filters or []
+        result = []
+        for id, object in self.context.items():
+            _search_helper(id, object, self.context, id_filters, object_filters,
+                result)
+        return result
+
+
 class SearchableValues(z3c.table.value.ValuesMixin):
     """Values based on given search."""
 
-    zope.component.adapts(IReadContainer, IBrowserRequest, interfaces.IContentsPage)
+    zope.component.adapts(IReadContainer, IBrowserRequest,
+        interfaces.IContentsPage)
 
     @property
     def values(self):



More information about the Checkins mailing list