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')
print ("****** %s" % string) if __name__ == "__main__": testfile = './test2.txt' # TEMP DEBUG - DELETE BEFORE: # fu.delete(testfile) fd = FileDict(testfile) fd.fileRead() try: if ('read-count' in fd.data.keys() and int(fd.data['read-count']) > 5): log("DELETING!!!") fu.delete(testfile) fd.data.clear() except: log("readcount failed!!!!!") if not os.path.exists(testfile): log("create a new dict file") newlines = ["TESTING DICTFILES.PY", "- A Script to read and handle Key:Value Pairs embedded files with normal text", "", "read-count :0:fldsjfal", "greetings friendly tester.", "", "c:0", "DD:09", "varA:1",