def load_config_file(self): """ Load default values from a configuration file. @return: if-file-exist, is-config-file-in-old-format """ old_format = False fname = platform.local_settings_file(self.name) from ovirtcli.infrastructure.settings import OvirtCliSettings if fname is None: return False, old_format cp = ConfigParser() if not cp.read(fname): return False, old_format for section in cp.sections(): items = cp.items(section) if len(items) != len(OvirtCliSettings.config_items): old_format = True for key, value in items: conf_key = '%s:%s' % (section, key) if conf_key not in OvirtCliSettings.config_items: old_format = True else: self[conf_key] = value return True, old_format
def load_config_file(self): """Load default values from a configuration file.""" fname = platform.local_settings_file(self.name) if fname is None: return False cp = ConfigParser() if not cp.read(fname): return False for section in cp.sections(): for key, value in cp.items(section): self['%s:%s' % (section, key)] = value return True
def _write_config_file(self, settings, example=False): """Overwrite the configuration file with the current settings.""" fname = platform.local_settings_file(self.name) if fname is None: return ftmp = '%s.%d-tmp' % (fname, os.getpid()) fout = file(ftmp, 'w') sections = {} for key in settings: section, name = key.split(':') if section not in sections: sections[section] = {} sections[section][name] = settings[key] for section in sorted(sections): fout.write('[%s]\n' % section) for key in sorted(sections[section]): if example: fout.write('#') fout.write('%s = %s\n' % (key, sections[section][key])) fout.close() os.rename(ftmp, fname)
def _write_config_file(self, settings, example=False): """Overwrite the configuration file with the current settings.""" fname = platform.local_settings_file(self.name) if fname is None: return ftmp = '%s.%d-tmp' % (fname, os.getpid()) fout = file(ftmp, 'w') sections = {} from ovirtcli.infrastructure.settings import OvirtCliSettings for key in settings: if key in OvirtCliSettings.config_items: section, name = key.split(':') if section not in sections: sections[section] = {} sections[section][name] = settings[key] for section in sorted(sections): fout.write('[%s]\n' % section) for key in sections[section]: if example: fout.write('#') fout.write('%s = %s\n' % (key, sections[section][key])) fout.close() self.set_file_permissions(ftmp) os.rename(ftmp, fname)