[Zope-Checkins] CVS: Zope/lib/python/Products/PluginIndexes/KeywordIndex - KeywordIndex.py:1.11.6.2

Sidnei da Silva sidnei@x3ng.com.br
Tue, 17 Jun 2003 14:14:57 -0400


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

Modified Files:
      Tag: Zope-2_6-branch
	KeywordIndex.py 
Log Message:
 - Made all PluginIndexes and ZCTextIndex use 'safe_callable',
        which is aware of extension classes that fill 'tp_callable'
        but don't define '__call__'.

      - Made KeywordIndex be more robust about receiving a value that
        is not a string or an iterable type.

=== Zope/lib/python/Products/PluginIndexes/KeywordIndex/KeywordIndex.py 1.11.6.1 => 1.11.6.2 ===
--- Zope/lib/python/Products/PluginIndexes/KeywordIndex/KeywordIndex.py:1.11.6.1	Fri Feb 28 10:58:18 2003
+++ Zope/lib/python/Products/PluginIndexes/KeywordIndex/KeywordIndex.py	Tue Jun 17 14:14:26 2003
@@ -11,6 +11,8 @@
 #
 ##############################################################################
 
+from types import StringType, UnicodeType
+
 from zLOG import LOG, ERROR
 from BTrees.OOBTree import OOSet, difference
 
@@ -18,6 +20,7 @@
 from Products.PluginIndexes import PluggableIndex
 
 from Products.PluginIndexes.common.UnIndex import UnIndex
+from Products.PluginIndexes.common import safe_callable
 
 class KeywordIndex(UnIndex):
 
@@ -86,17 +89,23 @@
 
     def _get_object_keywords(self,obj):
         newKeywords = getattr(obj, self.id, ())
-        if callable(newKeywords):
+
+        if safe_callable(newKeywords):
             newKeywords = newKeywords()
-        if hasattr(newKeywords,'capitalize'): # is it string-like ?
-            newKeywords = (newKeywords, )
+
+        if (isinstance(newKeywords, StringType)
+            or isinstance(newKeywords, UnicodeType)): #Python 2.1 compat isinstance
+            return (newKeywords,)
         else:
-            # Uniqueify keywords
             unique = {}
-            for k in newKeywords:
-                unique[k] = None
-            newKeywords = unique.keys()
-        return newKeywords
+            try:
+                for k in newKeywords:
+                    unique[k] = None
+            except TypeError:
+                # Not a sequence
+                return (newKeywords,)
+            else:
+                return unique.keys()
 
     def unindex_objectKeywords(self, documentId, keywords):
         """ carefully unindex the object with integer id 'documentId'"""