как-то так:
from UserDict import UserDict
class configs(UserDict):
def __init__(self, iniFile='config.ini'):
self.data = self._load_(iniFile)
def _load_(self, iniFile='config.ini', raw=False, vars=None):
"""Convert an INI file to a dictionary"""
import ConfigParser
config = ConfigParser.ConfigParser()
config.read(iniFile)
result = {}
for section in config.sections():
if section not in result:
result[section] = {}
for option in config.options(section):
value = config.get(section, option, raw, vars)
result[section][option] = value
return result
def dump(self, iniFile='config.ini'):
"""Convert an dictionary to a INI file"""
import ConfigParser
config = ConfigParser.ConfigParser()
config_dict = self.data
for section, values in config_dict.items():
config.add_section(section)
for var_name, var_value in values.items():
config.set(section, var_name, var_value)
config.write(open(iniFile, 'w'))
[first]
aa = 1
ba = 2
[second]
ab = 3
bb = 4
>>>conf=ConPars2Dict2.configs('sample.ini')
>>>print conf
<<<{'second': {'ab': '3', 'bb': '4'}, 'first': {'aa': '1', 'ba': '2'}}