[Zope-Checkins] CVS: Zope/lib/python/Products/PluginIndexes/TopicIndex - FilteredSet.py:1.1.2.4 TopicIndex.py:1.1.2.4

Andreas Jung andreas@zope.com
Tue, 16 Oct 2001 10:22:41 -0400


Update of /cvs-repository/Zope/lib/python/Products/PluginIndexes/TopicIndex
In directory cvs.zope.org:/tmp/cvs-serv11527

Modified Files:
      Tag: ajung-topicindex
	FilteredSet.py TopicIndex.py 
Log Message:
added support for customizable FilteredSets


=== Zope/lib/python/Products/PluginIndexes/TopicIndex/FilteredSet.py 1.1.2.3 => 1.1.2.4 ===
 
 
-class FilteredSet(Persistent):
+class FilteredSetBase(Persistent):
 
     def __init__(self, id, expr):
 
@@ -98,27 +98,61 @@
         self.expr = expr
         self.ids  = IISet()
 
+    
+    def clear(self):
+        self.ids  = IISet()
 
-    def index_object(self, documentId, obj):
 
-        # Lame,lame,lame - just for testing purposes
-        res = eval("obj.%s" % self.expr)
-        if res:
-            self.ids.insert(documentId)
+    def index_object(self, documentId, obj):
+        raise RuntimeError,'index_object not defined'
 
 
-    def removedId(self,id):
-        try:
-            self.ids.remove(id)
+    def unindex_object(self,documentId):
+        try: self.ids.remove(Id)
         except: pass
 
 
     def getId(self):            return self.id
     def getExpression(self):    return self.expr
     def getIds(self):           return self.ids
-    def Ids(self):              return self.getIds()
+    def getType(self):          return self.meta_type
     
     def __repr__(self):
         return '%s: (%s) %s' % (self.id,self.expr,map(None,self.ids))
         
     __str__ = __repr__
+
+
+
+class PythonFilteredSet(FilteredSetBase):
+
+    meta_type = 'PythonFilteredSet'
+
+    def index_object(self, documentId, o):
+
+        # Lame,lame,lame - just for testing purposes
+        res = eval(self.expr)
+        if res:
+            self.ids.insert(documentId)
+
+
+class CatalogFilteredSet(FilteredSetBase):
+
+    meta_type = 'CatalogFilteredSet'
+
+    def index_object(self, documentId, obj):
+        pass
+
+
+
+
+def factory(f_id, f_type, expr):
+
+    if f_type=='PythonFilteredSet':
+        return PythonFilteredSet(f_id, expr)
+
+    elif f_type=='CatalogFilteredSet':
+        return CatalogFilteredSet(f_id, expr)
+
+    else:
+        raise TypeError,'unknown type for FilteredSets: %s' % f_type


=== Zope/lib/python/Products/PluginIndexes/TopicIndex/TopicIndex.py 1.1.2.3 => 1.1.2.4 ===
 from Acquisition import Implicit
 from OFS.SimpleItem import SimpleItem
-from FilteredSet import FilteredSet
 from BTrees.OOBTree import OOBTree,OOSet
 from BTrees.IIBTree import IISet,difference,intersection,union
 from types import StringType, ListType, TupleType
 import re,warnings
+import FilteredSet
 
 _marker = []
 
@@ -141,7 +141,6 @@
         self.filteredSets = OOBTree()
 
             
-
     def index_object(self, documentId, obj ,threshold=100):
         """ hook for (Z)Catalog """
 
@@ -153,9 +152,11 @@
 
     def unindex_object(self,documentId):
         """ hook for (Z)Catalog """
+
         self.uncatalogedIds.insert(documentId)
+
         for fs in self.filteredSets.values():
-            fs.removeId(documentId)
+            fs.unindex_object(documentId)
 
         return 1
 
@@ -179,7 +180,7 @@
     def search(self,filterId):
 
         if self.filteredSets.has_key(filterId):
-            return self.filteredSets[filterId].Ids()
+            return self.filteredSets[filterId].getIds()
 
 
     def _apply_index(self, request, cid=''): 
@@ -223,13 +224,14 @@
         return self.filteredSets.keys()
 
 
-    def addFilteredSet(self, filterId, expr):
+    def addFilteredSet(self, filterId, typeFilteredSet, expr):
 
         if self.filteredSets.has_key(filterId):
             raise KeyError,\
                 'A FilteredSet with this name already exists: %s' % filterId        
 
-        self.filteredSets[filterId] = FilteredSet(filterId, expr)
+        self.filteredSets[filterId] = \
+            FilteredSet.factory(filterId, typeFilteredSet, expr)
 
 
     def delFilteredSet(self,filterId):
@@ -241,11 +243,20 @@
         del self.filteredSets[filterId]
 
 
-    def manage_addFilteredSet(self, filterId, expr, URL1, \
+    def clearFilteredSet(self,filterId):
+        
+        if not self.filteredSets.has_key(filterId):
+            raise KeyError,\
+                'no such FilteredSet:  %s' % filterId        
+
+        self.filteredSets[filterId].clear()
+
+
+    def manage_addFilteredSet(self, filterId, typeFilteredSet, expr, URL1, \
             REQUEST=None,RESPONSE=None):
         """ add a new filtered set """
 
-        self.addFilteredSet(filterId, expr)
+        self.addFilteredSet(filterId, typeFilteredSet, expr)
 
         if RESPONSE:
             RESPONSE.redirect(URL1+'/addFilteredSetForm?manage_tabs_message=FilteredSet%20added')
@@ -262,6 +273,16 @@
             RESPONSE.redirect(URL1+'/addFilteredSetForm?manage_tabs_message=FilteredSet(s)%20deleted')
 
 
+    def manage_clearFilteredSet(self, filterIds, URL1, \
+            REQUEST=None,RESPONSE=None):
+        """  clear a list of FilteredSets"""
+
+        for filterId in filterIds:
+            self.clearFilteredSet(filterId)
+
+        if RESPONSE:
+            RESPONSE.redirect(URL1+'/addFilteredSetForm?manage_tabs_message=FilteredSet(s)%20cleared')
+
     index_html = DTMLFile('dtml/index', globals())
     manage_workspace = DTMLFile('dtml/manageTopicIndex', globals())
     manage_main = DTMLFile('dtml/manageTopicIndex', globals())
@@ -273,6 +294,3 @@
     """Add a TopicIndex"""
     return self.manage_addIndex(id, 'TopicIndex', extra=None, \
                 REQUEST=REQUEST, RESPONSE=RESPONSE, URL1=URL3)
-
-
-