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):
__fields__ = [ class Record(dbi.Record):
'at', __fields__ = [
'by', 'at',
'text', 'by',
] 'text',
]
def __init__(self, filename):
# We use self.__class__ here because apparently DB isn't in our
# scope. python--
self.__parent = super(self.__class__, self)
self.__parent.__init__(filename)
class DbiDunnoDB(object): def add(self, text, by, at):
class DunnoDB(dbi.DB): return self.__parent.add(self.Record(at=at, by=by, text=text))
Record = DunnoRecord
def __init__(self, filename): def change(self, id, f):
self.dbs = ircutils.IrcDict() dunno = self.get(id)
self.filename = filename dunno.text = f(dunno.text)
self.set(id, dunno)
def _getDb(self, channel): DunnoDB = plugins.DB('Dunno', {'flat': DbiDunnoDB})
filename = plugins.makeChannelFilename(self.filename, channel)
if channel in self.dbs:
return self.dbs[channel]
self.dbs[channel] = self.DunnoDB(filename)
return self.dbs[channel]
def close(self):
for db in self.dbs.itervalues():
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)
db.set(id, dunno)
def random(self, channel):
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):