Python Config Parser (Duplicate Key Support)

后端 未结 2 1417
栀梦
栀梦 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

    Well I would certainly try to leverage what is in the standard library if I could.

    The signature for the config parser classes look like this:

    class ConfigParser.SafeConfigParser([defaults[, dict_type[, allow_no_value]]])

    Notice the dict_type argument. When provided, this will be used to construct the dictionary objects for the list of sections, for the options within a section, and for the default values. It defaults to collections.OrderedDict. Perhaps you could pass something in there to get your desired multiple-key behavior, and then reap all the advantages of ConfigParser. You might have to write your own class to do this, or you could possibly find one written for you on PyPi or in the ActiveState recipes. Try looking for a bag or multiset class.

    I'd either go that route or just suck it up and make a list:

    foo = value1, value2, value3
    
    0 讨论(0)
  • 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() 
    
    0 讨论(0)
提交回复
热议问题