Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
支持节继承的Python配置解析器?_Python_Config_Ini - Fatal编程技术网

支持节继承的Python配置解析器?

支持节继承的Python配置解析器?,python,config,ini,Python,Config,Ini,我正在寻找Python中的ini样式配置解析器,它支持与PHP中类似的节继承 这样的模块存在吗?或者我需要自己运行吗?Python的ConfigParser可以加载多个文件。以后在can上读取的文件 覆盖第一个文件中的设置 例如,我的应用程序在其内部默认设置中有数据库设置 配置文件: [database] server = 127.0.0.1 port = 1234 ... 我在另一台服务器上用一个包含 相同的部分但不同的值: [database] server = 192.168.0.12

我正在寻找Python中的ini样式配置解析器,它支持与PHP中类似的节继承


这样的模块存在吗?或者我需要自己运行吗?

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

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

[database]
server = 127.0.0.1
port = 1234
...
我在另一台服务器上用一个包含 相同的部分但不同的值:

[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

我也没有找到任何现成的解决办法。为了解决这个问题,我调整了ConfigParser的get函数,在子部分进行搜索,然后在父部分进行搜索:

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
限制:

  • 仅支持对配置的只读访问
  • 只有一个继承级别

    • 这是我用过的
      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'))
      

      谢谢你的信息。不幸的是,这对我来说不起作用,因为业务需要一个主文件,可以用多种编程语言进行解析。看起来我需要自己实现。是的,我实现了一个符合我要求的(Zend_Config_Ini样式),并在可能的情况下转换为python本机类型。看这里。希望对你有帮助。@maascap干得好!我正在寻找PythonINI解析,比如Zend_Config_ini,您应该回答自己的问题并发布解决方案:)谢谢!不知怎的,旧方法名潜入了代码:)