diff --git a/plugin.py b/plugin.py index 47330fb..bfaf1a1 100644 --- a/plugin.py +++ b/plugin.py @@ -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,22 +295,22 @@ 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 @@ -313,14 +318,14 @@ def getIs(self, channel, factoid): 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,28 +334,28 @@ 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 @@ -358,14 +363,14 @@ def getAre(self, channel, factoid): 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() + 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()]) + \ + '], [' + ', '.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): + """[ ] + + Returns the facts in the Infobot database matching (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): """[]