Let's properly mimic Infobot's stats command and add some initial items to

a freshly created db.
This commit is contained in:
James Vega 2004-08-29 17:05:14 +00:00
parent f73acda4ff
commit b111642379

View File

@ -40,12 +40,14 @@ import supybot.plugins as plugins
import os import os
import re import re
import time
import random import random
import cPickle as pickle import cPickle as pickle
import supybot.dbi as dbi 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.world as world
import supybot.ircmsgs as ircmsgs import supybot.ircmsgs as ircmsgs
import supybot.ircutils as ircutils import supybot.ircutils as ircutils
import supybot.privmsgs as privmsgs import supybot.privmsgs as privmsgs
@ -98,21 +100,42 @@ confirms = ['10-4',
'Got it', 'Got it',
'Gotcha', 'Gotcha',
'I hear ya'] 'I hear ya']
initialIs = {'who': '<reply>',
'what': '<reply>',
'when': '<reply>',
'where': '<reply>',
'why': '<reply>',
'it': '<reply>',
}
initialAre = {'who': '<reply>',
'what': '<reply>',
'when': '<reply>',
'where': '<reply>',
'why': '<reply>',
'it': '<reply>',
'roses': 'red',
'violets': 'blue',
}
class PickleInfobotDB(object): class PickleInfobotDB(object):
def __init__(self): def __init__(self):
self._changes = 0
self._responses = 0
try: try:
fd = file(filename) fd = file(filename)
except EnvironmentError: except EnvironmentError:
self._is = utils.InsensitivePreservingDict() self._is = utils.InsensitivePreservingDict()
self._are = utils.InsensitivePreservingDict() self._are = utils.InsensitivePreservingDict()
for (k, v) in initialIs.iteritems():
self.setIs(k, v)
for (k, v) in initialAre.iteritems():
self.setAre(k, v)
self._changes = 0
else: else:
try: try:
(self._is, self._are) = pickle.load(fd) (self._is, self._are) = pickle.load(fd)
except cPickle.UnpicklingError, e: except cPickle.UnpicklingError, e:
raise dbi.InvalidDBError, str(e) raise dbi.InvalidDBError, str(e)
self._changes = 0
self._responses = 0
def flush(self): def flush(self):
fd = utils.transactionalFile(filename, 'wb') fd = utils.transactionalFile(filename, 'wb')
@ -196,6 +219,9 @@ class PickleInfobotDB(object):
def getResponseCount(self): def getResponseCount(self):
return self._responses return self._responses
def getNumFacts(self):
return len(self._are.keys()) + len(self._is.keys())
class SqliteInfobotDB(object): class SqliteInfobotDB(object):
def __init__(self): def __init__(self):
self._changes = 0 self._changes = 0
@ -216,6 +242,11 @@ class SqliteInfobotDB(object):
key TEXT UNIQUE ON CONFLICT REPLACE, key TEXT UNIQUE ON CONFLICT REPLACE,
value TEXT value TEXT
);""") );""")
for (k, v) in initialIs.iteritems():
self.setIs(k, v)
for (k, v) in initialAre.iteritems():
self.setAre(k, v)
self._changes = 0
db.commit() db.commit()
return db return db
except sqlite.DatabaseError, e: except sqlite.DatabaseError, e:
@ -322,6 +353,15 @@ class SqliteInfobotDB(object):
def getResponseCount(self): def getResponseCount(self):
return self._responses return self._responses
def getNumFacts(self):
db = self._getDb()
cursor = db.cursor()
cursor.execute("""SELECT COUNT(*) FROM areFacts""")
areFacts = int(cursor.fetchone()[0])
cursor.execute("""SELECT COUNT(*) FROM isFacts""")
isFacts = int(cursor.fetchone()[0])
return areFacts + isFacts
def InfobotDB(): def InfobotDB():
return SqliteInfobotDB() return SqliteInfobotDB()
@ -620,10 +660,22 @@ class Infobot(callbacks.PrivmsgCommandAndRegexp):
Returns the number of changes and requests made to the Infobot database Returns the number of changes and requests made to the Infobot database
since the plugin was loaded. since the plugin was loaded.
""" """
irc.reply('There have been %s answered and %s made ' changes = self.db.getChangeCount()
'to the database since this plugin was loaded.' % responses = self.db.getResponseCount()
(utils.nItems('request', self.db.getResponseCount()), now = time.time()
utils.nItems('change', self.db.getChangeCount()))) diff = int(now - world.startedAt)
mode = {True: 'optional', False: 'require'}
answer = self.registryValue('answerUnaddressedQuestions')
irc.reply('Since %s, there %s been %s and %s. I have been awake for %s'
' this session, and currently reference %s. Addressing is in'
' %s mode.' % (time.ctime(world.startedAt),
utils.has(changes),
utils.nItems('modification', changes),
utils.nItems('question', responses),
utils.timeElapsed(int(now - world.startedAt)),
utils.nItems('factoid',self.db.getNumFacts()),
mode[answer]))
status=stats
def tell(self, irc, msg, args): def tell(self, irc, msg, args):
"""<nick> [about] <factoid> """<nick> [about] <factoid>
@ -648,7 +700,6 @@ class Infobot(callbacks.PrivmsgCommandAndRegexp):
self.dunno() self.dunno()
Class = Infobot Class = Infobot
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: # vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: