Converted to use dbi.

This commit is contained in:
Jeremy Fincher 2004-08-11 05:48:28 +00:00
parent e5e98fdc2f
commit c941521ba0
2 changed files with 71 additions and 88 deletions

View File

@ -45,6 +45,7 @@ import time
import random import random
import itertools import itertools
import supybot.dbi as dbi
import supybot.conf as conf import supybot.conf as conf
import supybot.utils as utils import supybot.utils as utils
import supybot.ircdb as ircdb import supybot.ircdb as ircdb
@ -97,19 +98,18 @@ class DunnoDBInterface(object):
raise NotImplementedError raise NotImplementedError
class FlatfileDunnoDB(DunnoDBInterface): class DbiDunnoDB(DunnoDBInterface):
class DunnoDB(plugins.FlatfileDB): class DunnoDB(dbi.DB):
def serialize(self, record): class Record(object):
return csv.join(map(str, record)) __metaclass__ = dbi.Record
__fields__ = [
def deserialize(self, s): 'at',
L = csv.split(s) 'by',
L[0] = float(L[0]) ('text', (str, '')),
L[1] = int(L[1]) ]
return L
def __init__(self): def __init__(self):
self.filenames = sets.Set() self.filenames = sets.Set()
def _getDb(self, channel): def _getDb(self, channel):
# Why cache? It gains very little. # Why cache? It gains very little.
filename = plugins.makeChannelFilename(channel, 'Dunno.db') filename = plugins.makeChannelFilename(channel, 'Dunno.db')
@ -124,53 +124,41 @@ class FlatfileDunnoDB(DunnoDBInterface):
except EnvironmentError: except EnvironmentError:
pass pass
def add(self, channel, dunno, by, at): def add(self, channel, text, by, at):
db = self._getDb(channel) db = self._getDb(channel)
return db.addRecord([at, by, dunno]) return db.add(db.Record(at=at, by=by, text=text))
def remove(self, channel, id): def remove(self, channel, id):
db = self._getDb(channel) db = self._getDb(channel)
db.delRecord(id) db.remove(id)
def get(self, channel, id): def get(self, channel, id):
db = self._getDb(channel) db = self._getDb(channel)
L = db.getRecord(id) return db.get(id)
L.reverse()
return L # [dunno, by, at]
def change(self, channel, id, f): def change(self, channel, id, f):
db = self._getDb(channel) db = self._getDb(channel)
(at, by, dunno) = db.getRecord(id) dunno = db.get(id)
newDunno = f(dunno) dunno.text = f(dunno.text)
db.setRecord(id, [at, by, newDunno]) db.set(id, dunno)
def random(self, channel): def random(self, channel):
db = self._getDb(channel) db = self._getDb(channel)
x = random.choice(db.records()) return random.choice(db)
if x:
(id, (at, by, dunno)) = x
return (id, dunno)
else:
return None
def search(self, channel, p): def search(self, channel, p):
L = []
db = self._getDb(channel) db = self._getDb(channel)
for (id, (at, by, dunno)) in db.records(): return db.select(p)
if p(dunno):
L.append((id, dunno))
return L
def size(self, channel): def size(self, channel):
try: try:
db = self._getDb(channel) db = self._getDb(channel)
return itertools.ilen(db.records()) return itertools.ilen(db)
except EnvironmentError, e: except EnvironmentError, e:
return 0 return 0
def DunnoDB():
DunnoDB = FlatfileDunnoDB return DbiDunnoDB()
class Dunno(callbacks.Privmsg): class Dunno(callbacks.Privmsg):
"""This plugin was written initially to work with MoobotFactoids, the two """This plugin was written initially to work with MoobotFactoids, the two
@ -191,7 +179,7 @@ class Dunno(callbacks.Privmsg):
if ircutils.isChannel(channel): if ircutils.isChannel(channel):
dunno = self.db.random(channel) dunno = self.db.random(channel)
if dunno is not None: if dunno is not None:
dunno = dunno[1] dunno = dunno.text
prefixName = self.registryValue('prefixNick', channel) prefixName = self.registryValue('prefixNick', channel)
dunno = plugins.standardSubstitute(irc, msg, dunno) dunno = plugins.standardSubstitute(irc, msg, dunno)
irc.reply(dunno, prefixName=prefixName) irc.reply(dunno, prefixName=prefixName)
@ -230,8 +218,13 @@ class Dunno(callbacks.Privmsg):
irc.errorNotRegistered() irc.errorNotRegistered()
return return
id = privmsgs.getArgs(args) id = privmsgs.getArgs(args)
(dunno, dunnoBy, at) = self.db.get(channel, id) try:
if by != dunnoBy: id = int(id)
except ValueError:
irc.error('Invalid id: %r' % id)
return
dunno = self.db.get(channel, id)
if by != dunno.by:
cap = ircdb.makeChannelCapability(channel, 'op') cap = ircdb.makeChannelCapability(channel, 'op')
if not ircdb.users.checkCapability(cap): if not ircdb.users.checkCapability(cap):
irc.errorNoCapability(cap) irc.errorNoCapability(cap)
@ -251,9 +244,9 @@ class Dunno(callbacks.Privmsg):
""" """
channel = privmsgs.getChannel(msg, args) channel = privmsgs.getChannel(msg, args)
text = privmsgs.getArgs(args) text = privmsgs.getArgs(args)
def p(s): def p(dunno):
return text.lower() in s.lower() return text.lower() in dunno.text.lower()
ids = [str(id) for (id, _) in self.db.search(channel, p)] ids = [str(dunno.id) for dunno in self.db.search(channel, p)]
if ids: if ids:
s = 'Dunno search for %r (%s found): %s.' % \ s = 'Dunno search for %r (%s found): %s.' % \
(text, len(ids), utils.commaAndify(ids)) (text, len(ids), utils.commaAndify(ids))
@ -275,12 +268,12 @@ class Dunno(callbacks.Privmsg):
irc.error('%r is not a valid dunno id.' % id) irc.error('%r is not a valid dunno id.' % id)
return return
try: try:
(dunno, by, at) = self.db.get(channel, id) dunno = self.db.get(channel, id)
name = ircdb.users.getUser(by).name name = ircdb.users.getUser(dunno.by).name
at = time.localtime(at) at = time.localtime(dunno.at)
timeStr = time.strftime(conf.supybot.humanTimestampFormat(), at) timeStr = time.strftime(conf.supybot.humanTimestampFormat(), at)
irc.reply("Dunno #%s: %r (added by %s at %s)" % \ irc.reply("Dunno #%s: %r (added by %s at %s)" % \
(id, dunno, name, timeStr)) (id, dunno.text, name, timeStr))
except KeyError: except KeyError:
irc.error('No dunno found with that id.') irc.error('No dunno found with that id.')

