1717 Column ('chan' , String )
1818)
1919
20+ grab_cache = {}
21+
22+
2023@hook .on_start ()
2124def load_cache (db ):
2225 """
2326 :type db: sqlalchemy.orm.Session
2427 """
25- global grab_cache
26- grab_cache = {}
28+ grab_cache .clear ()
2729 for row in db .execute (table .select ().order_by (table .c .time )):
2830 name = row ["name" ].lower ()
2931 quote = row ["quote" ]
3032 chan = row ["chan" ]
31- if chan not in grab_cache :
32- grab_cache .update ({chan :{name :[chan ]}})
33- elif name not in grab_cache [chan ]:
34- grab_cache [chan ].update ({name :[quote ]})
35- else :
36- grab_cache [chan ][name ].append (quote )
33+ grab_cache .setdefault (chan , {}).setdefault (name , []).append (quote )
34+
3735
3836def two_lines (bigstring , chan ):
3937 """Receives a string with new lines. Groups the string into a list of strings with up to 3 new lines per string element. Returns first string element then stores the remaining list in search_pages."""
@@ -61,7 +59,7 @@ def moregrab(text, chan):
6159 index = ""
6260 try :
6361 index = int (text )
64- except :
62+ except ValueError :
6563 return "Please specify an integer value."
6664 if abs (int (index )) > len (search_pages [chan ]) or index == 0 :
6765 return "please specify a valid page number between 1 and {}." .format (len (search_pages [chan ]))
@@ -74,15 +72,17 @@ def moregrab(text, chan):
7472 else :
7573 return "All pages have been shown you can specify a page number or do a new search."
7674
75+
7776def check_grabs (name , quote , chan ):
7877 try :
7978 if quote in grab_cache [chan ][name ]:
8079 return True
8180 else :
8281 return False
83- except :
82+ except KeyError :
8483 return False
8584
85+
8686def grab_add (nick , time , msg , chan , db , conn ):
8787 # Adds a quote to the grab table
8888 db .execute (table .insert ().values (name = nick , time = time , quote = msg , chan = chan ))
@@ -103,15 +103,16 @@ def grab(text, nick, chan, db, conn):
103103 # check to see if the quote has been added
104104 if check_grabs (name .lower (), msg , chan ):
105105 return "I already have that quote from {} in the database" .format (text )
106- break
107106 else :
108107 # the quote is new so add it to the db.
109108 grab_add (name .lower (),timestamp , msg , chan , db , conn )
110109 if check_grabs (name .lower (), msg , chan ):
111110 return "the operation succeeded."
112- break
111+ else :
112+ return "the operation failed"
113113 return "I couldn't find anything from {} in recent history." .format (text )
114114
115+
115116def format_grab (name , quote ):
116117 # add nonbreaking space to nicks to avoid highlighting people with printed grabs
117118 name = "{}{}{}" .format (name [0 ], u"\u200B " , name [1 :])
@@ -123,13 +124,14 @@ def format_grab(name, quote):
123124 out = "<{}> {}" .format (name , quote )
124125 return out
125126
127+
126128@hook .command ("lastgrab" , "lgrab" )
127129def lastgrab (text , chan , message ):
128130 """prints the last grabbed quote from <nick>."""
129131 lgrab = ""
130132 try :
131133 lgrab = grab_cache [chan ][text .lower ()][- 1 ]
132- except :
134+ except ( KeyError , IndexError ) :
133135 return "<{}> has never been grabbed." .format (text )
134136 if lgrab :
135137 quote = lgrab
@@ -150,11 +152,11 @@ def grabrandom(text, chan, message):
150152 else :
151153 try :
152154 name = random .choice (list (grab_cache [chan ].keys ()))
153- except :
155+ except KeyError :
154156 return "I couldn't find any grabs in {}." .format (chan )
155157 try :
156158 grab = random .choice (grab_cache [chan ][name .lower ()])
157- except :
159+ except KeyError :
158160 return "it appears {} has never been grabbed in {}" .format (name , chan )
159161 if grab :
160162 message (format_grab (name , grab ), chan )
@@ -173,8 +175,8 @@ def grabsearch(text, chan):
173175 quotes = grab_cache [chan ][text .lower ()]
174176 for grab in quotes :
175177 result .append ((text , grab ))
176- except :
177- pass
178+ except KeyError :
179+ pass
178180 for name in grab_cache [chan ]:
179181 for grab in grab_cache [chan ][name ]:
180182 if name != text .lower ():
0 commit comments