From d4ad562a5abd7dac58f81915ff0bba9dcfd320a7 Mon Sep 17 00:00:00 2001 From: Ryan Hitchman Date: Thu, 18 Mar 2010 15:11:20 -0600 Subject: [PATCH] improve quote retrieval regex and some exception handling --- core/main.py | 4 +-- core/reload.py | 6 ++-- plugins/quote.py | 92 +++++++++++++++++++++++------------------------- 3 files changed, 49 insertions(+), 53 deletions(-) diff --git a/core/main.py b/core/main.py index ed56710..52a741b 100644 --- a/core/main.py +++ b/core/main.py @@ -63,9 +63,9 @@ def run(func, input): def do_sieve(sieve, bot, input, func, type, args): try: return sieve(bot, input, func, type, args) - except Exception, e: + except Exception: print 'sieve error', - traceback.print_exc(Exception) + traceback.print_exc() return None diff --git a/core/reload.py b/core/reload.py index 6a88ef7..cfc9831 100644 --- a/core/reload.py +++ b/core/reload.py @@ -53,8 +53,8 @@ def reload(init=False): eval(compile(open(filename, 'U').read(), filename, 'exec'), globals()) except Exception: - traceback.print_exc(Exception) - if init: # stop if there's a syntax error in a core + traceback.print_exc() + if init: # stop if there's an error (syntax?) in a core sys.exit() # script on startup continue @@ -90,7 +90,7 @@ def reload(init=False): namespace = {} eval(code, namespace) except Exception: - traceback.print_exc(Exception) + traceback.print_exc() continue # remove plugins already loaded from this filename diff --git a/plugins/quote.py b/plugins/quote.py index 20e2258..c7565ca 100644 --- a/plugins/quote.py +++ b/plugins/quote.py @@ -6,10 +6,9 @@ from util import hook def add_quote(db, chan, nick, add_nick, msg): - now = time.time() db.execute('''insert or fail into quote (chan, nick, add_nick, msg, time) values(?,?,?,?,?)''', - (chan, nick, add_nick, msg, now)) + (chan, nick, add_nick, msg, time.time())) db.commit() @@ -33,7 +32,7 @@ def format_quote(q, num, n_quotes): @hook.command('q') @hook.command def quote(inp, nick='', chan='', db=None): - ".q/.quote [#n]/.quote add -- gets " \ + ".q/.quote [#n]/.quote add -- gets " \ "random or [#n]th quote by or from <#chan>/adds quote" db.execute("create table if not exists quote" @@ -41,51 +40,48 @@ def quote(inp, nick='', chan='', db=None): "primary key (chan, nick, msg))") db.commit() - try: - add = re.match(r"add\s+?\s+(.*)", inp, re.I) - retrieve = re.match(r"(\S+)(?:\s+#?(-?\d+))?", inp) + add = re.match(r"add\W+(\S+?)>?\s+(.*)", inp, re.I) + retrieve = re.match(r"(\S+)(?:\s+#?(-?\d+))?$", inp) - if add: - quoted_nick, msg = add.groups() - try: - add_quote(db, chan, quoted_nick, nick, msg) - except db.IntegrityError: - return "message already stored, doing nothing." - return "quote added." - elif retrieve: - select, num = retrieve.groups() + if add: + quoted_nick, msg = add.groups() + try: + add_quote(db, chan, quoted_nick, nick, msg) + db.commit() + except db.IntegrityError: + return "message already stored, doing nothing." + return "quote added." + elif retrieve: + select, num = retrieve.groups() - by_chan = False - if select.startswith('#'): - by_chan = True - quotes = get_quotes_by_chan(db, select) - else: - quotes = get_quotes_by_nick(db, chan, select) - - n_quotes = len(quotes) - - if not n_quotes: - return "no quotes found" - - if num: - num = int(num) - - if num: - if num > n_quotes or (num < 0 and num < -n_quotes): - return "I only have %d quote%s for %s" % (n_quotes, - ('s', '')[n_quotes == 1], select) - elif num < 0: - selected_quote = quotes[num] - num = n_quotes + num + 1 - else: - selected_quote = quotes[num - 1] - else: - num = random.randint(1, n_quotes) - selected_quote = quotes[num - 1] - - return format_quote(selected_quote, num, n_quotes) + by_chan = False + if select.startswith('#'): + by_chan = True + quotes = get_quotes_by_chan(db, select) else: - return quote.__doc__ - finally: - db.commit() - db.close() + quotes = get_quotes_by_nick(db, chan, select) + + n_quotes = len(quotes) + + if not n_quotes: + return "no quotes found" + + if num: + num = int(num) + + if num: + if num > n_quotes or (num < 0 and num < -n_quotes): + return "I only have %d quote%s for %s" % (n_quotes, + ('s', '')[n_quotes == 1], select) + elif num < 0: + selected_quote = quotes[num] + num = n_quotes + num + 1 + else: + selected_quote = quotes[num - 1] + else: + num = random.randint(1, n_quotes) + selected_quote = quotes[num - 1] + + return format_quote(selected_quote, num, n_quotes) + else: + return quote.__doc__