[Zope-Checkins] CVS: Zope/lib/python/Products/PluginIndexes/PathIndex - PathIndex.py:1.4.4.3

Andreas Jung andreas@zope.com
Mon, 26 Nov 2001 14:08:00 -0500


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

Modified Files:
      Tag: Zope-2_4-branch
	PathIndex.py 
Log Message:
backport of major fixes from trunk
updated testsuite


=== Zope/lib/python/Products/PluginIndexes/PathIndex/PathIndex.py 1.4.4.2 => 1.4.4.3 ===
 from BTrees.OIBTree import OIBTree
 from BTrees.IIBTree import IISet,difference,intersection,union
-from types import StringType
-import re
+from types import StringType, ListType, TupleType
+import re,warnings
+
+_marker = []
 
 
 class PathIndex(PluggableIndex.PluggableIndex, Persistent,
@@ -165,21 +167,34 @@
         
         if level > self._depth: self._depth = level
 
-        # reverse index
-        if not self._unindex.has_key(id):
-            self._unindex[id] = OOSet()
-        self._unindex[id].insert( (comp,level) )
             
 
     def index_object(self, documentId, obj ,threshold=100):
         """ hook for (Z)Catalog """
 
-        try:
-            path = obj.getPhysicalPath()
-        except:
-            return 0
 
-        path = '/'+ '/'.join(path[1:])
+        # first we check if the object provide an attribute or
+        # method to be used as hook for the PathIndex
+
+        if hasattr(obj,self.id):
+            f = getattr(obj,self.id)
+
+            try:
+                if callable(f): path = f()
+                else:           path = f
+            except:
+                return 0
+
+        else:             
+
+            try:
+                path = obj.getPhysicalPath()
+            except:
+                return 0
+
+        if type(path) in (ListType,TupleType):
+            path = '/'+ '/'.join(path[1:])
+
         comps = self.splitPath(path,obj)
 
         if obj.meta_type != 'Folder':
@@ -188,18 +203,24 @@
         for i in range(len(comps)):
             self.insertEntry( comps[i],documentId,i)
 
+        self._unindex[documentId] = path
+
         return 1
 
 
-    def unindex_object(self,id):
+    def unindex_object(self,documentId):
         """ hook for (Z)Catalog """
 
-        if not self._unindex.has_key(id):
-            return 
+        if not self._unindex.has_key(documentId):
+            return
 
-        for comp,level in self._unindex[id]:
+        path = self._unindex[documentId]
+        comps = path.split('/')
 
-            self._index[comp][level].remove(id)
+        for level in range(len(comps[1:])-1):
+            comp = comps[level+1]
+    
+            self._index[comp][level].remove(documentId)
 
             if len(self._index[comp][level])==0:
                 del self._index[comp][level]
@@ -207,7 +228,7 @@
             if len(self._index[comp])==0:
                 del self._index[comp]
 
-        del self._unindex[id]
+        del self._unindex[documentId]
 
 
     def printIndex(self):
@@ -247,7 +268,7 @@
         if isinstance(path,StringType):
             level = default_level
         else:
-            level = path[1]
+            level = int(path[1])
             path  = path[0]
 
         comps = self.splitPath(path)
@@ -260,8 +281,8 @@
                 
                 comp = comps[i]
 
-                if not self._index.has_key(comp): return []
-                if not self._index[comp].has_key(level+i): return []
+                if not self._index.has_key(comp): return IISet()
+                if not self._index[comp].has_key(level+i): return IISet()
 
                 results.append( self._index[comp][level+i] )
 
@@ -274,7 +295,7 @@
 
         else:
 
-            results = None
+            results = IISet()
 
             for level in range(0,self._depth):
            
@@ -345,6 +366,13 @@
         record = parseIndexRequest(request,self.id,self.query_options)
         if record.keys==None: return None
 
+        if request.has_key('%s_level' % cid):
+            warnings.warn("The usage of the '%s_level' "
+                          "is no longer recommended.\n"
+                          "Please use a mapping object and the "
+                          "'level' key to specify the operator." % cid)
+
+
         # get the level parameter 
         level    = record.get("level",0)
 
@@ -368,11 +396,20 @@
 
     
     def uniqueValues(self,name=None,withLength=0):
-        """ need to be consistent with the interface """
+        """ needed to be consistent with the interface """
 
         return self._index.keys()
-        
 
+
+    def getEntryForObject(self,documentId,default=_marker):
+        """ Takes a document ID and returns all the information we have
+        on that specific object. """
+
+        try:        
+            return self._unindex[documentId]
+        except:
+            return None
+        
 
     index_html = DTMLFile('dtml/index', globals())
     manage_workspace = DTMLFile('dtml/managePathIndex', globals())