improve quote retrieval regex and some exception handling

This commit is contained in:
Ryan Hitchman 2010-03-18 15:11:20 -06:00
parent 8a93c7f21b
commit d4ad562a5a
3 changed files with 49 additions and 53 deletions

View File

@ -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

View File

@ -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

View File

@ -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 <nick/#chan> [#n]/.quote add <nick> <msg> -- gets " \
".q/.quote <nick|#chan> [#n]/.quote add <nick> <msg> -- gets " \
"random or [#n]th quote by <nick> 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+<?[^\w]?(\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__