diff --git a/site_ansto/instrument/util/config_edit.py b/site_ansto/instrument/util/config_edit.py index 0f830c62..856af673 100755 --- a/site_ansto/instrument/util/config_edit.py +++ b/site_ansto/instrument/util/config_edit.py @@ -323,11 +323,11 @@ class InstConfigData(object): print emsg.format(i=selected_imp, o=opt) for imp, opt in self.imp2opt_dict.iteritems(): - if opt == 'none': - continue - elif imp == 'none': + if imp == 'none': print 'ERROR: Found "none" as a keyword in imp2opt_dict' continue + elif opt == 'none': + continue else: selected_imp = self.opt_dict[opt]['selected_imp'] @@ -361,6 +361,12 @@ class InstConfigData(object): if os.path.exists(self.config_filename): shutil.copy2(self.config_filename, self.config_filename + ".1") + def write_section(self, fhandle, sect): + """Write a configuration section with sorted options""" + fhandle.write("[%s]\n" % sect) + for opt in sorted(self.file_parser.options(sect)): + fhandle.write('{0} = {1}\n'.format(opt, self.file_parser.get(sect, opt))) + def write_config_file(self): """ Write out InstConfigData values to the configuration file.""" for opt, opt_desc in self.opt_dict.iteritems(): @@ -382,22 +388,27 @@ class InstConfigData(object): if imp != 'none' and opt != 'none' and 'id' in self.opt_dict[opt]: self.file_parser.set(imp, 'id', self.opt_dict[opt]['id']) - with open(self.config_filename, 'w') as cfile: - for section in sorted(self.file_parser.sections()): - cfile.write("[%s]\n" % section) - for option in sorted(self.file_parser.options(section)): - cfile.write( - '{0} = {1}\n'.format(option, - self.file_parser.get(section, - option))) + scratch_file = self.config_filename + '.scratch' + with open(scratch_file, 'w') as cfile: + for config in sorted(self.config_dict.keys()): + self.write_section(cfile, config) + + for opt in sorted(self.opt_dict.keys()): + self.write_section(cfile, opt) + + for imp in sorted(self.imp2opt_dict.keys()): + self.write_section(cfile, imp) cfile.write("\n") + os.rename(scratch_file, self.config_filename) + def set_imp(self, opt, new_imp): """Keep option dictionaray and implementation -> option map in sync.""" if 'selected_imp' in self.opt_dict[opt]: old_imp = self.opt_dict[opt]['selected_imp'] - self.imp2opt_dict[old_imp] = 'none' + if old_imp != 'none': + self.imp2opt_dict[old_imp] = 'none' self.opt_dict[opt]['selected_imp'] = new_imp if new_imp != 'none':