improve quote retrieval regex and some exception handling
This commit is contained in:
parent
8a93c7f21b
commit
d4ad562a5a
|
@ -63,9 +63,9 @@ def run(func, input):
|
||||||
def do_sieve(sieve, bot, input, func, type, args):
|
def do_sieve(sieve, bot, input, func, type, args):
|
||||||
try:
|
try:
|
||||||
return sieve(bot, input, func, type, args)
|
return sieve(bot, input, func, type, args)
|
||||||
except Exception, e:
|
except Exception:
|
||||||
print 'sieve error',
|
print 'sieve error',
|
||||||
traceback.print_exc(Exception)
|
traceback.print_exc()
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -53,8 +53,8 @@ def reload(init=False):
|
||||||
eval(compile(open(filename, 'U').read(), filename, 'exec'),
|
eval(compile(open(filename, 'U').read(), filename, 'exec'),
|
||||||
globals())
|
globals())
|
||||||
except Exception:
|
except Exception:
|
||||||
traceback.print_exc(Exception)
|
traceback.print_exc()
|
||||||
if init: # stop if there's a syntax error in a core
|
if init: # stop if there's an error (syntax?) in a core
|
||||||
sys.exit() # script on startup
|
sys.exit() # script on startup
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
@ -90,7 +90,7 @@ def reload(init=False):
|
||||||
namespace = {}
|
namespace = {}
|
||||||
eval(code, namespace)
|
eval(code, namespace)
|
||||||
except Exception:
|
except Exception:
|
||||||
traceback.print_exc(Exception)
|
traceback.print_exc()
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# remove plugins already loaded from this filename
|
# remove plugins already loaded from this filename
|
||||||
|
|
|
@ -6,10 +6,9 @@ from util import hook
|
||||||
|
|
||||||
|
|
||||||
def add_quote(db, chan, nick, add_nick, msg):
|
def add_quote(db, chan, nick, add_nick, msg):
|
||||||
now = time.time()
|
|
||||||
db.execute('''insert or fail into quote (chan, nick, add_nick,
|
db.execute('''insert or fail into quote (chan, nick, add_nick,
|
||||||
msg, time) values(?,?,?,?,?)''',
|
msg, time) values(?,?,?,?,?)''',
|
||||||
(chan, nick, add_nick, msg, now))
|
(chan, nick, add_nick, msg, time.time()))
|
||||||
db.commit()
|
db.commit()
|
||||||
|
|
||||||
|
|
||||||
|
@ -33,7 +32,7 @@ def format_quote(q, num, n_quotes):
|
||||||
@hook.command('q')
|
@hook.command('q')
|
||||||
@hook.command
|
@hook.command
|
||||||
def quote(inp, nick='', chan='', db=None):
|
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"
|
"random or [#n]th quote by <nick> or from <#chan>/adds quote"
|
||||||
|
|
||||||
db.execute("create table if not exists 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))")
|
"primary key (chan, nick, msg))")
|
||||||
db.commit()
|
db.commit()
|
||||||
|
|
||||||
try:
|
add = re.match(r"add\W+(\S+?)>?\s+(.*)", inp, re.I)
|
||||||
add = re.match(r"add\s+<?[^\w]?(\S+?)>?\s+(.*)", inp, re.I)
|
retrieve = re.match(r"(\S+)(?:\s+#?(-?\d+))?$", inp)
|
||||||
retrieve = re.match(r"(\S+)(?:\s+#?(-?\d+))?", inp)
|
|
||||||
|
|
||||||
if add:
|
if add:
|
||||||
quoted_nick, msg = add.groups()
|
quoted_nick, msg = add.groups()
|
||||||
try:
|
try:
|
||||||
add_quote(db, chan, quoted_nick, nick, msg)
|
add_quote(db, chan, quoted_nick, nick, msg)
|
||||||
except db.IntegrityError:
|
db.commit()
|
||||||
return "message already stored, doing nothing."
|
except db.IntegrityError:
|
||||||
return "quote added."
|
return "message already stored, doing nothing."
|
||||||
elif retrieve:
|
return "quote added."
|
||||||
select, num = retrieve.groups()
|
elif retrieve:
|
||||||
|
select, num = retrieve.groups()
|
||||||
|
|
||||||
by_chan = False
|
by_chan = False
|
||||||
if select.startswith('#'):
|
if select.startswith('#'):
|
||||||
by_chan = True
|
by_chan = True
|
||||||
quotes = get_quotes_by_chan(db, select)
|
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)
|
|
||||||
else:
|
else:
|
||||||
return quote.__doc__
|
quotes = get_quotes_by_nick(db, chan, select)
|
||||||
finally:
|
|
||||||
db.commit()
|
n_quotes = len(quotes)
|
||||||
db.close()
|
|
||||||
|
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__
|
||||||
|
|
Loading…
Reference in New Issue