clean up karma module

This commit is contained in:
Mike Bloy 2010-10-24 09:36:15 -05:00
parent b1fe99d5a2
commit e453778791
1 changed files with 59 additions and 54 deletions

View File

@ -46,68 +46,73 @@ class Karma(Module):
self.queryre = re.compile(querypattern)
def do(self, connection, event, nick, userhost, replypath, what, admin_unlocked):
"""look for karma strings."""
"""look for karma strings at the start of messages"""
if (self.karmare.match(what)):
self.handle_karma_change(connection, nick, userhost, replypath, what)
elif (self.queryre.match(what)):
self.handle_karma_query(connection, nick, userhost, replypath, what)
match = self.karmare.match(what)
key = match.group(1)
value = match.group(2)
if (value == '++'):
value = 1;
def handle_karma_change(self, connection, nick, userhost, replypath, what):
"""
handle the karma change and storage.
"""
match = self.karmare.match(what)
key = match.group(1)
value = match.group(2)
if (value == '++'):
value = 1;
else:
value = -1;
# do karma recording
karma = shelve.open(self.karmafile)
try:
oldvalue = 0;
if karma.has_key(key):
oldvalue = karma[key]
newvalue = oldvalue + value;
karma[key] = newvalue;
finally:
karma.close()
trend = shelve.open(self.trendfile)
try:
nickpos = nick + "_pos"
nickneg = nick + "_neg"
trend_pos = 0;
trend_neg = 0;
if trend.has_key(nickpos):
trend_pos = trend[nickpos]
if trend.has_key(nickneg):
trend_neg = trend[nickneg]
if value > 0:
trend_pos = trend_pos + 1
else:
value = -1;
trend_neg = trend_neg + 1
trend[nickpos] = trend_pos;
trend[nickneg] = trend_neg;
finally:
trend.close();
# do karma recording
karma = shelve.open(self.karmafile)
try:
oldvalue = 0;
if karma.has_key(key):
oldvalue = karma[key]
newvalue = oldvalue + value;
karma[key] = newvalue;
finally:
karma.close()
reply = "karma change for '" + key + "' (" + str(value) + ") by " + nick
self.reply(connection, replypath, reply)
trend = shelve.open(self.trendfile)
try:
nickpos = nick + "_pos"
nickneg = nick + "_neg"
trend_pos = 0;
trend_neg = 0;
if trend.has_key(nickpos):
trend_pos = trend[nickpos]
if trend.has_key(nickneg):
trend_neg = trend[nickneg]
if value > 0:
trend_pos = trend_pos + 1
else:
trend_neg = trend_neg + 1
trend[nickpos] = trend_pos;
trend[nickneg] = trend_neg;
finally:
trend.close();
def handle_karma_query(self, connection, nick, userhost, replypath, what):
match = self.queryre.match(what)
key = match.group(1)
reply = "karma change for '" + key + "' (" + str(value) + ") by " + nick
self.reply(connection, replypath, reply)
return
karma = shelve.open(self.karmafile, "r")
reply = key + ' has no karma'
try:
if karma.has_key(key):
value = karma[key]
reply = key + ' has ' + str(value) + ' points of karma'
finally:
karma.close()
if (self.queryre.match(what)):
# tell about karma
match = self.queryre.match(what)
key = match.group(1)
karma = shelve.open(self.karmafile, "r")
reply = key + ' has no karma'
try:
if karma.has_key(key):
value = karma[key]
reply = key + ' has ' + str(value) + ' points of karma'
finally:
karma.close()
self.reply(connection, replypath, reply)
return
self.reply(connection, replypath, reply)
if __name__ == "__main__":
print "Hello World"