This commit is contained in:
Kovid Goyal 2008-01-15 02:34:24 +00:00
parent da51c78758
commit 36ce446990
2 changed files with 19 additions and 6 deletions

View File

@ -25,7 +25,7 @@ from PyQt4.QtCore import QAbstractTableModel, QVariant, Qt, QString, \
QSettings
from libprs500.ptempfile import PersistentTemporaryFile
from libprs500.library.database import LibraryDatabase
from libprs500.library.database import LibraryDatabase, SearchToken
from libprs500.gui2 import NONE, TableView
from libprs500.gui2 import qstring_to_unicode
@ -160,7 +160,7 @@ class BooksModel(QAbstractTableModel):
ans = []
for i in tokens:
try:
ans.append(re.compile(i, re.IGNORECASE))
ans.append(SearchToken(i))
except sre_constants.error:
continue
return ans
@ -505,7 +505,7 @@ class DeviceBooksModel(BooksModel):
add = True
q = self.db[i].title + ' ' + self.db[i].authors + ' ' + ', '.join(self.db[i].tags)
for token in tokens:
if not token.search(q):
if not token.match(q):
add = False
break
if add:

View File

@ -805,8 +805,8 @@ ALTER TABLE books ADD COLUMN isbn TEXT DEFAULT "" COLLATE NOCASE;
for item in self.data if refilter else self.cache:
keep = True
test = ' '.join([item[i] if item[i] else '' for i in (1,2,3,7,8,9,13)])
for filter in filters:
if not filter.search(test):
for token in filters:
if not token.match(test):
keep = False
break
if keep:
@ -1236,7 +1236,20 @@ ALTER TABLE books ADD COLUMN isbn TEXT DEFAULT "" COLLATE NOCASE;
traceback.print_exc()
f.close()
class SearchToken(object):
def __init__(self, text_token):
if text_token.startswith('!'):
self.negate = True
text_token = text_token[1:]
else:
self.negate = False
self.pattern = re.compile(text_token, re.IGNORECASE)
def match(self, text):
return bool(self.pattern.search(text)) ^ self.negate
if __name__ == '__main__':
db = LibraryDatabase('/home/kovid/library1.db')