From 30791324ac9937960d4f9bda895038f73db3a01e Mon Sep 17 00:00:00 2001 From: Ferdi Franceschini Date: Sat, 5 Jul 2014 20:09:47 +1000 Subject: [PATCH] Always enable permanent options when changing instrument configuration. --- site_ansto/instrument/util/config_edit.py | 94 +++++++++-------------- 1 file changed, 38 insertions(+), 56 deletions(-) diff --git a/site_ansto/instrument/util/config_edit.py b/site_ansto/instrument/util/config_edit.py index a6b9cea6..a23df917 100755 --- a/site_ansto/instrument/util/config_edit.py +++ b/site_ansto/instrument/util/config_edit.py @@ -151,14 +151,6 @@ class InstConfigData: self.configuration_dict[s]['cascade_list'] = cascade_list def __get_options(self): - # opt_dict: dict of configuration options as defined below, - # {optname:{'enabled': T/F/Always, 'imptype':optype, 'selected_imp':dflt}} - opt_dict = defaultdict(dict) - - # imp2opt_dict: Maps each implementation to an option or None, - # {imp: opt/None} - imp2opt_dict = {} - for s in self.file_parser.sections(): if self.file_parser.has_option(s, 'implementation'): selected_imp = self.file_parser.get(s, 'implementation') @@ -201,20 +193,6 @@ class InstConfigData: print 'Add imp2opt_dict[{0}] = none'.format(s) self.imp2opt_dict[s] = 'none' - def set_imp(self, opt, new_imp): - old_imp =self.opt_dict[opt]['selected_imp'] - self.imp2opt_dict[old_imp] = 'none' - self.opt_dict[opt]['selected_imp'] = new_imp - self.imp2opt_dict[new_imp] = opt - - def get_optypelist (self): - return self.optypelist - - def iter_implementations(self, opt): - dict = self.opt_dict[opt] - for imp in self.imp_dict[dict['imptype']]: - yield imp - def read_config_file(self, config_filename): self.config_filename = config_filename self.file_parser = ConfigParser.SafeConfigParser() @@ -225,13 +203,12 @@ class InstConfigData: for opt,dict in self.opt_dict.iteritems(): self.optypelist.append((opt, dict['imptype'])) - return - def backup_files(self): for idx in range(8, 0, -1): if os.path.exists(self.config_filename + "." + str(idx)): os.rename(self.config_filename + "." + str(idx), self.config_filename + "." + str(idx + 1)) + if os.path.exists(self.config_filename): shutil.copy(self.config_filename, self.config_filename + ".1") @@ -262,17 +239,28 @@ class InstConfigData: cfile.write("\n") - def cf_statechange(self, checkbox, new_state, udat=None): - cfg_id = checkbox.get_label() + def set_imp(self, opt, new_imp): + old_imp =self.opt_dict[opt]['selected_imp'] + self.imp2opt_dict[old_imp] = 'none' + self.opt_dict[opt]['selected_imp'] = new_imp + self.imp2opt_dict[new_imp] = opt + + def get_optypelist (self): + return self.optypelist + + def iter_implementations(self, opt): + dict = self.opt_dict[opt] + for imp in self.imp_dict[dict['imptype']]: + yield imp + + def cf_statechange(self, cfg_id, new_state, udat=None): self.configuration_dict[cfg_id]['enabled'] = new_state - def opt_statechange(self, checkbox, new_state, udat=None): - opt = checkbox.get_label().split(':')[0] + def opt_statechange(self, opt, new_state, udat=None): dbg.msg(3, 'InstConfigData:opt_statechange({0},{1},{2})'.format(opt, new_state, udat)) self.opt_dict[opt]['enabled'] = new_state - def imp_statechange(self, button, new_state, opt): - selected_imp = button.get_label() + def imp_statechange(self, selected_imp, new_state, opt): dbg.msg(self.msg_index, 'InstConfigData:imp_statechange({0},{1},{2})'.format(selected_imp, new_state, opt)) self.msg_index = (self.msg_index - 3) % 2 + 4 if new_state == True: @@ -289,6 +277,7 @@ class InstConfigView(urwid.Pile): self.cf_man.opt_lb, self.cf_man.imp_lb, dbmsg] + super(InstConfigView, self).__init__(option_ListBoxes) return @@ -312,7 +301,6 @@ class InstConfigView(urwid.Pile): self.set_focus(self.cf_man.config_lb) else: self.set_focus(self.cf_man.opt_lb) - return # Contains OptionListWalker dict indexed by option @@ -326,10 +314,10 @@ class InstConfigManager: def __init__(self, cf_dat): self.cf_dat = cf_dat urwid.register_signal(InstConfigManager, ['focus_change']) - self.options = self.cf_dat.get_optypelist() + self.opt_optype_list = self.cf_dat.get_optypelist() - self.options.sort() - firstopt = self.options[0][0] + self.opt_optype_list.sort() + firstopt = self.opt_optype_list[0][0] self.imp_lw = self.__gen_imp_listwalker(firstopt) self.opt_lw = OptionListWalker(cf_dat.opt_dict, self.opt_statechange) for label, button in self.opt_lw.button_dict.iteritems(): @@ -369,29 +357,27 @@ class InstConfigManager: imp_items = imp_items[:1] + sorted(imp_items[1:]) - return RadioButtonListWalker(imp_items, on_state_change=self.imp_statechange, user_data=opt) + rb_lw = RadioButtonListWalker(imp_items, on_state_change=self.imp_statechange, user_data=opt) + return rb_lw def cf_statechange(self, button, new_state, udat=None): - self.cf_dat.cf_statechange(button, new_state, udat) - b = button.get_label() - cascade = self.cf_dat.configuration_dict[b]['cascade_list'] - + cfg_id = button.get_label() + self.cf_dat.cf_statechange(cfg_id, new_state, udat) + cascade = self.cf_dat.configuration_dict[cfg_id]['cascade_list'] if new_state == True: for opt in self.cf_dat.opt_dict.keys(): -# self.cf_dat.set_imp(opt, 'none') self.opt_lw.button_dict[opt].set_state(False) - if self.cf_dat.opt_dict[opt]['permanent'] == False: - self.opt_lw.button_dict[opt].set_state(False) + for opt,imp in cascade: self.cf_dat.set_imp(opt, imp) self.opt_lw.button_dict[opt].set_state(True) imp_lw = self.__gen_imp_listwalker(opt) -# imp_lw.button_dict[imp].set_state(True) -# currpos = self.opt_lb.get_focus()[1] -# self.opt_lb.set_focus(currpos) + for opt in self.cf_dat.opt_dict.keys(): + if self.cf_dat.opt_dict[opt]['permanent'] == True: + self.opt_lw.button_dict[opt].set_state(True) - dbg.msg(self.cf_msg_index, 'InstConfigManager:cf_statechange({0},{1},{2}), cascade = {3}'.format(b, new_state, udat, cascade)) + dbg.msg(self.cf_msg_index, 'InstConfigManager:cf_statechange({0},{1},{2}), cascade = {3}'.format(cfg_id, new_state, udat, cascade)) self.cf_msg_index = (self.cf_msg_index - 7) % 2 + 8 return @@ -401,8 +387,8 @@ class InstConfigManager: if new_state == True: if self.__imp_unavailable(imp): self.cf_dat.opt_dict[opt]['selected_imp'] = 'none' - imp_button = self.imp_lw.button_dict['none'] - imp_button.set_state(True) + imp_none_button = self.imp_lw.button_dict['none'] + imp_none_button.set_state(True) opt_button = self.opt_lw.button_dict[opt] opt_button.set_label('{0}:none'.format(opt)) else: @@ -410,8 +396,7 @@ class InstConfigManager: opt_button.set_label('{0}:{1}'.format(opt,imp)) self.cf_dat.set_imp(opt, imp) - self.cf_dat.opt_statechange(button, new_state, udat) - return + self.cf_dat.opt_statechange(opt, new_state, udat) def imp_statechange(self, button, new_state, opt): if new_state == True: @@ -420,12 +405,12 @@ class InstConfigManager: opt_button = self.opt_lw.button_dict[opt] opt_button.set_label('{0}:{1}'.format(opt, imp)) - self.cf_dat.imp_statechange(button, new_state, opt) + self.cf_dat.imp_statechange(imp, new_state, opt) return def update_imp_lb(self, pos): - optname = self.options[pos][0] - optype = self.options[pos][1] + optname = self.opt_optype_list[pos][0] + optype = self.opt_optype_list[pos][1] mstr = 'InstConfigManager:update_imp_lb({0}) -> select {1}'.format(pos, optype) dbg.msg(1, mstr) self.imp_lw = self.__gen_imp_listwalker(optname) @@ -448,14 +433,11 @@ class DEBUG: mlw = urwid.SimpleListWalker(self.msglist) self.mlb = urwid.ListBox(mlw) - return def msg(self, index, msg): if self.enabled: mid = self.msg_ids[index] self.msgTextDict[mid].set_text(msg) - return - dbg = DEBUG(enabled=True) def main(config_ini):