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,60 +152,21 @@ 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
for line in fd:
lineno += 1
line = line.rstrip('\r\n')
try:
(channelOrId, rest) = line.split(':', 1)
if ircutils.isChannel(channelOrId):
channel = channelOrId
values = map(int, rest.split(','))
self.channelStats[channel] = ChannelStat(*values)
else:
id = int(channelOrId)
(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): def deserialize(self, channel, id, L):
fd = file(self.filename, 'w') L = map(int, L)
L = self.channelStats.items() if id == 'channelStats':
L.sort() return ChannelStat(L)
for (channel, stat) in L: else:
fd.write('%s:%s' % (channel, ','.join(map(str, stat.values())))) return UserStat(L)
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]
if ircutils.isChannel(channel): if ircutils.isChannel(channel):
@ -223,13 +184,12 @@ 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
def __init__(self): def __init__(self):
@ -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 ' \