From ff82aad50f661ae21b6f04b78cdcba960761602c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pauline=20Gom=C3=A9r?= Date: Sun, 10 Feb 2013 14:18:38 +0100 Subject: [PATCH 1/3] refactor the large if-elif-else used for searching --- anki/find.py | 54 +++++++++++++++++++++++----------------------------- 1 file changed, 24 insertions(+), 30 deletions(-) diff --git a/anki/find.py b/anki/find.py index 8e7949c3e..6a5587f13 100644 --- a/anki/find.py +++ b/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 From b07254cb09719e093a474e0cb98a969ef4c81836 Mon Sep 17 00:00:00 2001 From: Jon-Erik 'TorrentKatten' Johnzon Date: Fri, 15 Feb 2013 19:28:55 +0100 Subject: [PATCH 2/3] added search-hook for plugins using "search" scope --- anki/find.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/anki/find.py b/anki/find.py index 6a5587f13..04d72979b 100644 --- a/anki/find.py +++ b/anki/find.py @@ -5,6 +5,7 @@ import re from anki.utils import ids2str, splitFields, joinFields, intTime from anki.consts import * +from anki.hooks import * import sre_constants # Find @@ -27,6 +28,10 @@ class Finder(object): self.search['rated'] = self._findRated self.search['tag'] = self._findTag + + def _loadSearchHookPlugins(self): + runHook("search", self.search) + def findCards(self, query, order=False): "Return a list of card ids for QUERY." tokens = self._tokenize(query) @@ -120,6 +125,7 @@ select distinct(n.id) from cards c, notes n where c.nid=n.id and """+preds ###################################################################### def _where(self, tokens): + self._loadSearchHookPlugins() # state and query s = dict(isnot=False, isor=False, join=False, q="", bad=False) args = [] From fc612a71d3f686fd91f46b56708f3d347748b341 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pauline=20Gom=C3=A9r?= Date: Sat, 16 Feb 2013 13:23:51 +0100 Subject: [PATCH 3/3] moved loading of plugins to initialization --- anki/find.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/anki/find.py b/anki/find.py index 04d72979b..31b37e59e 100644 --- a/anki/find.py +++ b/anki/find.py @@ -27,6 +27,7 @@ class Finder(object): self.search['prop'] = self._findProp self.search['rated'] = self._findRated self.search['tag'] = self._findTag + self._loadSearchHookPlugins() def _loadSearchHookPlugins(self): @@ -125,7 +126,6 @@ select distinct(n.id) from cards c, notes n where c.nid=n.id and """+preds ###################################################################### def _where(self, tokens): - self._loadSearchHookPlugins() # state and query s = dict(isnot=False, isor=False, join=False, q="", bad=False) args = []