ConfigParser 中不区分大小写的部分

Case-insensitive sections in ConfigParser

我正在查看Python 3.6 documentation

By default, section names are case sensitive but keys are not [1].

对于脚注它说

[1] (1, 2, 3, 4, 5, 6, 7, 8, 9, 10) Config parsers allow for heavy customization. If you are interested in changing the behaviour outlined by the footnote reference, consult the Customizing Parser Behaviour section.

所以我查看了“14.2.7. 自定义解析器行为”,但找不到有关如何使部分不区分大小写的说明。

我想要这样的版块:

[SETTINGS]
...

可以像这样访问 config['section'],但目前我收到一个错误。这是我要应用的对配置解析器的唯一更改。

您可以在 Python 3.x 中相当轻松地完成此操作,方法是将某些内容作为 ConfigParser documentation 中描述的可选 dict_type= 关键字参数传递——在本例中我们我希望类型是 case-insensitive 有序 dictionary.

不幸的是,标准库中没有,也没有我所知道的锥形实现...所以我拼凑了一个作为示例。它尚未经过严格测试,但效果很好,足以说明总体思路。

注意: 为了进行测试,我使用了以下 simple.ini 文件(我从 pymotw 中提取):

# This is a simple example with comments.
[bug_tracker]
url = http://localhost:8080/bugs/
username = dhellmann
; You should not store passwords in plain text
; configuration files.
password = SECRET

下面是一个演示,展示了使用一个来完成所需的操作:

import collections
from configparser import ConfigParser

class CaseInsensitiveDict(collections.MutableMapping):
    """ Ordered case insensitive mutable mapping class. """
    def __init__(self, *args, **kwargs):
        self._d = collections.OrderedDict(*args, **kwargs)
        self._convert_keys()
    def _convert_keys(self):
        for k in list(self._d.keys()):
            v = self._d.pop(k)
            self._d.__setitem__(k, v)
    def __len__(self):
        return len(self._d)
    def __iter__(self):
        return iter(self._d)
    def __setitem__(self, k, v):
        self._d[k.lower()] = v
    def __getitem__(self, k):
        return self._d[k.lower()]
    def __delitem__(self, k):
        del self._d[k.lower()]


parser = ConfigParser(dict_type=CaseInsensitiveDict)
parser.read('simple.ini')

print(parser.get('bug_tracker', 'url'))  # -> http://localhost:8080/bugs/
print(parser.get('Bug_tracker', 'url'))  # -> http://localhost:8080/bugs/