Skip to content

Commit 67bf83e

Browse files
authored
Merge pull request CloudBotIRC#180 from linuxdaemon/gonzobot+fix-grabsearch-keyerror
Clean up error handling in grabsearch command
2 parents dbf5cda + 83b9cb5 commit 67bf83e

File tree

1 file changed

+30
-23
lines changed

1 file changed

+30
-23
lines changed

plugins/grab.py

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -178,31 +178,38 @@ def grabrandom(text, chan, message):
178178
def grabsearch(text, chan, conn):
179179
"""[text] - matches "text" against nicks or grab strings in the database"""
180180
result = []
181+
lower_text = text.lower()
181182
with cache_lock:
182183
try:
183-
quotes = grab_cache[chan][text.lower()]
184-
for grab in quotes:
185-
result.append((text, grab))
184+
chan_grabs = grab_cache[chan]
185+
except LookupError:
186+
return "I couldn't find any grabs in {}.".format(chan)
187+
188+
try:
189+
quotes = chan_grabs[lower_text]
186190
except KeyError:
187191
pass
188-
for name in grab_cache[chan]:
189-
for grab in grab_cache[chan][name]:
190-
if name != text.lower():
191-
if text.lower() in grab.lower():
192-
result.append((name, grab))
193-
194-
if result:
195-
grabs = []
196-
for name, quote in result:
197-
if text.lower() == name:
198-
name = text
199-
grabs.append(format_grab(name, quote))
200-
pager = paginated_list(grabs)
201-
search_pages[conn.name][chan] = pager
202-
page = pager.next()
203-
if len(pager) > 1:
204-
page[-1] += " .moregrab"
205-
206-
return page
207-
else:
192+
else:
193+
result.extend((text, quote) for quote in quotes)
194+
195+
for name, quotes in chan_grabs.items():
196+
if name != lower_text:
197+
result.extend((name, quote) for quote in quotes if lower_text in quote.lower())
198+
199+
if not result:
208200
return "I couldn't find any matches for {}.".format(text)
201+
202+
grabs = []
203+
for name, quote in result:
204+
if lower_text == name:
205+
name = text
206+
207+
grabs.append(format_grab(name, quote))
208+
209+
pager = paginated_list(grabs)
210+
search_pages[conn.name][chan] = pager
211+
page = pager.next()
212+
if len(pager) > 1:
213+
page[-1] += " .moregrab"
214+
215+
return page

0 commit comments

Comments
 (0)