支持节inheritance的Python配置解析器?

15 投票
3 回答
11393 浏览
提问于 2025-04-16 20:33

我在找一个Python的配置解析器,想要那种ini风格的,能够支持类似于Zend_Config_Ini在PHP中那样的部分继承功能。

有没有这样的模块?还是说我需要自己写一个?

3 个回答

0

我也没有找到现成的解决办法。为了处理这个问题,我修改了ConfigParser的获取功能,让它先在子部分查找,然后再到父部分查找:

config = SafeConfigParser()
config.read(filenames)
required_environment = "mysection"

# determine fallback requirement in case parameter is not found in required environment
fallback_environment = "default"
# loop through all sections of config files
for environment in config.sections():
    # check whether we find an inheritance based on the required section
    if re.search(required_environment + " *: *\w+", environment):
        # found inheritance, take parent as fallback section
        fallback_environment = re.sub(required_environment + r" : (\w+)", r"\1", environment)
        # take this name as requested section
        required_environment = environment

# override get method
_config_parser_get = config.get
def myConfigParserGet(id):
    # check different sections for desired value
    if config.has_option(required_environment, id):
        return _config_parser_get(required_environment, id)
    else:
        return _config_parser_get(fallback_environment, id)

config.get = myConfigParserGet

限制条件:

  • 只支持只读访问配置
  • 只支持一层继承
1

这是我用的东西。extended_get 方法就是你需要的,它支持层级结构的部分。

import re
import io
import ConfigParser

class ZendConfigParser(ConfigParser.ConfigParser):
    def extended_get(self, section, key):
        if self.has_option(section, key):
            return self.get(section, key)
        else:
            orig_section, parent_section = self._get_orig_section(section)
            if orig_section != None:
                if self.has_option(orig_section,key):
                    return self.get(orig_section,key)
                else:
                    return self.extended_get(parent_section,key)
            else:
                return None



    def _get_orig_section(self, zend_section):
        orig_section = None
        parent_section = None
        for section in self.sections():
            if re.search(r'^[ \t]*' + zend_section + '\\b', section) != None:
                orig_section = section
                #look for a parent section
                match = re.match(r'\w+[ \t]*:[ \t]*(\w+)$', section)
                if match != None:
                    parent_section = match.groups()[0]
                break

        return (orig_section, parent_section)

config = ZendConfigParser()
config.read(file)
print(config.extended_get('production', 'database.params.host'))
20

Python的ConfigParser可以加载多个文件。后面读取的文件可以覆盖第一个文件中的设置。

举个例子,我的应用程序在内部的默认配置文件中有数据库设置:

[database]
server = 127.0.0.1
port = 1234
...

在另一个服务器上,我用一个名为"environment.ini"的文件来覆盖这些设置,这个文件包含相同的部分但值不同:

[database]
server = 192.168.0.12
port = 2345
...

在Python中:

import os
from ConfigParser import ConfigParser
dbconf = ConfigParser()
dbconf.readfp(open('default.ini'))
if os.path.exists('environment.ini'):
    dbconf.readfp(open('environment.ini'))
dbconf.get('database', 'server') # Returns 192.168.0.12

撰写回答