- 论坛徽章:
- 0
|
def options(self,section):
"""
get all option by section
"""
section = section.strip()
options = []
if self.__section_options.has_key(section):
for line in self.__section_options[section]:
option = line.split('=')[0].strip()
options.append(option)
return options
else:
raise self.NoSectionError,"No section:%s"%section
def add_section(self,section):
"""
"""
section = section.strip()
if self.__section_options.has_key(section):
raise self.DuplicateSectionError,"Section %s already exists" % section
else:
self.__section.append(section)
self.__section_options[section] = []
def remove_section(self,section):
"""
"""
section = section.strip()
if self.__section_options.has_key(section):
del self.__section_options[section]
temp_sections = []
for local_section in self.__section:
if local_section == section:
continue
else:
temp_sections.append(local_section)
self.__section = temp_sections
else:
raise self.NoSectionError,"No section:%s"%section
def has_section(self,section):
"""
"""
section = section.strip()
if self.__section_options.has_key(section):
return True
else:
return False
def get(self,section,option):
"""
"""
find_option = False
section = section.strip()
option = option.strip()
if self.__section_options.has_key(section):
lines = self.__section_options[section]
for line in lines:
local_option = line.split('=')[0].strip()
value = line.split('=')[-1].strip()
if option == local_option:
find_option = True
return value
else:
raise self.NoSectionError,"No section:%s"%section
if not find_option:
raise self.NoOptionError,"Failed to find %s in %s"%(option,section)
def has_option(self, section, option):
"""
"""
find_option = False
section = section.strip()
option = option.strip()
if self.__section_options.has_key(section):
lines = self.__section_options[section]
for line in lines:
local_option = line.split('=')[0].strip()
value = line.split('=')[-1].strip()
if option == local_option:
find_option = True
else:
raise self.NoSectionError,"No section:%s"%section
return find_option
def remove_option(self,section,option):
"""
"""
source_section = section.strip()
section = section.strip()
option = option.strip()
if self.__section_options.has_key(section):
if self.has_option(source_section,option):
lines = self.__section_options[section]
temp_options = []
for line in lines:
local_option = line.split('=')[0].strip()
value = line.split('=')[-1].strip()
if option == local_option:
continue
else:
temp_options.append(line)
self.__section_options[section] = temp_options
else:
raise self.NoOptionError,"Failed to find %s in %s"%(option,section)
else:
raise self.NoSectionError,"No section:%s"%section
|
|