mirror of
https://github.com/ankitects/anki.git
synced 2025-09-18 14:02:21 -04:00
refactor the large if-elif-else used for searching
This commit is contained in:
parent
295723ee8f
commit
ff82aad50f
1 changed files with 24 additions and 30 deletions
54
anki/find.py
54
anki/find.py
|
@ -15,6 +15,18 @@ class Finder(object):
|
|||
def __init__(self, col):
|
||||
self.col = col
|
||||
|
||||
self.search = {}
|
||||
self.search['added'] = self._findAdded
|
||||
self.search['card'] = self._findTemplate
|
||||
self.search['deck'] = self._findDeck
|
||||
self.search['is'] = self._findCardState
|
||||
self.search['mid'] = self._findMid
|
||||
self.search['nid'] = self._findNids
|
||||
self.search['note'] = self._findModel
|
||||
self.search['prop'] = self._findProp
|
||||
self.search['rated'] = self._findRated
|
||||
self.search['tag'] = self._findTag
|
||||
|
||||
def findCards(self, query, order=False):
|
||||
"Return a list of card ids for QUERY."
|
||||
tokens = self._tokenize(query)
|
||||
|
@ -154,26 +166,8 @@ select distinct(n.id) from cards c, notes n where c.nid=n.id and """+preds
|
|||
elif ":" in token:
|
||||
cmd, val = token.split(":", 1)
|
||||
cmd = cmd.lower()
|
||||
if cmd == "tag":
|
||||
add(self._findTag(val, args))
|
||||
elif cmd == "is":
|
||||
add(self._findCardState(val))
|
||||
elif cmd == "nid":
|
||||
add(self._findNids(val))
|
||||
elif cmd == "card":
|
||||
add(self._findTemplate(val))
|
||||
elif cmd == "note":
|
||||
add(self._findModel(val))
|
||||
elif cmd == "mid":
|
||||
add(self._findMid(val))
|
||||
elif cmd == "deck":
|
||||
add(self._findDeck(val))
|
||||
elif cmd == "prop":
|
||||
add(self._findProp(val))
|
||||
elif cmd == "rated":
|
||||
add(self._findRated(val))
|
||||
elif cmd == "added":
|
||||
add(self._findAdded(val))
|
||||
if cmd in self.search:
|
||||
add(self.search[cmd]((val, args)))
|
||||
else:
|
||||
add(self._findField(cmd, val))
|
||||
# normal text search
|
||||
|
@ -239,7 +233,7 @@ select distinct(n.id) from cards c, notes n where c.nid=n.id and """+preds
|
|||
# Commands
|
||||
######################################################################
|
||||
|
||||
def _findTag(self, val, args):
|
||||
def _findTag(self, (val, args)):
|
||||
if val == "none":
|
||||
return 'n.tags = ""'
|
||||
val = val.replace("*", "%")
|
||||
|
@ -250,7 +244,7 @@ select distinct(n.id) from cards c, notes n where c.nid=n.id and """+preds
|
|||
args.append(val)
|
||||
return "n.tags like ?"
|
||||
|
||||
def _findCardState(self, val):
|
||||
def _findCardState(self, (val, args)):
|
||||
if val in ("review", "new", "learn"):
|
||||
if val == "review":
|
||||
n = 2
|
||||
|
@ -267,7 +261,7 @@ select distinct(n.id) from cards c, notes n where c.nid=n.id and """+preds
|
|||
(c.queue = 1 and c.due <= %d)""" % (
|
||||
self.col.sched.today, self.col.sched.dayCutoff)
|
||||
|
||||
def _findRated(self, val):
|
||||
def _findRated(self, (val, args)):
|
||||
# days(:optional_ease)
|
||||
r = val.split(":")
|
||||
try:
|
||||
|
@ -285,7 +279,7 @@ select distinct(n.id) from cards c, notes n where c.nid=n.id and """+preds
|
|||
return ("c.id in (select cid from revlog where id>%d %s)" %
|
||||
(cutoff, ease))
|
||||
|
||||
def _findAdded(self, val):
|
||||
def _findAdded(self, (val, args)):
|
||||
try:
|
||||
days = int(val)
|
||||
except ValueError:
|
||||
|
@ -293,7 +287,7 @@ select distinct(n.id) from cards c, notes n where c.nid=n.id and """+preds
|
|||
cutoff = (self.col.sched.dayCutoff - 86400*days)*1000
|
||||
return "c.id > %d" % cutoff
|
||||
|
||||
def _findProp(self, val):
|
||||
def _findProp(self, (val, args)):
|
||||
# extract
|
||||
m = re.match("(^.+?)(<=|>=|!=|=|<|>)(.+?$)", val)
|
||||
if not m:
|
||||
|
@ -329,17 +323,17 @@ select distinct(n.id) from cards c, notes n where c.nid=n.id and """+preds
|
|||
args.append("%"+val+"%")
|
||||
return "(n.sfld like ? escape '\\' or n.flds like ? escape '\\')"
|
||||
|
||||
def _findNids(self, val):
|
||||
def _findNids(self, (val, args)):
|
||||
if re.search("[^0-9,]", val):
|
||||
return
|
||||
return "n.id in (%s)" % val
|
||||
|
||||
def _findMid(self, val):
|
||||
def _findMid(self, (val, args)):
|
||||
if re.search("[^0-9]", val):
|
||||
return
|
||||
return "n.mid = %s" % val
|
||||
|
||||
def _findModel(self, val):
|
||||
def _findModel(self, (val, args)):
|
||||
ids = []
|
||||
val = val.lower()
|
||||
for m in self.col.models.all():
|
||||
|
@ -347,7 +341,7 @@ select distinct(n.id) from cards c, notes n where c.nid=n.id and """+preds
|
|||
ids.append(m['id'])
|
||||
return "n.mid in %s" % ids2str(ids)
|
||||
|
||||
def _findDeck(self, val):
|
||||
def _findDeck(self, (val, args)):
|
||||
# if searching for all decks, skip
|
||||
if val == "*":
|
||||
return "skip"
|
||||
|
@ -377,7 +371,7 @@ select distinct(n.id) from cards c, notes n where c.nid=n.id and """+preds
|
|||
sids = ids2str(ids)
|
||||
return "c.did in %s or c.odid in %s" % (sids, sids)
|
||||
|
||||
def _findTemplate(self, val):
|
||||
def _findTemplate(self, (val, args)):
|
||||
# were we given an ordinal number?
|
||||
try:
|
||||
num = int(val) - 1
|
||||
|
|
Loading…
Reference in a new issue