Abstracted out some functionality to plugins.DbiChannelDB.

This commit is contained in:
Jeremy Fincher 2004-09-24 20:06:07 +00:00
parent 5753195f45
commit 7b68eb622d
2 changed files with 54 additions and 63 deletions

View File

@ -64,71 +64,29 @@ conf.registerChannelValue(conf.supybot.plugins.Dunno, 'prefixNick',
registry.Boolean(True, """Determines whether the bot will prefix the nick registry.Boolean(True, """Determines whether the bot will prefix the nick
of the user giving an invalid command to the "dunno" response.""")) of the user giving an invalid command to the "dunno" response."""))
class DunnoRecord(object): class DbiDunnoDB(plugins.DbiChannelDB):
__metaclass__ = dbi.Record class DB(dbi.DB):
class Record(dbi.Record):
__fields__ = [ __fields__ = [
'at', 'at',
'by', 'by',
'text', 'text',
] ]
class DbiDunnoDB(object):
class DunnoDB(dbi.DB):
Record = DunnoRecord
def __init__(self, filename): def __init__(self, filename):
self.dbs = ircutils.IrcDict() # We use self.__class__ here because apparently DB isn't in our
self.filename = filename # scope. python--
self.__parent = super(self.__class__, self)
self.__parent.__init__(filename)
def _getDb(self, channel): def add(self, text, by, at):
filename = plugins.makeChannelFilename(self.filename, channel) return self.__parent.add(self.Record(at=at, by=by, text=text))
if channel in self.dbs:
return self.dbs[channel]
self.dbs[channel] = self.DunnoDB(filename)
return self.dbs[channel]
def close(self): def change(self, id, f):
for db in self.dbs.itervalues(): dunno = self.get(id)
db.close()
def flush(self):
pass
def add(self, channel, text, by, at):
db = self._getDb(channel)
return db.add(db.Record(at=at, by=by, text=text))
def remove(self, channel, id):
db = self._getDb(channel)
db.remove(id)
def get(self, channel, id):
db = self._getDb(channel)
return db.get(id)
def change(self, channel, id, f):
db = self._getDb(channel)
dunno = db.get(id)
dunno.text = f(dunno.text) dunno.text = f(dunno.text)
db.set(id, dunno) self.set(id, dunno)
def random(self, channel): DunnoDB = plugins.DB('Dunno', {'flat': DbiDunnoDB})
db = self._getDb(channel)
return random.choice(db)
def search(self, channel, p):
db = self._getDb(channel)
return db.select(p)
def size(self, channel):
try:
db = self._getDb(channel)
return itertools.ilen(db)
except EnvironmentError, e:
return 0
DunnoDB = plugins.DB('Dunno',
{'flat': 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
@ -218,7 +176,7 @@ class Dunno(callbacks.Privmsg):
text = privmsgs.getArgs(args) text = privmsgs.getArgs(args)
def p(dunno): def p(dunno):
return text.lower() in dunno.text.lower() return text.lower() in dunno.text.lower()
ids = [str(dunno.id) for dunno in self.db.search(channel, p)] ids = [str(dunno.id) for dunno in self.db.select(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))

View File

@ -219,6 +219,39 @@ class ChannelDBHandler(object):
gc.collect() gc.collect()
class DbiChannelDB(object):
"""This just handles some of the general stuff for Channel DBI databases.
Check out plugins/Dunno.py for an example of how to use this."""
def __init__(self, filename):
self.filename = filename
self.dbs = ircutils.IrcDict()
def _getDb(self, channel):
filename = makeChannelFilename(self.filename, channel)
try:
db = self.dbs[channel]
except KeyError:
db = self.DB(filename)
self.dbs[channel] = db
return db
def close(self):
for db in self.dbs.itervalues():
db.close()
def flush(self):
for db in self.dbs.itervalues():
db.flush()
def __getattr__(self, attr):
def _getDbAndDispatcher(channel, *args, **kwargs):
db = self._getDb(channel)
return getattr(db, attr)(*args, **kwargs)
return _getDbAndDispatcher
# XXX This should eventually be gotten rid of in favor of some dbi thing. At
# the very least, it ought to get an interface much closer to dbi.DB.
class ChannelUserDictionary(UserDict.DictMixin): class ChannelUserDictionary(UserDict.DictMixin):
IdDict = dict IdDict = dict
def __init__(self): def __init__(self):