def __init__(self, parent=None): self._parent = parent self._defaults = MultiOrderedDict() self._sections = MultiOrderedDict() self._includes = OrderedDict()
class MultiOrderedConfigParser: def __init__(self, parent=None): self._parent = parent self._defaults = MultiOrderedDict() self._sections = MultiOrderedDict() self._includes = OrderedDict() def find_value(self, sections, key): """Given a list of sections, try to find value(s) for the given key.""" # always start looking in the last one added sections.sort(reverse=True) for s in sections: try: # try to find in section and section's templates return s.get(key, from_defaults=False) except KeyError: pass # wasn't found in sections or a section's templates so check in # defaults for s in sections: try: # try to find in section's defaultsects return s.get(key, from_self=False, from_templates=False) except KeyError: pass raise KeyError(key) def defaults(self): return self._defaults def default(self, key): """Retrieves a list of dictionaries for a default section.""" return self.get_defaults(key) def add_default(self, key, template_keys=None): """ Adds a default section to defaults, returning the default Section object. """ if template_keys is None: template_keys = [] return self.add_section(key, template_keys, self._defaults) def sections(self): return self._sections def section(self, key): """Retrieves a list of dictionaries for a section.""" return self.get_sections(key) def get_sections(self, key, attr='_sections', searched=None): """ Retrieve a list of sections that have values for the given key. The attr parameter can be used to control what part of the parser to retrieve values from. """ if searched is None: searched = [] if self in searched: return [] sections = getattr(self, attr) res = sections[key] if key in sections else [] searched.append(self) if self._includes: res.extend( list( itertools.chain(*[ incl.get_sections(key, attr, searched) for incl in self._includes.itervalues() ]))) if self._parent: res += self._parent.get_sections(key, attr, searched) return res def get_defaults(self, key): """ Retrieve a list of defaults that have values for the given key. """ return self.get_sections(key, '_defaults') def add_section(self, key, template_keys=None, mdicts=None): """ Create a new section in the configuration. The name of the new section is the 'key' parameter. """ if template_keys is None: template_keys = [] if mdicts is None: mdicts = self._sections res = Section() for t in template_keys: res.add_templates(self.get_defaults(t)) res.add_defaults(self.get_defaults(DEFAULTSECT)) mdicts.insert(0, key, res) return res def includes(self): return self._includes def add_include(self, filename, parser=None): """ Add a new #include file to the configuration. """ if filename in self._includes: return self._includes[filename] self._includes[filename] = res = \ MultiOrderedConfigParser(self) if parser is None else parser return res def get(self, section, key): """Retrieves the list of values from a section for a key.""" try: # search for the value in the list of sections return self.find_value(self.section(section), key) except KeyError: pass try: # section may be a default section so, search # for the value in the list of defaults return self.find_value(self.default(section), key) except KeyError: raise LookupError("key %r not found for section %r" % (key, section)) def multi_get(self, section, key_list): """ Retrieves the list of values from a section for a list of keys. This method is intended to be used for equivalent keys. Thus, as soon as any match is found for any key in the key_list, the match is returned. This does not concatenate the lookups of all of the keys together. """ for i in key_list: try: return self.get(section, i) except LookupError: pass # Making it here means all lookups failed. raise LookupError("keys %r not found for section %r" % (key_list, section)) def set(self, section, key, val): """Sets an option in the given section.""" # TODO - set in multiple sections? (for now set in first) # TODO - set in both sections and defaults? if section in self._sections: self.section(section)[0][key] = val else: self.defaults(section)[0][key] = val def read(self, filename, sect=None): """Parse configuration information from a file""" try: with open(filename, 'rt') as config_file: self._read(config_file, sect) except IOError: print "Could not open file ", filename, " for reading" def _read(self, config_file, sect): """Parse configuration information from the config_file""" is_comment = False # used for multi-lined comments for line in config_file: line, is_comment = remove_comment(line, is_comment) if not line: # line was empty or was a comment continue include_name = try_include(line) if include_name: parser = self.add_include(include_name) parser.read(include_name, sect) continue section, is_template, templates = try_section(line) if section: if section == DEFAULTSECT or is_template: sect = self.add_default(section, templates) else: sect = self.add_section(section, templates) continue key, val = try_option(line) if sect is None: raise Exception("Section not defined before assignment") sect[key] = val def write(self, config_file): """Write configuration information out to a file""" try: for key, val in self._includes.iteritems(): val.write(key) config_file.write('#include "%s"\n' % key) config_file.write('\n') write_dicts(config_file, self._defaults) write_dicts(config_file, self._sections) except: try: with open(config_file, 'wt') as fp: self.write(fp) except IOError: print "Could not open file ", config_file, " for writing"
class MultiOrderedConfigParser: def __init__(self, parent=None): self._parent = parent self._defaults = MultiOrderedDict() self._sections = MultiOrderedDict() self._includes = OrderedDict() def defaults(self): return self._defaults def default(self, key): """Retrieves a list of dictionaries for a default section.""" return get_defaults(self, key) def add_default(self, key, template_keys=None): """Adds a default section to defaults, returning the default Section object. """ if template_keys is None: template_keys = [] return self.add_section(key, template_keys, self._defaults) def sections(self): return self._sections def section(self, key): """Retrieves a list of dictionaries for a section.""" return get_sections(self, key) def add_section(self, key, template_keys=None, mdicts=None): if template_keys is None: template_keys = [] if mdicts is None: mdicts = self._sections res = Section() for t in template_keys: res.add_templates(get_defaults(self, t)) res.add_defaults(get_defaults(self, DEFAULTSECT)) mdicts.insert(0, key, res) return res def includes(self): return self._includes def add_include(self, filename, parser=None): if filename in self._includes: return self._includes[filename] self._includes[filename] = res = \ MultiOrderedConfigParser(self) if parser is None else parser return res; def get(self, section, key): """Retrieves the list of values from a section for a key.""" try: # search for the value in the list of sections return find_value(self.section(section), key) except KeyError: pass try: # section may be a default section so, search # for the value in the list of defaults return find_value(self.default(section), key) except KeyError: raise LookupError("key %r not found for section %r" % (key, section)) def set(self, section, key, val): """Sets an option in the given section.""" # TODO - set in multiple sections? (for now set in first) # TODO - set in both sections and defaults? if section in self._sections: self.section(section)[0][key] = val else: self.defaults(section)[0][key] = val def read(self, filename): try: with open(filename, 'rt') as file: self._read(file, filename) except IOError: print "Could not open file ", filename, " for reading" def _read(self, file, filename): is_comment = False # used for multi-lined comments for line in file: line, is_comment = remove_comment(line, is_comment) if not line: # line was empty or was a comment continue include_name = try_include(line) if include_name: parser = self.add_include(include_name) parser.read(include_name) continue section, is_template, templates = try_section(line) if section: if section == DEFAULTSECT or is_template: sect = self.add_default(section, templates) else: sect = self.add_section(section, templates) continue key, val = try_option(line) sect[key] = val def write(self, f): try: for key, val in self._includes.iteritems(): val.write(key) f.write('#include "%s"\n' % key) f.write('\n') write_dicts(f, self._defaults) write_dicts(f, self._sections) except: try: with open(f, 'wt') as fp: self.write(fp) except IOError: print "Could not open file ", f, " for writing"
class MultiOrderedConfigParser: def __init__(self, parent=None): self._parent = parent self._defaults = MultiOrderedDict() self._sections = MultiOrderedDict() self._includes = OrderedDict() def find_value(self, sections, key): """Given a list of sections, try to find value(s) for the given key.""" # always start looking in the last one added sections.sort(reverse=True) for s in sections: try: # try to find in section and section's templates return s.get(key, from_defaults=False) except KeyError: pass # wasn't found in sections or a section's templates so check in # defaults for s in sections: try: # try to find in section's defaultsects return s.get(key, from_self=False, from_templates=False) except KeyError: pass raise KeyError(key) def defaults(self): return self._defaults def default(self, key): """Retrieves a list of dictionaries for a default section.""" return self.get_defaults(key) def add_default(self, key, template_keys=None): """ Adds a default section to defaults, returning the default Section object. """ if template_keys is None: template_keys = [] return self.add_section(key, template_keys, self._defaults) def sections(self): return self._sections def section(self, key): """Retrieves a list of dictionaries for a section.""" return self.get_sections(key) def get_sections(self, key, attr='_sections', searched=None): """ Retrieve a list of sections that have values for the given key. The attr parameter can be used to control what part of the parser to retrieve values from. """ if searched is None: searched = [] if self in searched: return [] sections = getattr(self, attr) res = sections[key] if key in sections else [] searched.append(self) if self._includes: res.extend(list(itertools.chain(*[ incl.get_sections(key, attr, searched) for incl in self._includes.itervalues()]))) if self._parent: res += self._parent.get_sections(key, attr, searched) return res def get_defaults(self, key): """ Retrieve a list of defaults that have values for the given key. """ return self.get_sections(key, '_defaults') def add_section(self, key, template_keys=None, mdicts=None): """ Create a new section in the configuration. The name of the new section is the 'key' parameter. """ if template_keys is None: template_keys = [] if mdicts is None: mdicts = self._sections res = Section() for t in template_keys: res.add_templates(self.get_defaults(t)) res.add_defaults(self.get_defaults(DEFAULTSECT)) mdicts.insert(0, key, res) return res def includes(self): return self._includes def add_include(self, filename, parser=None): """ Add a new #include file to the configuration. """ if filename in self._includes: return self._includes[filename] self._includes[filename] = res = \ MultiOrderedConfigParser(self) if parser is None else parser return res def get(self, section, key): """Retrieves the list of values from a section for a key.""" try: # search for the value in the list of sections return self.find_value(self.section(section), key) except KeyError: pass try: # section may be a default section so, search # for the value in the list of defaults return self.find_value(self.default(section), key) except KeyError: raise LookupError("key %r not found for section %r" % (key, section)) def multi_get(self, section, key_list): """ Retrieves the list of values from a section for a list of keys. This method is intended to be used for equivalent keys. Thus, as soon as any match is found for any key in the key_list, the match is returned. This does not concatenate the lookups of all of the keys together. """ for i in key_list: try: return self.get(section, i) except LookupError: pass # Making it here means all lookups failed. raise LookupError("keys %r not found for section %r" % (key_list, section)) def set(self, section, key, val): """Sets an option in the given section.""" # TODO - set in multiple sections? (for now set in first) # TODO - set in both sections and defaults? if section in self._sections: self.section(section)[0][key] = val else: self.defaults(section)[0][key] = val def read(self, filename, sect=None): """Parse configuration information from a file""" try: with open(filename, 'rt') as config_file: self._read(config_file, sect) except IOError: print("Could not open file " + filename + " for reading") def _read(self, config_file, sect): """Parse configuration information from the config_file""" is_comment = False # used for multi-lined comments for line in config_file: line, is_comment = remove_comment(line, is_comment) if not line: # line was empty or was a comment continue include_name = try_include(line) if include_name: parser = self.add_include(include_name) parser.read(include_name, sect) continue section, is_template, templates = try_section(line) if section: if section == DEFAULTSECT or is_template: sect = self.add_default(section, templates) else: sect = self.add_section(section, templates) continue key, val = try_option(line) if sect is None: raise Exception("Section not defined before assignment") sect[key] = val def write(self, config_file): """Write configuration information out to a file""" try: for key, val in self._includes.iteritems(): val.write(key) config_file.write('#include "%s"\n' % key) config_file.write('\n') write_dicts(config_file, self._defaults) write_dicts(config_file, self._sections) except: try: with open(config_file, 'wt') as fp: self.write(fp) except IOError: print("Could not open file " + config_file + " for writing")