clean up karma module

This commit is contained in:
Mike Bloy 2010-10-24 09:36:15 -05:00
parent b1fe99d5a2
commit e453778791

View File

@ -46,68 +46,73 @@ class Karma(Module):
self.queryre = re.compile(querypattern) self.queryre = re.compile(querypattern)
def do(self, connection, event, nick, userhost, replypath, what, admin_unlocked): 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)): if (self.karmare.match(what)):
self.handle_karma_change(connection, nick, userhost, replypath, what)
match = self.karmare.match(what) elif (self.queryre.match(what)):
key = match.group(1) self.handle_karma_query(connection, nick, userhost, replypath, what)
value = match.group(2)
if (value == '++'): def handle_karma_change(self, connection, nick, userhost, replypath, what):
value = 1; """
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: else:
value = -1; trend_neg = trend_neg + 1
trend[nickpos] = trend_pos;
trend[nickneg] = trend_neg;
finally:
trend.close();
# do karma recording reply = "karma change for '" + key + "' (" + str(value) + ") by " + nick
karma = shelve.open(self.karmafile) self.reply(connection, replypath, reply)
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) def handle_karma_query(self, connection, nick, userhost, replypath, what):
try: match = self.queryre.match(what)
nickpos = nick + "_pos" key = match.group(1)
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();
reply = "karma change for '" + key + "' (" + str(value) + ") by " + nick karma = shelve.open(self.karmafile, "r")
self.reply(connection, replypath, reply) reply = key + ' has no karma'
return 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)): self.reply(connection, replypath, reply)
# 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
if __name__ == "__main__": if __name__ == "__main__":
print "Hello World" print "Hello World"