構成ファイルがあり、セクションとオプション名を入力してオプションを動的にロードしたいと考えています。
たとえば、これは構成ファイルの内容です。
[templates]
path = /full/path/to/template
および config.py ファイル
class Config(object):
def __init__(self):
self.config = ConfigParser.ConfigParser()
self.config.read('config.ini')
for section in self.config.sections():
self.__dict__[section] = dict()
for k, v in self.config.items(section):
self.__dict__[section][k] = v
def from_options(self, section, option):
if section in self.__dict__:
if option in self.__dict__[section]:
return self.__dict__[section][option]
if __name__ == '__main__':
c = Config()
print c.from_options('templates', 'path')
編集: 問題は、これが pythonic == 正しい方法であるかどうかです。