def _write(self, parser): if not self.filename: return wait_for_file_mtime_change(self.filename) with AtomicFile(self.filename, 'w') as fd: fd.writelines(['# -*- coding: utf-8 -*-\n', '\n']) parser.write(fd)
def save(self): """Write the configuration options to the primary file.""" if not self.filename: return # Only save options that differ from the defaults sections = [] for section in self.sections(): section_str = _to_utf8(section) options = [] for option in self[section]: default_str = None for parent in self.parents: if parent.has_option(section, option, defaults=False): default_str = _to_utf8(parent.get(section, option)) break option_str = _to_utf8(option) current_str = False if self.parser.has_option(section_str, option_str): current_str = self.parser.get(section_str, option_str) if current_str is not False and current_str != default_str: options.append((option_str, current_str)) if options: sections.append((section_str, sorted(options))) # At this point, all the strings in `sections` are UTF-8 encoded `str` try: wait_for_file_mtime_change(self.filename) with AtomicFile(self.filename, 'w') as fileobj: fileobj.write('# -*- coding: utf-8 -*-\n\n') for section_str, options in sections: fileobj.write('[%s]\n' % section_str) section = to_unicode(section_str) for key_str, val_str in options: if to_unicode(key_str) in self[section].overridden: fileobj.write('# %s = <inherited>\n' % key_str) else: val_str = val_str.replace(CRLF, '\n') \ .replace('\n', '\n ') fileobj.write('%s = %s\n' % (key_str, val_str)) fileobj.write('\n') self._old_sections = deepcopy(self.parser._sections) except Exception: # Revert all changes to avoid inconsistencies self.parser._sections = deepcopy(self._old_sections) raise
def save(self): """Write the configuration options to the primary file.""" if not self.filename: return # Only save options that differ from the defaults sections = [] for section in self.sections(): section_str = _to_utf8(section) options = [] for option in self[section]: default_str = None if self.parent: default_str = _to_utf8(self.parent.get(section, option)) option_str = _to_utf8(option) current_str = False if self.parser.has_option(section_str, option_str): current_str = self.parser.get(section_str, option_str) if current_str is not False and current_str != default_str: options.append((option_str, current_str)) if options: sections.append((section_str, sorted(options))) # At this point, all the strings in `sections` are UTF-8 encoded `str` try: fileobj = AtomicFile(self.filename, 'w') try: fileobj.write('# -*- coding: utf-8 -*-\n\n') for section, options in sections: fileobj.write('[%s]\n' % section) for key_str, val_str in options: if to_unicode(key_str) in self[section].overridden: fileobj.write('# %s = <inherited>\n' % key_str) else: val_str = val_str.replace(CRLF, '\n') \ .replace('\n', '\n ') fileobj.write('%s = %s\n' % (key_str, val_str)) fileobj.write('\n') finally: fileobj.close() self._old_sections = deepcopy(self.parser._sections) except Exception: # Revert all changes to avoid inconsistencies self.parser._sections = deepcopy(self._old_sections) raise
def _write(self, content): if not self.filename: return wait_for_file_mtime_change(self.filename) with AtomicFile(self.filename, 'w') as fileobj: fileobj.writelines(content)