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. # Returns (follower, last) tuple.
pass pass
def firsts(self, channel):
pass
def lasts(self, channel):
pass
def pairs(self, channel):
pass
def follows(self, channel):
pass
class SqliteMarkovDB(object): 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, def addPair(self, channel, first, second, follower,
isFirst=False, isLast=False): isFirst=False, isLast=False):
pass pass
@ -80,10 +118,24 @@ class SqliteMarkovDB(object):
# Returns (follower, last) tuple. # Returns (follower, last) tuple.
pass pass
def firsts(self, channel):
pass
def lasts(self, channel):
pass
def pairs(self, channel):
pass
def follows(self, channel):
pass
class DbmMarkovDB(object): class DbmMarkovDB(object):
def __init__(self): def __init__(self, filename):
self.dbs = ircutils.IrcDict() self.dbs = ircutils.IrcDict()
# Stupid anydbm seems to append .db to the end of this.
self.filename = filename.replace('.db', '')
def close(self): def close(self):
for db in self.dbs.values(): for db in self.dbs.values():
@ -91,8 +143,7 @@ class DbmMarkovDB(object):
def _getDb(self, channel): def _getDb(self, channel):
if channel not in self.dbs: if channel not in self.dbs:
# Stupid anydbm seems to append .db to the end of this. filename = plugins.makeChannelFilename(self.filename, channel)
filename = plugins.makeChannelFilename('DbmMarkovDB', channel)
# To keep the code simpler for addPair, I decided not to make # To keep the code simpler for addPair, I decided not to make
# self.dbs[channel]['firsts'] and ['lasts']. Instead, we'll pad # self.dbs[channel]['firsts'] and ['lasts']. Instead, we'll pad
# the words list being sent to addPair such that ['\n \n'] will be # 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] follows = [len(v.split()) for (k,v) in db.iteritems() if '\n' not in k]
return sum(follows) return sum(follows)
def MarkovDB(): MarkovDB = plugins.DB('Markov',
return DbmMarkovDB() {'anydbm': DbmMarkovDB})
class MarkovWorkQueue(threading.Thread): class MarkovWorkQueue(threading.Thread):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):

View File

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