示例#1
0
    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')
示例#2
0
    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",
                    "testWord :       the WORD : a comment ... and another",
                    "and YET another comment",
                    "",
                    "then the END OF FREE TEXT!!!!!!!"]
        fu.writelines(testfile, newlines, True)
        fd.fileRead()

    # log("===================================")
    # log("print the dict file first time!")
    # fd.printDictFile()

    # log("read dict from the file")
    # fd.fileRead()

    log("===================================")
    log("Print the dictionary:")
    fd.printDict()

    log("===================================")
    log("Then change the dict in code ...")