Add anydbm to our conf.Databases so that Markov can be converted to our

new-style db infrastructure.
This commit is contained in:
James Vega 2004-09-13 17:28:31 +00:00
parent 2bfc2a98bf
commit 7e52305f35
2 changed files with 57 additions and 6 deletions

View File

@ -68,7 +68,45 @@ class MarkovDBInterface(object):
# Returns (follower, last) tuple.
pass
def firsts(self, channel):
pass
def lasts(self, channel):
pass
def pairs(self, channel):
pass
def follows(self, channel):
pass
class SqliteMarkovDB(object):
def __init__(self, filename):
self.dbs = ircutils.IrcDict()
self.filename = filename
def close(self):
for db in self.dbs.values():
db.close()
def _getDb(self, channel):
try:
import sqlite
except ImportError:
raise callbacks.Error, 'You need to have PySQLite installed to '\
'use this plugin. Download it at '\
'<http://pysqlite.sf.net/>'
if channel not in self.dbs:
filename = plugins.makeChannelFilename(self.filename, channel)
if os.path.exists(filename):
self.dbs[channel] = sqlite.connect(filename)
return self.dbs[channel]
#else:
self.dbs[channel] = sqlite.connect(filename)
cursor = self.dbs[channel].cursor()
# TODO Finish the rest of the implementation
return self.dbs[channel]
def addPair(self, channel, first, second, follower,
isFirst=False, isLast=False):
pass
@ -80,10 +118,24 @@ class SqliteMarkovDB(object):
# Returns (follower, last) tuple.
pass
def firsts(self, channel):
pass
def lasts(self, channel):
pass
def pairs(self, channel):
pass
def follows(self, channel):
pass
class DbmMarkovDB(object):
def __init__(self):
def __init__(self, filename):
self.dbs = ircutils.IrcDict()
# Stupid anydbm seems to append .db to the end of this.
self.filename = filename.replace('.db', '')
def close(self):
for db in self.dbs.values():
@ -91,8 +143,7 @@ class DbmMarkovDB(object):
def _getDb(self, channel):
if channel not in self.dbs:
# Stupid anydbm seems to append .db to the end of this.
filename = plugins.makeChannelFilename('DbmMarkovDB', channel)
filename = plugins.makeChannelFilename(self.filename, channel)
# To keep the code simpler for addPair, I decided not to make
# self.dbs[channel]['firsts'] and ['lasts']. Instead, we'll pad
# the words list being sent to addPair such that ['\n \n'] will be
@ -168,8 +219,8 @@ class DbmMarkovDB(object):
follows = [len(v.split()) for (k,v) in db.iteritems() if '\n' not in k]
return sum(follows)
def MarkovDB():
return DbmMarkovDB()
MarkovDB = plugins.DB('Markov',
{'anydbm': DbmMarkovDB})
class MarkovWorkQueue(threading.Thread):
def __init__(self, *args, **kwargs):

View File

@ -663,7 +663,7 @@ class Databases(registry.SpaceSeparatedListOfStrings):
def __call__(self):
v = super(Databases, self).__call__()
if not v:
v = ['flat', 'cdb', 'pickle']
v = ['anydbm', 'cdb', 'flat', 'pickle']
if 'sqlite' in sys.modules:
v.insert(0, 'sqlite')
return v