PEP8, lots of tweaks/fixes

This commit is contained in:
neersighted 2012-04-02 09:17:55 -07:00
parent 641b770dc3
commit 508fec8ae8
29 changed files with 165 additions and 111 deletions

View file

@ -4,24 +4,27 @@ import time
from util import hook
def format_quote(q, num, n_quotes):
"Returns a formatted string of a quote"
"""Returns a formatted string of a quote"""
ctime, nick, msg = q
return "[%d/%d] <%s> %s" % (num, n_quotes,
nick, msg)
def create_table_if_not_exists(db):
"Creates an empty quote table if one does not already exist"
"""Creates an empty quote table if one does not already exist"""
db.execute("create table if not exists quote"
"(chan, nick, add_nick, msg, time real, deleted default 0, "
"primary key (chan, nick, msg))")
db.commit()
def add_quote(db, chan, nick, add_nick, msg):
"Adds a quote to a nick, returns message string"
"""Adds a quote to a nick, returns message string"""
try:
db.execute('''INSERT OR FAIL INTO quote
(chan, nick, add_nick, msg, time)
db.execute('''INSERT OR FAIL INTO quote
(chan, nick, add_nick, msg, time)
VALUES(?,?,?,?,?)''',
(chan, nick, add_nick, msg, time.time()))
db.commit()
@ -29,30 +32,34 @@ def add_quote(db, chan, nick, add_nick, msg):
return "Message already stored, doing nothing."
return "Quote added."
def del_quote(db, chan, nick, add_nick, msg):
"Deletes a quote from a nick"
"""Deletes a quote from a nick"""
db.execute('''UPDATE quote SET deleted = 1 WHERE
chan=? AND lower(nick)=lower(?) AND msg=msg''')
db.commit()
def get_quote_num(num, count, name):
"Returns the quote number to fetch from the DB"
"""Returns the quote number to fetch from the DB"""
if num: # Make sure num is a number if it isn't false
num = int(num)
if count == 0: # If there are no quotes in the database, raise an Exception.
if count == 0: # Error on no quotes
raise Exception("No quotes found for %s." % name)
if num and num < 0: # If the selected quote is less than 0, count back if possible.
if num and num < 0: # Count back if possible
num = count + num + 1 if num + count > -1 else count + 1
if num and num > count: # If a number is given and and there are not enough quotes, raise an Exception.
raise Exception("I only have %d quote%s for %s." % (count, ('s', '')[count == 1], name))
if num and num > count: # If there are not enough quotes, raise an error
raise Exception("I only have %d quote%s for %s."\
% (count, ('s', '')[count == 1], name))
if num and num == 0: # If the number is zero, set it to one
num = 1
if not num: # If a number is not given, select a random one
num = random.randint(1, count)
return num
def get_quote_by_nick(db, nick, num=False):
"Returns a formatted quote from a nick, random or selected by number"
"""Returns a formatted quote from a nick, random or selected by number"""
count = db.execute('''SELECT COUNT(*) FROM quote WHERE deleted != 1
AND lower(nick) = lower(?)''', [nick]).fetchall()[0][0]
@ -60,17 +67,18 @@ def get_quote_by_nick(db, nick, num=False):
num = get_quote_num(num, count, nick)
except Exception as error_message:
return error_message
quote = db.execute('''SELECT time, nick, msg
FROM quote
WHERE deleted != 1
AND lower(nick) = lower(?)
ORDER BY time
LIMIT ?, 1''', (nick, (num-1))).fetchall()[0]
LIMIT ?, 1''', (nick, (num - 1))).fetchall()[0]
return format_quote(quote, num, count)
def get_quote_by_nick_chan(db, chan, nick, num=False):
"Returns a formatted quote from a nick in a channel, random or selected by number"
"""Returns a formatted quote from a nick in a channel, random or selected by number"""
count = db.execute('''SELECT COUNT(*)
FROM quote
WHERE deleted != 1
@ -88,11 +96,12 @@ def get_quote_by_nick_chan(db, chan, nick, num=False):
AND chan = ?
AND lower(nick) = lower(?)
ORDER BY time
LIMIT ?, 1''', (chan, nick, (num-1))).fetchall()[0]
LIMIT ?, 1''', (chan, nick, (num - 1))).fetchall()[0]
return format_quote(quote, num, count)
def get_quote_by_chan(db, chan, num=False):
"Returns a formatted quote from a channel, random or selected by number"
def get_quote_by_chan(db, chan, num=False):
"""Returns a formatted quote from a channel, random or selected by number"""
count = db.execute('''SELECT COUNT(*)
FROM quote
WHERE deleted != 1
@ -103,14 +112,15 @@ def get_quote_by_chan(db, chan, num=False):
except Exception as error_message:
return error_message
quote = db.execute('''SELECT time, nick, msg
quote = db.execute('''SELECT time, nick, msg
FROM quote
WHERE deleted != 1
AND chan = ?
AND chan = ?
ORDER BY time
LIMIT ?, 1''', (chan, (num -1))).fetchall()[0]
LIMIT ?, 1''', (chan, (num - 1))).fetchall()[0]
return format_quote(quote, num, count)
@hook.command('q')
@hook.command
def quote(inp, nick='', chan='', db=None, notice=None):
@ -128,12 +138,12 @@ def quote(inp, nick='', chan='', db=None, notice=None):
elif retrieve:
select, num = retrieve.groups()
by_chan = True if select.startswith('#') else False
if by_chan:
if by_chan:
return get_quote_by_chan(db, select, num)
else:
return get_quote_by_nick(db, select, num)
elif retrieve_chan:
chan, nick, num = retrieve_chan.groups()
return get_quote_by_nick_chan(db, chan, nick, num)
notice(quote.__doc__)