Python Config Parser (Duplicate Key Support)

后端 未结 2 1437
栀梦
栀梦 2020-12-12 06:13

So I recently started writing a config parser for a Python project I\'m working on. I initially avoided configparser and configobj, because I wanted to support a config file

2条回答
  •  半阙折子戏
    2020-12-12 07:15

    Crazy idea: make your dictionary values as a list of 3-tuples with line number, col number and value itself and add special key for comment.

    CommentSymbol = ';'
    def readConfig(filename):
        f = open(filename, 'r')
        if not f:
            return
        def addValue(dict, key, lineIdx, colIdx, value):
            if key in dict:
                dict[key].append((lineIdx, colIdx, value))
            else:
                dict[key] = [(lineIdx, colIdx, value)]
        res = {}
        i = 0
        for line in f.readlines():
            idx = line.find(CommentSymbol)
            if idx != -1:
                comment = line[idx + 1:]
                addValue(res, CommentSymbol, i, idx, comment)
                line = line[:idx]
            pair = [x.strip() for x in line.split('=')][:2]
            if len(pair) == 2:
                addValue(res, pair[0], i, 0, pair[1])
            i += 1
        return res
    
    def writeConfig(dict, filename):
        f = open(filename, 'w')
        if not f:
            return
        index = sorted(dict.iteritems(), cmp = lambda x, y: cmp(x[1][:2], y[1][:2]))
        i = 0
        for k, V in index:
            for v in V:
                if v[0] > i:
                    f.write('\n' * (v[0] - i - 1))
                if k == CommentSymbol:
                    f.write('{0}{1}'.format(CommentSymbol, str(v[2])))
                else:
                    f.write('{0} = {1}'.format(str(k), str(v[2])))
                i = v[0]
        f.close() 
    

提交回复
热议问题