2011-06-29 6 views

答えて

12

PythonのConfigParserは複数のファイルを読み込むことができます。後で読むファイル は、最初のファイルの設定を上書きできます。私は 同じセク​​ションが、異なる値を含む「environment.ini」ファイルを別のサーバー上でこれらを無効に

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

:たとえば

、私のアプリケーションは、その内部のデフォルト 設定ファイルのデータベース設定を持っています:Pythonで

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

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 
+1

ありがとうございました。残念ながら、これは私にとってはうまく機能しません。なぜなら、1つのマスターファイルを複数のプログラミング言語で解析するビジネス要件があるからです。私は自分自身を実装する必要があるように見えます。 – Maascamp

+0

Maascamp:成功しましたか?私は同じ状況を抱えています... – xvga

+2

はい、私は自分の要件(Zend_Config_Iniスタイル)を満たし、可能であればPythonのネイティブタイプに変換するものを実装しました。こちら[https://bitbucket.org/maascamp/pyconfigini](https://bitbucket.org/maascamp/pyconfigini)を参照してください。それが役に立てば幸い。 – Maascamp

0

また、解決策が見つかりませんでした。それを解決するために、私は、親のセクションで、その後、子セクションで検索し、するの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 

制限:

  • 設定への読み取り専用アクセスが
  • をサポート継承のレベルは1つだけです
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')) 
+0

ありがとうRoman!何とか古いメソッド名がコードに潜入した:) – xvga

関連する問題