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 main(args): if len(args) < 2: sys.stderr.write("error: no input file(s) specified\n") return 1 for filename in args[1:]: conf = ConfParse(filename) if conf.has_option('named_styles', 'default'): def_line = conf.get('named_styles', 'default') else: def_line = '#000;#fff;false;false' def_fields = expand_style(def_line) defaultify_named_styles(conf, def_fields) #print(str(conf)) conf.save() return 0
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 add_option(self, section, option): self.add_section(section) if not self.has_option(section, option): for i, line in enumerate(self.lines): if ConfParse.is_group(line): group = line[1:-1] if group == section: self.lines.insert(i+1, '%s=' % option) break
def add_option(self, section, option): self.add_section(section) if not self.has_option(section, option): for i, line in enumerate(self.lines): if ConfParse.is_group(line): group = line[1:-1] if group == section: self.lines.insert(i + 1, '%s=' % option) break
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