2017-01-21 33 views
1

Documentation有一個範例,唯一的部分稱爲設置 這似乎是默認命名空間中的python-脫鉤因此,如果您有:解析的.ini與部分中的python-脫鉤

[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 

但我不知道它是如何通過做的python-脫鉤因爲它也支持的.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)