def options(self, section): in_section = False options = [] for line in self.lines: if ConfParse.is_comment(line): continue if ConfParse.is_group(line): sect = line[1:-1] in_section = True if sect == section else False continue if in_section and '=' in line: key = line.split('=')[0].strip() options.append(key) return options
def get(self, section, option): in_section = False for line in self.lines: if ConfParse.is_comment(line): continue if ConfParse.is_group(line): sect = line[1:-1] in_section = True if sect == section else False continue if in_section and '=' in line: parts = line.split('=') key = parts[0].strip() if key == option: return '='.join(parts[1:]).strip() if len(parts) > 1 else '' return None
def get(self, section, option): in_section = False for line in self.lines: if ConfParse.is_comment(line): continue if ConfParse.is_group(line): sect = line[1:-1] in_section = True if sect == section else False continue if in_section and '=' in line: parts = line.split('=') key = parts[0].strip() if key == option: return '='.join( parts[1:]).strip() if len(parts) > 1 else '' return None
def set(self, section, option, new_value): self.add_section(section) self.add_option(section, option) in_section = False new_lines = self.lines[:] for i, line in enumerate(self.lines): if ConfParse.is_comment(line): continue if ConfParse.is_group(line): sect = line[1:-1] in_section = True if sect == section else False continue if in_section and '=' in line: parts = line.split('=') key = parts[0] value = '='.join(parts[1:]) if key == option: new_lines[i] = '%s=%s' % (key, new_value.strip()) self.lines = new_lines
def to_dict(self): out_dict = {} current_group = None for line in self.lines: if ConfParse.is_comment(line): continue if ConfParse.is_group(line): current_group = line[1:-1] out_dict[current_group] = {} continue if current_group is not None and ConfParse.is_key_value(line): fields = line.split('=') if len(fields) > 0: key = fields[0] if len(fields) > 1: value = '='.join(fields[1:]) else: value = '' out_dict[current_group][key] = value return out_dict