Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a 'listfacts' command to the Infobot plugin. #1

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
72 changes: 49 additions & 23 deletions plugin.py
Expand Up @@ -229,6 +229,11 @@ def getNumFacts(self, channel):
((Is, Are), _) = self._getDb(channel)
return len(Are.keys()) + len(Is.keys())

def getFacts(self, channel, partial):
((Is, Are), _) = self._getDb(channel)
return '[' + ', '.join(Are.keys()) + '], [' + ', '.join(Is.keys()) + ']'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using string formatting is preferred over string concatenation. Using Supybot's utils.str library would be useful here:
return utils.str.format('[%L], [%L]', Are.keys(), Is.Keys())



class SqliteInfobotDB(object):
def __init__(self, filename):
self.filename = filename
Expand All @@ -238,7 +243,7 @@ def __init__(self, filename):

def _getDb(self, channel):
try:
import sqlite
import sqlite3 as sqlite
except ImportError:
raise callbacks.Error, 'You need to have PySQLite installed to '\
'use this plugin. Download it at '\
Expand Down Expand Up @@ -290,37 +295,37 @@ def incResponses(self):
def changeIs(self, channel, factoid, replacer):
(db, filename) = self._getDb(channel)
cursor = db.cursor()
cursor.execute("""SELECT value FROM isFacts WHERE key LIKE %s""",
factoid)
if cursor.rowcount == 0:
cursor.execute("""SELECT value FROM isFacts WHERE key LIKE ?""",
(factoid,))
if (len(cursor.fetchall()) == 0):
raise dbi.NoRecordError
old = cursor.fetchone()[0]
if replacer is not None:
cursor.execute("""UPDATE isFacts SET value=%s WHERE key LIKE %s""",
replacer(old), factoid)
cursor.execute("""UPDATE isFacts SET value=? WHERE key LIKE ?""",
(replacer(old), factoid))
db.commit()
self.incChanges()

def getIs(self, channel, factoid):
(db, filename) = self._getDb(channel)
cursor = db.cursor()
cursor.execute("""SELECT value FROM isFacts WHERE key LIKE %s""",
factoid)
cursor.execute("""SELECT value FROM isFacts WHERE key LIKE ?""",
(factoid,))
ret = cursor.fetchone()[0]
self.incResponses()
return ret

def setIs(self, channel, fact, oid):
(db, filename) = self._getDb(channel)
cursor = db.cursor()
cursor.execute("""INSERT INTO isFacts VALUES (%s, %s)""", fact, oid)
cursor.execute("""INSERT INTO isFacts VALUES (?, ?)""", (fact, oid))
db.commit()
self.incChanges()

def delIs(self, channel, factoid):
(db, filename) = self._getDb(channel)
cursor = db.cursor()
cursor.execute("""DELETE FROM isFacts WHERE key LIKE %s""", factoid)
cursor.execute("""DELETE FROM isFacts WHERE key LIKE ?""", (factoid,))
if cursor.rowcount == 0:
raise dbi.NoRecordError
db.commit()
Expand All @@ -329,43 +334,43 @@ def delIs(self, channel, factoid):
def hasIs(self, channel, factoid):
(db, _) = self._getDb(channel)
cursor = db.cursor()
cursor.execute("""SELECT * FROM isFacts WHERE key LIKE %s""", factoid)
return cursor.rowcount == 1
cursor.execute("""SELECT * FROM isFacts WHERE key LIKE ?""", (factoid,))
return (len(cursor.fetchall()) == 1)

def changeAre(self, channel, factoid, replacer):
(db, filename) = self._getDb(channel)
cursor = db.cursor()
cursor.execute("""SELECT value FROM areFacts WHERE key LIKE %s""",
factoid)
if cursor.rowcount == 0:
cursor.execute("""SELECT value FROM areFacts WHERE key LIKE ?""",
(factoid,))
if (len(cursor.fetchall()) == 0):
raise dbi.NoRecordError
old = cursor.fetchone()[0]
if replacer is not None:
sql = """UPDATE areFacts SET value=%s WHERE key LIKE %s"""
cursor.execute(sql, replacer(old), factoid)
sql = """UPDATE areFacts SET value=? WHERE key LIKE ?"""
cursor.execute(sql, (replacer(old), factoid))
db.commit()
self.incChanges()

def getAre(self, channel, factoid):
(db, filename) = self._getDb(channel)
cursor = db.cursor()
cursor.execute("""SELECT value FROM areFacts WHERE key LIKE %s""",
factoid)
cursor.execute("""SELECT value FROM areFacts WHERE key LIKE ?""",
(factoid,))
ret = cursor.fetchone()[0]
self.incResponses()
return ret

def setAre(self, channel, fact, oid):
(db, filename) = self._getDb(channel)
cursor = db.cursor()
cursor.execute("""INSERT INTO areFacts VALUES (%s, %s)""", fact, oid)
cursor.execute("""INSERT INTO areFacts VALUES (?, ?)""", (fact, oid))
db.commit()
self.incChanges()

def delAre(self, channel, factoid):
(db, filename) = self._getDb(channel)
cursor = db.cursor()
cursor.execute("""DELETE FROM areFacts WHERE key LIKE %s""", factoid)
cursor.execute("""DELETE FROM areFacts WHERE key LIKE ?""", (factoid,))
if cursor.rowcount == 0:
raise dbi.NoRecordError
db.commit()
Expand All @@ -374,8 +379,8 @@ def delAre(self, channel, factoid):
def hasAre(self, channel, factoid):
(db, _) = self._getDb(channel)
cursor = db.cursor()
cursor.execute("""SELECT * FROM areFacts WHERE key LIKE %s""", factoid)
return cursor.rowcount == 1
cursor.execute("""SELECT * FROM areFacts WHERE key LIKE ?""", (factoid,))
return (len(cursor.fetchall()) == 1)

def getDunno(self):
return utils.iter.choice(dunnos) + utils.iter.choice(ends)
Expand Down Expand Up @@ -406,6 +411,17 @@ def getNumFacts(self, channel):
isFacts = int(cursor.fetchone()[0])
return areFacts + isFacts

def getFacts(self, channel, partial):
(db, _) = self._getDb(channel)
areCursor = db.cursor()
isCursor = db.cursor()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is going to work how you expect. Just get one cursor and do the queries in steps.

key = partial or '%'
areCursor.execute("""SELECT key FROM areFacts WHERE key LIKE ?""",
(key,))
isCursor.execute("""SELECT key FROM isFacts WHERE key LIKE ?""", (key,))
return '[' + ', '.join([obj[0] for obj in areCursor.fetchall()]) + \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, use of string formatting would be good here.

'], [' + ', '.join([obj[0] for obj in isCursor.fetchall()]) + ']'


InfobotDB = plugins.DB('Infobot',
{'sqlite': SqliteInfobotDB,
Expand Down Expand Up @@ -769,6 +785,16 @@ def doFactoid(self, irc, msg, match):
if msg.addressed:
self.confirm()

def listfacts(self, irc, msg, args, channel, partial):
"""[<channel> <partial>]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"[<channel>] [<partial>]" is more accurate. Either can be provided without the other. The existing line implies that both must be given together or none should be given.


Returns the facts in the Infobot database matching <partial> (when
using an sqlite database; returns all facts otherwise).
"""
facts = self.db.getFacts(channel, partial)
irc.reply(facts)
listfacts = wrap(listfacts, ['channeldb', optional('text')])

def stats(self, irc, msg, args, channel):
"""[<channel>]

Expand Down