refactor the large if-elif-else used for searching

This commit is contained in:
Pauline Gomér 2013-02-10 14:18:38 +01:00 committed by Jon-Erik 'TorrentKatten' Johnzon
parent 295723ee8f
commit ff82aad50f

View file

@ -15,6 +15,18 @@ class Finder(object):
def __init__(self, col): def __init__(self, col):
self.col = 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): def findCards(self, query, order=False):
"Return a list of card ids for QUERY." "Return a list of card ids for QUERY."
tokens = self._tokenize(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: elif ":" in token:
cmd, val = token.split(":", 1) cmd, val = token.split(":", 1)
cmd = cmd.lower() cmd = cmd.lower()
if cmd == "tag": if cmd in self.search:
add(self._findTag(val, args)) add(self.search[cmd]((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))
else: else:
add(self._findField(cmd, val)) add(self._findField(cmd, val))
# normal text search # normal text search
@ -239,7 +233,7 @@ select distinct(n.id) from cards c, notes n where c.nid=n.id and """+preds
# Commands # Commands
###################################################################### ######################################################################
def _findTag(self, val, args): def _findTag(self, (val, args)):
if val == "none": if val == "none":
return 'n.tags = ""' return 'n.tags = ""'
val = val.replace("*", "%") 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) args.append(val)
return "n.tags like ?" return "n.tags like ?"
def _findCardState(self, val): def _findCardState(self, (val, args)):
if val in ("review", "new", "learn"): if val in ("review", "new", "learn"):
if val == "review": if val == "review":
n = 2 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)""" % ( (c.queue = 1 and c.due <= %d)""" % (
self.col.sched.today, self.col.sched.dayCutoff) self.col.sched.today, self.col.sched.dayCutoff)
def _findRated(self, val): def _findRated(self, (val, args)):
# days(:optional_ease) # days(:optional_ease)
r = val.split(":") r = val.split(":")
try: 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)" % return ("c.id in (select cid from revlog where id>%d %s)" %
(cutoff, ease)) (cutoff, ease))
def _findAdded(self, val): def _findAdded(self, (val, args)):
try: try:
days = int(val) days = int(val)
except ValueError: 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 cutoff = (self.col.sched.dayCutoff - 86400*days)*1000
return "c.id > %d" % cutoff return "c.id > %d" % cutoff
def _findProp(self, val): def _findProp(self, (val, args)):
# extract # extract
m = re.match("(^.+?)(<=|>=|!=|=|<|>)(.+?$)", val) m = re.match("(^.+?)(<=|>=|!=|=|<|>)(.+?$)", val)
if not m: 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+"%") args.append("%"+val+"%")
return "(n.sfld like ? escape '\\' or n.flds like ? escape '\\')" 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): if re.search("[^0-9,]", val):
return return
return "n.id in (%s)" % val return "n.id in (%s)" % val
def _findMid(self, val): def _findMid(self, (val, args)):
if re.search("[^0-9]", val): if re.search("[^0-9]", val):
return return
return "n.mid = %s" % val return "n.mid = %s" % val
def _findModel(self, val): def _findModel(self, (val, args)):
ids = [] ids = []
val = val.lower() val = val.lower()
for m in self.col.models.all(): 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']) ids.append(m['id'])
return "n.mid in %s" % ids2str(ids) return "n.mid in %s" % ids2str(ids)
def _findDeck(self, val): def _findDeck(self, (val, args)):
# if searching for all decks, skip # if searching for all decks, skip
if val == "*": if val == "*":
return "skip" 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) sids = ids2str(ids)
return "c.did in %s or c.odid in %s" % (sids, sids) 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? # were we given an ordinal number?
try: try:
num = int(val) - 1 num = int(val) - 1