def fileUpdate(self, keepbackups=True): """ updates the file with the contents of the dictionary Preserves comments and non key:value pairs returns 1 it if succeeds, otherwise 0 """ if os.path.exists(self.filename): fu.backup(self.filename) tempdict = self.data templines = [] for line in fu.readlines(self.filename): templine = line splitline = templine.split(self.seperator) if len(splitline) > 1: tempkey = splitline[0].strip() tempval = splitline[1].strip() if tempkey in self.data: if tempdict[tempkey] == tempval: templines.append(line.strip()) else: templines.append('%-20s : %s'.strip() % (tempkey, tempdict[tempkey])) # del existing pairs from tempdict - so you dont repeat them when u append tempdict del tempdict[tempkey] else: templines.append(line.strip()) for k in sorted(tempdict.iterkeys()): templines.append('%-20s : %s'.strip() % (k, tempdict[k])) fu.writelines(self.filename, templines, True) if not keepbackups: fu.delete(self.filename + '.bak')
def printDictFile(self): print("") if os.path.exists(self.filename): # log('print fd file contents:') for l in (fu.readlines(self.filename)): print l,