2018-02-20 15:22:04 +01:00
|
|
|
class ConfigParser:
|
2018-02-16 01:03:02 +01:00
|
|
|
def __init__(self):
|
2018-02-20 15:22:04 +01:00
|
|
|
self.config_dict = {}
|
2018-02-16 01:03:02 +01:00
|
|
|
|
|
|
|
def sections(self):
|
|
|
|
"""Return a list of section names, excluding [DEFAULT]"""
|
2018-02-20 15:22:04 +01:00
|
|
|
dict_to_return = [section for section in self.config_dict.keys() if not section in "DEFAULT"]
|
|
|
|
return dict_to_return
|
2018-02-16 01:03:02 +01:00
|
|
|
|
|
|
|
def add_section(self, section):
|
2018-02-23 00:37:03 +01:00
|
|
|
self.config_dict[section] = {}
|
2018-02-16 01:03:02 +01:00
|
|
|
|
|
|
|
def has_section(self, section):
|
2018-02-23 00:37:03 +01:00
|
|
|
if section in self.config_dict.keys():
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
2018-02-16 01:03:02 +01:00
|
|
|
|
|
|
|
def options(self, section):
|
2018-02-23 00:37:03 +01:00
|
|
|
if not section in self.config_dict:
|
|
|
|
raise
|
|
|
|
return self.config_dict[section].keys()
|
|
|
|
|
|
|
|
def read(self, filename=None, fp=None):
|
|
|
|
if not fp and not filename:
|
|
|
|
print("ERROR : no filename and no fp")
|
|
|
|
raise
|
|
|
|
elif not fp and filename:
|
2018-02-20 15:22:04 +01:00
|
|
|
fp = open(filename)
|
|
|
|
|
2018-02-23 00:37:03 +01:00
|
|
|
content = fp.read()
|
|
|
|
fp.close()
|
|
|
|
self.config_dict = {line.replace('[','').replace(']',''):{} for line in content.split('\n')\
|
|
|
|
if line.startswith('[') and line.endswith(']')
|
|
|
|
}
|
|
|
|
|
|
|
|
striped_content = [line.strip() for line in content.split('\n')]
|
|
|
|
for section in self.config_dict.keys():
|
|
|
|
start_index = striped_content.index('[%s]' % section)
|
|
|
|
end_flag = [line for line in striped_content[start_index + 1:] if line.startswith('[')]
|
|
|
|
if not end_flag:
|
|
|
|
end_index = None
|
|
|
|
else:
|
|
|
|
end_index = striped_content.index(end_flag[0])
|
|
|
|
block = striped_content[start_index + 1 : end_index]
|
|
|
|
options = [line.split('=')[0].strip() for line in block if '=' in line]
|
|
|
|
for option in options:
|
|
|
|
start_flag = [line for line in block if line.startswith(option) and '=' in line]
|
|
|
|
start_index = block.index(start_flag[0])
|
|
|
|
end_flag = [line for line in block[start_index + 1:] if '=' in line]
|
|
|
|
if not end_flag:
|
|
|
|
end_index = None
|
|
|
|
else:
|
|
|
|
end_index = block.index(end_flag[0])
|
|
|
|
values = [value.split('=')[-1].strip() for value in block[start_index:end_index] if value]
|
|
|
|
if not values:
|
|
|
|
values = None
|
|
|
|
elif len(values) == 1:
|
|
|
|
values = values[0]
|
|
|
|
self.config_dict[section][option] = values
|
2018-02-16 01:03:02 +01:00
|
|
|
|
|
|
|
def get(self, section, option):
|
2018-02-23 00:37:03 +01:00
|
|
|
if not self.has_section(section) \
|
|
|
|
or not self.has_option(section,option):
|
|
|
|
raise
|
|
|
|
return self.config_dict[section][option]
|
2018-02-16 01:03:02 +01:00
|
|
|
|
|
|
|
def has_option(self, section, option):
|
|
|
|
"""Check for the existence of a given option in a given section."""
|
2018-02-23 00:37:03 +01:00
|
|
|
if not section in self.config_dict:
|
|
|
|
raise
|
|
|
|
if option in self.config_dict[section].keys():
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def write(self, filename = None, fp = None):
|
2018-02-16 01:03:02 +01:00
|
|
|
"""Write an .ini-format representation of the configuration state."""
|
2018-02-23 00:37:03 +01:00
|
|
|
if not fp and not filename:
|
|
|
|
print("ERROR : no filename and no fp")
|
|
|
|
raise
|
|
|
|
elif not fp and filename:
|
|
|
|
fp = open(filename,'w')
|
|
|
|
|
|
|
|
for section in self.config_dict.keys():
|
|
|
|
fp.write('[%s]\n' % section)
|
|
|
|
for option in self.config_dict[section].keys():
|
|
|
|
fp.write('\n%s =' % option)
|
|
|
|
values = self.config_dict[section][option]
|
|
|
|
if type(values) == type([]):
|
|
|
|
fp.write('\n ')
|
|
|
|
values = '\n '.join(values)
|
|
|
|
else:
|
|
|
|
fp.write(' ')
|
|
|
|
fp.write(values)
|
|
|
|
fp.write('\n')
|
|
|
|
fp.write('\n')
|
|
|
|
|
2018-02-16 01:03:02 +01:00
|
|
|
|
|
|
|
def remove_option(self, section, option):
|
|
|
|
"""Remove an option."""
|
2018-02-23 00:37:03 +01:00
|
|
|
if not self.has_section(section) \
|
|
|
|
or not self.has_option(section,option):
|
|
|
|
raise
|
|
|
|
del self.config_dict[section][option]
|
2018-02-16 01:03:02 +01:00
|
|
|
|
|
|
|
def remove_section(self, section):
|
|
|
|
"""Remove a file section."""
|
2018-02-23 00:37:03 +01:00
|
|
|
if not self.has_section(section):
|
|
|
|
raise
|
|
|
|
del self.config_dict[section]
|