From a802f98a2430794c64afc8c101f40253b84ac950 Mon Sep 17 00:00:00 2001 From: Ferdi Franceschini Date: Fri, 4 Jul 2014 13:46:47 +1000 Subject: [PATCH] Remove unavailable implementations from the impelementation list when an option is selected. --- site_ansto/instrument/util/config_edit.py | 83 +++++++++++++---------- 1 file changed, 47 insertions(+), 36 deletions(-) diff --git a/site_ansto/instrument/util/config_edit.py b/site_ansto/instrument/util/config_edit.py index 6ea41714..dae06dd9 100755 --- a/site_ansto/instrument/util/config_edit.py +++ b/site_ansto/instrument/util/config_edit.py @@ -3,18 +3,18 @@ # View Screen has 3 parts # (Instrument Configuration), (Configuration Options), (Option Implementation) -# Uses MVC implemented as InstConfigData, InstConfigView, ConfigEdit +# Uses MVC implemented as InstConfigData, InstConfigView, InstConfigManager # # InstConfigData <>--- ConfigParser.SafeConfig # |--set_cfparse() -# ConfigEdit <>--- InstConfigData, PresentationData +# InstConfigManager <>--- InstConfigData, PresentationData # |--set_cfdata(), set_presdata() # |--set_xyz_data() call self.cfgdata.set_xyz() methods # # urwid.Frame # ^ -# InstConfigView <>--- ConfigEdit, PresentationData +# InstConfigView <>--- InstConfigManager, PresentationData # |--set_cfedit(), set_presdata() # # PresentationData @@ -30,8 +30,6 @@ import copy from collections import defaultdict -## TODO Configuration Editor -## Configuration Viewer Palette = [ ('body', 'dark cyan', '', 'standout'), ('focus', 'dark red', '', 'standout'), @@ -69,7 +67,6 @@ class CheckBoxListWalker(urwid.SimpleListWalker): return -# Selects listwalker to display for ImpListBox on focus class OptionListWalker(CheckBoxListWalker): def __init__(self, opt_dict, statechange_cb): urwid.register_signal(OptionListWalker, ['focus_change']) @@ -212,11 +209,12 @@ class InstConfigData: self.opt_dict[s]['permanent'] = permanent self.opt_dict[s]['imptype'] = imptype if selected_imp in self.imp2opt_dict: - self.opt_dict[s]['selected_imp'] = "none" + self.opt_dict[s]['selected_imp'] = 'none' else: self.opt_dict[s]['selected_imp'] = selected_imp - print 'Add imp2opt_dict[{0}] = {1}'.format(selected_imp,s) - self.imp2opt_dict[selected_imp] = s + if selected_imp != 'none': + print 'Add imp2opt_dict[{0}] = {1}'.format(selected_imp,s) + self.imp2opt_dict[selected_imp] = s def __get_implementations(self): for s in self.file_parser.sections(): @@ -228,7 +226,7 @@ class InstConfigData: self.imp_dict[key].append(s) if s not in self.imp2opt_dict: print 'Add imp2opt_dict[{0}] = none'.format(s) - self.imp2opt_dict[s] = "none" + self.imp2opt_dict[s] = 'none' def read_config_file(self, config_filename): @@ -274,7 +272,6 @@ class InstConfigData: cfile.write("%s = %s\n" % (option, self.file_parser.get(section, option))) cfile.write("\n") - #self.file_parser.write(cfile) def cf_statechange(self, checkbox, new_state, udat=None): cfg_id = checkbox.get_label() @@ -302,30 +299,18 @@ class InstConfigManager: cf_msg_index = 8 options = [] imp_lw_dict = {} - def __init__(self, cfdat): - self.cfdat = cfdat + def __init__(self, cf_dat): + self.cf_dat = cf_dat urwid.register_signal(InstConfigManager, ['focus_change']) - for opt,dict in cfdat.opt_dict.iteritems(): + for opt,dict in cf_dat.opt_dict.iteritems(): self.options.append((opt, dict['imptype'])) -# imp_items = [] -# for imp in cfdat.imp_dict[dict['imptype']]: -# if imp == dict['selected_imp']: -# imp_items.append((imp, True)) -# else: -# imp_items.append((imp, False)) - -# imp_items.sort() -# self.imp_lw_dict[opt] = RadioButtonListWalker(imp_items, on_state_change=self.imp_statechange, user_data=opt) self.options.sort() -# imp_items.sort() firstopt = self.options[0][0] self.imp_lw = self.__gen_imp_listwalker(firstopt) -# self.imp_lw = RadioButtonListWalker([], on_state_change=self.imp_statechange, user_data=firstopt) - self.option_lw = OptionListWalker(cfdat.opt_dict, self.opt_statechange) + self.option_lw = OptionListWalker(cf_dat.opt_dict, self.opt_statechange) self.imp_lb = ImpListBox(self.imp_lw) urwid.connect_signal(self.option_lw, 'focus_change', self.update_imp_lb) - item_states = [(i,d['enabled']) for i,d in cf_dat.configuration_dict.iteritems()] item_states.sort() self.cfg_lw = RadioButtonListWalker(item_states, on_state_change = self.cf_statechange) @@ -334,10 +319,24 @@ class InstConfigManager: self.opt_lb.set_focus(0) return + def __imp_unavailable(self, imp): + if imp == 'none': + return False + + ckopt = self.cf_dat.imp2opt_dict[imp] + if ckopt == 'none': + return False + elif self.cf_dat.opt_dict[ckopt]['enabled']: + return True + else: + return False + def __gen_imp_listwalker(self, opt): imp_items = [] - dict = self.cfdat.opt_dict[opt] - for imp in self.cfdat.imp_dict[dict['imptype']]: + dict = self.cf_dat.opt_dict[opt] + for imp in self.cf_dat.imp_dict[dict['imptype']]: + if self.__imp_unavailable(imp): + continue if imp == dict['selected_imp']: imp_items.append((imp, True)) else: @@ -349,12 +348,12 @@ class InstConfigManager: def cf_statechange(self, button, new_state, udat=None): - self.cfdat.cf_statechange(button, new_state, udat) + self.cf_dat.cf_statechange(button, new_state, udat) b = button.get_label() - cascade = self.cfdat.configuration_dict[b]['cascade_list'] + cascade = self.cf_dat.configuration_dict[b]['cascade_list'] if new_state == True: - for opt in self.cfdat.opt_dict.keys(): - if self.cfdat.opt_dict[opt]['permanent'] == False: + for opt in self.cf_dat.opt_dict.keys(): + if self.cf_dat.opt_dict[opt]['permanent'] == False: self.option_lw.button_dict[opt].set_state(False) for opt,imp in cascade: self.option_lw.button_dict[opt].set_state(True) @@ -368,11 +367,23 @@ class InstConfigManager: return def opt_statechange(self, button, new_state, udat=None): - self.cfdat.opt_statechange(button, new_state, udat) + opt = button.get_label() + imp = self.cf_dat.opt_dict[opt]['selected_imp'] + if new_state == True: + if self.__imp_unavailable(imp): + self.cf_dat.opt_dict[opt]['selected_imp'] = 'none' + else: + self.cf_dat.imp2opt_dict[imp] = opt + + self.cf_dat.opt_statechange(button, new_state, udat) return - def imp_statechange(self, button, new_state, udat=None): - self.cfdat.imp_statechange(button, new_state, udat) + def imp_statechange(self, button, new_state, opt): + if new_state == True: + imp = button.get_label() + self.cf_dat.imp2opt_dict[imp] = opt + + self.cf_dat.imp_statechange(button, new_state, opt) return def update_imp_lb(self, pos):