""" RepList.py (C)2008 Don A. Hanlen I make no claims, and take no reponsibility, for use or abuse of this code! I require those who DO use it to cite its source! If this code is used for commercial purposes I expect a (negotiable) cut! This file is rsponsible for maintaining reputation info for rePYbot(c). Adjunct files: rbos.py repDict = {} ## reputation dictionary ## entry = ['NaMe': [int:reputation, ## ['complainers', etc], ## ['vouchers', etc], ## ['complaints', etc], ## [int:compNNs, etc], ## ['vouches', etc], ## [int:vouchNNs, etc]]] """ import rbos from string import split True = 1==1 False = not True class RepList: """ maintain reputation list """ def __init__(self, filename): """ fname is sans extension """ self.repDict = {} self.filename = filename self.getrepDict(self.filename) def repList(self, asker, user, ftell): """ output a reputation list using exterior function ftell """ if self.repDict.has_key(user): ftell(asker, "%s's complainers %s" %(user, self.repDict[user][1])) ftell(asker, "%s's vouchers %s" %(user, self.repDict[user][2])) ftell(asker, "%s's complaints %s" %(user, self.repDict[user][3])) ftell(asker, "%s's vouches %s" %(user, self.repDict[user][5])) else: ftell(asker, "I have no registered info about %s." %user) def dRep(self, name): """ return name's reputation """ if self.repDict.has_key(name): return self.repDict[name][0] return 0 def hasTouched(self, name1, name2): """ True if vouch/complaint BY name2 ABOUT name1 else False """ if self.repDict.has_key(name1) and ( name2 in self.repDict[name1][1] or name2 in self.repDict[name1][2]): return True return False def getrepDict(self, fileName): """ read reputation data from file make a .back first, in case the .bck file ops screw up """ rbos.copy(fileName+'.txt', fileName+'.back') fRep = open(fileName+'.txt', 'r') t = fRep.read() fRep.close() t = split(t, '\n') for x in t: xs = split(x) if len(xs)==4: if xs[3] == 'c': self.repComplain(xs[:-1], False) elif xs[3] == 'v': self.repVouch(xs[:-1], False) def repVouch(self, xs, dump=True): """ ['voucher', 'vouchee', int:weight], refrestDict return "Voucher registered." """ if self.repDict.has_key(xs[0]): if xs[1] in self.repDict[xs[0]][5]: return "You have already vouched %s." %xs[1] if xs[1] in self.repDict[xs[0]][3]: return "You have to withdraw your complaint about %s first." %xs[1] p = self.insertPos(xs[1], self.repDict[xs[0]][5]) self.repDict[xs[0]][5].insert(p, xs[1]) self.repDict[xs[0]][6].insert(p, int(xs[2])) else: self.repDict[xs[0]] = [0,[],[],[],[],[xs[1]],[int(xs[2])]] if self.repDict.has_key(xs[1]): p = self.insertPos(xs[0], self.repDict[xs[1]][2]) self.repDict[xs[1]][2].insert(p, xs[0]) self.repDict[xs[1]][0] = self.repDict[xs[1]][0] + int(xs[2]) else: self.repDict[xs[1]] = [int(xs[2]),[],[xs[0]],[],[],[],[]] if dump: self.dumprepDict() return "Voucher registered." def repComplain(self, xs, dump=True): """ ['complainer', 'complainee', int:weight], refreshDict return "Complaint registered." """ if self.repDict.has_key(xs[0]): if xs[1] in self.repDict[xs[0]][3]: return "You have already complained about %s." %xs[1] if xs[1] in self.repDict[xs[0]][5]: return "You have to withdraw your vouch for %s first." %xs[1] p = self.insertPos(xs[1], self.repDict[xs[0]][3]) self.repDict[xs[0]][3].insert(p, xs[1]) self.repDict[xs[0]][4].insert(p, int(xs[2])) else: self.repDict[xs[0]] = [0,[],[],[xs[1]],[int(xs[2])],[],[]] if self.repDict.has_key(xs[1]): p = self.insertPos(xs[0], self.repDict[xs[1]][1]) self.repDict[xs[1]][1].insert(p, xs[0]) self.repDict[xs[1]][0] = self.repDict[xs[1]][0] - int(xs[2]) else: self.repDict[xs[1]] = [-int(xs[2]),[xs[0]],[],[],[],[],[]] if dump: self.dumprepDict() return "Complaint registered." def insertPos(self, lName, lList): """ return int:p = alphabetical insert position in a list. """ p = 0 for x in lList: if lName > x: p = p + 1 return p def repWithdraw(self, xs, dump=True): """ ['withdrawer', 'withdrawee'] return "Vouch withdrawn." | "You have no vouches or complaints for %s" % xs[1] """ if self.repDict.has_key(xs[0]) and self.repDict.has_key(xs[1]): if xs[1] in self.repDict[xs[0]][3]: p = self.repDict[xs[0]][3].index(xs[1]) self.repDict[xs[0]][3].pop(p) change = self.repDict[xs[0]][4].pop(p) self.repDict[xs[1]][0] = self.repDict[xs[1]][0] + change p = self.repDict[xs[1]][1].index(xs[0]) self.repDict[xs[1]][1].pop(p) if dump: self.dumprepDict() return "Complaint withdrawn." if xs[1] in self.repDict[xs[0]][5]: p = self.repDict[xs[0]][5].index(xs[1]) self.repDict[xs[0]][5].pop(p) change = self.repDict[xs[0]][6].pop(p) self.repDict[xs[1]][0] = self.repDict[xs[1]][0] - change p = self.repDict[xs[1]][2].index(xs[0]) self.repDict[xs[1]][2].pop(p) if dump: self.dumprepDict() return "Vouch withdrawn." return "You have no vouches or complaints for %s" % xs[1] def dumprepDict(self): """ back up and save reputation dictionary to a file .bck, not .back """ fRep = rbos.backupOpen(self.filename, 'txt') for x in self.repDict.keys(): for i in range(0, len(self.repDict[x][3])): fRep.write("%s %s %d c\n" %(x, self.repDict[x][3][i], self.repDict[x][4][i])) for i in range(0, len(self.repDict[x][5])): fRep.write("%s %s %d v\n" %(x, self.repDict[x][5][i], self.repDict[x][6][i])) fRep.close()