11''' This implements the full-text indexer using the Xapian indexer.
22'''
3- import re , os , time
4-
3+ import os
4+ import re
5+ import time
56import xapian
67
7- from roundup .backends .indexer_common import Indexer as IndexerBase
88from roundup .anypy .strings import b2s , s2b
9+ from roundup .backends .indexer_common import Indexer as IndexerBase
910from roundup .i18n import _
1011
1112# TODO: we need to delete documents when a property is *reindexed*
1516# "Where std::string is returned, it's always mapped to bytes in
1617# Python..."
1718
19+
1820class Indexer (IndexerBase ):
1921 def __init__ (self , db ):
2022 IndexerBase .__init__ (self , db )
@@ -35,7 +37,8 @@ def _get_database(self):
3537 # we are back to the for loop
3638
3739 # Get here only if we dropped out of the for loop.
38- raise xapian .DatabaseLockError ("Unable to get lock after 10 retries on %s." % index )
40+ raise xapian .DatabaseLockError (_ (
41+ "Unable to get lock after 10 retries on %s." ) % index )
3942
4043 def save_index (self ):
4144 '''Save the changes to the index.'''
@@ -70,23 +73,24 @@ def add_text(self, identifier, text, mime_type='text/plain'):
7073 ''' "identifier" is (classname, itemid, property) '''
7174 if mime_type != 'text/plain' :
7275 return
73- if not text : text = ''
76+ if not text :
77+ text = ''
7478
7579 # open the database and start a transaction if needed
7680 database = self ._get_database ()
7781
78- # XXX: Xapian now supports transactions,
82+ # XXX: Xapian now supports transactions,
7983 # but there is a call to save_index() missing.
80- #if not self.transaction_active:
81- # database.begin_transaction()
82- # self.transaction_active = True
84+ # if not self.transaction_active:
85+ # database.begin_transaction()
86+ # self.transaction_active = True
8387
8488 stemmer = xapian .Stem (self .language )
8589
8690 # We use the identifier twice: once in the actual "text" being
8791 # indexed so we can search on it, and again as the "data" being
8892 # indexed so we know what we're matching when we get results
89- identifier = s2b ('%s:%s:%s' % identifier )
93+ identifier = s2b ('%s:%s:%s' % identifier )
9094
9195 # create the new document
9296 doc = xapian .Document ()
@@ -118,7 +122,7 @@ def find(self, wordlist):
118122 stemmer = xapian .Stem (self .language )
119123 terms = []
120124 for term in [word .upper () for word in wordlist
121- if self .minlength <= len (word ) <= self .maxlength ]:
125+ if self .minlength <= len (word ) <= self .maxlength ]:
122126 if not self .is_stopword (term ):
123127 terms .append (stemmer (s2b (term .lower ())))
124128 query = xapian .Query (xapian .Query .OP_AND , terms )
@@ -127,5 +131,4 @@ def find(self, wordlist):
127131 matches = enquire .get_mset (0 , database .get_doccount ())
128132
129133 return [tuple (b2s (m .document .get_data ()).split (':' ))
130- for m in matches ]
131-
134+ for m in matches ]
0 commit comments