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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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()) + ']' | ||
|
||
|
||
class SqliteInfobotDB(object): | ||
def __init__(self, filename): | ||
self.filename = filename | ||
|
@@ -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 '\ | ||
|
@@ -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() | ||
|
@@ -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() | ||
|
@@ -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) | ||
|
@@ -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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()]) + \ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
|
@@ -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>] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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>] | ||
|
||
|
There was a problem hiding this comment.
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())