2017-01-21 4 views
1

Documentation은 당신이 이렇게 경우에만 섹션 설정이을 파이썬은-분리 의 기본 네임 스페이스 것으로 보인다 라고 패러다임이 있습니다구문 분석 .INI 섹션 파이썬 분리

[settings] 
DEBUG=True 
우리가 같은 사용자 정의 섹션이있는 경우

from decouple import config 
DEBUG = config('DEBUG', default=False, cast=bool) # no section argument 

그러나 :

당신은과 설정을 구문 분석 할 수

[sectionA] 
DEBUG=True 

[sectionB] 
foo="bar" 

?

나는 하나가 쉽게과 같이, 사용자 정의 섹션을 구문 분석 ConfigParser를 사용할 수 있다는 것을 알고 :

config_parser.get('sectionA', 'DEBUG') # the corresponding call in ConfigParser 

하지만 그것은 또한 .INI을 지원하기 때문에을-분리 파이썬은 을 통해 어떻게하는지 궁금 해서요 파일

답변

0

섹션은 code에 클래스 속성으로 하드 코드 된 것으로 보이므로이 문제에 대한 매개 변수가있는 정리 된 솔루션이 없다고 가정합니다.

class RepositoryIni(RepositoryEmpty): 
    """ 
    Retrieves option keys from .ini files. 
    """ 
    SECTION = 'settings' 

    def __init__(self, source): 
     self.parser = ConfigParser() 
     with open(source) as file_: 
      self.parser.readfp(file_) 

    def __contains__(self, key): 
     return (key in os.environ or 
       self.parser.has_option(self.SECTION, key)) 

    def __getitem__(self, key): 
     return self.parser.get(self.SECTION, key)