Updated to use plugins.ChannelUserDB.

This commit is contained in:
Jeremy Fincher 2004-02-08 06:48:07 +00:00
parent 04b2ecd5e6
commit 645d654ada

View File

@ -152,59 +152,20 @@ class UserStat(ChannelStat):
self.doPayload(msg.args[0], msg.args[2]) self.doPayload(msg.args[0], msg.args[2])
self.kicks += 1 self.kicks += 1
class StatsDB(object): class StatsDB(plugins.ChannelUserDB):
def __init__(self, filename): def __init__(self, *args, **kwargs):
self.filename = filename
self.channels = ircutils.IrcDict()
self.channelStats = ircutils.IrcDict() self.channelStats = ircutils.IrcDict()
try: plugins.ChannelUserDB.__init__(self, *args, **kwargs)
fd = file(filename)
except EnvironmentError, e: def serialize(self, v):
log.info('Couldn\'t open %s: %s', filename, e) return v.values()
return
lineno = 0 def deserialize(self, channel, id, L):
for line in fd: L = map(int, L)
lineno += 1 if id == 'channelStats':
line = line.rstrip('\r\n') return ChannelStat(L)
try:
(channelOrId, rest) = line.split(':', 1)
if ircutils.isChannel(channelOrId):
channel = channelOrId
values = map(int, rest.split(','))
self.channelStats[channel] = ChannelStat(*values)
else: else:
id = int(channelOrId) return UserStat(L)
(channel, rest) = rest.split(':', 1)
d = self.channels.setdefault(channel, {})
values = map(int, rest.split(','))
d[id] = UserStat(id, *values)
except ValueError:
log.warning('Invalid line (#%s): %r', lineno, line)
continue
fd.close()
def flush(self):
fd = file(self.filename, 'w')
L = self.channelStats.items()
L.sort()
for (channel, stat) in L:
fd.write('%s:%s' % (channel, ','.join(map(str, stat.values()))))
fd.write(os.linesep)
if channel not in self.channels:
continue
d = self.channels[channel]
LL = d.items()
LL.sort()
for (id, stat) in LL:
fd.write('%s:%s:%s' % (id, channel,
','.join(map(str, stat.values()))))
fd.write(os.linesep)
fd.close()
def close(self):
self.flush()
self.channels.clear()
self.channelStats.clear()
def addMsg(self, msg, id=None): def addMsg(self, msg, id=None):
channel = msg.args[0] channel = msg.args[0]
@ -223,12 +184,11 @@ class StatsDB(object):
self.channels[channel][id] = UserStat(id) self.channels[channel][id] = UserStat(id)
self.channels[channel][id].addMsg(msg) self.channels[channel][id].addMsg(msg)
def get(self, channel, id=None): def getChannelStats(self, channel):
if id is None: return self[channel, -1]
return self.channelStats[channel]
else:
return self.channels[channel][id]
def getUserStats(self, channel, id):
return self[channel, id]
class ChannelStats(callbacks.Privmsg): class ChannelStats(callbacks.Privmsg):
noIgnore = True noIgnore = True
@ -329,7 +289,7 @@ class ChannelStats(callbacks.Privmsg):
else: else:
id = ircdb.users.getUserId(name) id = ircdb.users.getUserId(name)
try: try:
stats = self.db.get(channel, id) stats = self.db.getUserStats(channel, id)
s = '%s has sent %s; a total of %s, %s, ' \ s = '%s has sent %s; a total of %s, %s, ' \
'%s, and %s; %s of those messages %s' \ '%s, and %s; %s of those messages %s' \
'%s has joined %s, parted %s, quit %s, kicked someone %s, ' \ '%s has joined %s, parted %s, quit %s, kicked someone %s, ' \
@ -362,7 +322,7 @@ class ChannelStats(callbacks.Privmsg):
""" """
channel = privmsgs.getChannel(msg, args) channel = privmsgs.getChannel(msg, args)
try: try:
stats = self.db.get(channel) stats = self.db.getChannelStats(channel)
s = 'On %s there have been %s messages, containing %s ' \ s = 'On %s there have been %s messages, containing %s ' \
'characters, %s, %s, and %s; ' \ 'characters, %s, %s, and %s; ' \
'%s of those messages %s. There have been ' \ '%s of those messages %s. There have been ' \