View File

@ -31,18 +31,12 @@
from testsupport import * from testsupport import *
try: class DunnoTestCase(ChannelPluginTestCase, PluginDocumentation):
import sqlite
except ImportError:
sqlite = None
if sqlite is not None:
class DunnoTestCase(PluginTestCase, PluginDocumentation):
plugins = ('Dunno', 'User') plugins = ('Dunno', 'User')
def setUp(self): def setUp(self):
PluginTestCase.setUp(self) PluginTestCase.setUp(self)
self.prefix = 'foo!bar@baz' self.prefix = 'foo!bar@baz'
self.assertNotError('register tester moo') self.assertNotError('register tester moo', private=True)
def testDunnoAdd(self): def testDunnoAdd(self):
self.assertNotError('dunno add moo') self.assertNotError('dunno add moo')
@ -54,24 +48,20 @@ if sqlite is not None:
def testDunnoSearch(self): def testDunnoSearch(self):
self.assertNotError('dunno add foo') self.assertNotError('dunno add foo')
self.assertError('dunno search moo') self.assertRegexp('dunno search moo', 'No dunnos found')
self.assertNotError('dunno add moo') self.assertNotError('dunno add moo')
self.assertResponse('dunno search moo', 'Dunno search for \'moo\' ' self.assertRegexp('dunno search moo', r'\(1 found\)')
'(1 found): 2.') self.assertRegexp('dunno search m', r'\(1 found\)')
self.assertResponse('dunno search m', 'Dunno search for \'m\' '
'(1 found): 2.')
# Test multiple adds # Test multiple adds
for i in range(5): for i in range(5):
self.assertNotError('dunno add moo%s' % i) self.assertNotError('dunno add moo%s' % i)
self.assertResponse('dunno search moo', self.assertRegexp('dunno search moo', r'\(6 found\)')
'Dunno search for \'moo\' (6 found): '
'2, 3, 4, 5, 6, and 7.')
def testDunnoGet(self): def testDunnoGet(self):
self.assertNotError('dunno add moo') self.assertNotError('dunno add moo')
self.assertResponse('dunno get 1', 'Dunno #1: \'moo\'.') self.assertRegexp('dunno get 1', r'#1.*moo')
self.assertNotError('dunno add $who') self.assertNotError('dunno add $who')
self.assertResponse('dunno get 2', 'Dunno #2: \'$who\'.') self.assertRegexp('dunno get 2', r'#2.*\$who')
self.assertError('dunno get 3') self.assertError('dunno get 3')
self.assertError('dunno get a') self.assertError('dunno get a')