diff --git a/site_ansto/instrument/util/config_edit.py b/site_ansto/instrument/util/config_edit.py index 54b53ea6..c435d786 100755 --- a/site_ansto/instrument/util/config_edit.py +++ b/site_ansto/instrument/util/config_edit.py @@ -29,6 +29,128 @@ import copy from collections import defaultdict +## TODO Configuration Editor +## Configuration Viewer +Palette = [ + ('body', 'dark cyan', '', 'standout'), + ('focus', 'dark red', '', 'standout'), + ('head', 'light red', 'black'), +] + + +#FIXME Replace the [(name,stateval)] list imp_states with list of item names +class RadioButtonListWalker(urwid.SimpleListWalker): + button_dict = {} + def __init__(self, item_states, on_state_change=None, user_data=None): + radio_grp = [] + mapped_rb_list = [] + for item,stateval in item_states: + rb = urwid.RadioButton(radio_grp, item, state=stateval, on_state_change=on_state_change, user_data=user_data) + self.button_dict[item] = rb + mapped_rb = urwid.AttrMap(rb, 'body', 'focus') + mapped_rb_list.append(mapped_rb) + + super(RadioButtonListWalker, self).__init__(mapped_rb_list) + return + + +class CheckBoxListWalker(urwid.SimpleListWalker): + button_dict = {} + def __init__(self, item_states, on_state_change = None, user_data = None): + mapped_cb_list = [] + for item,stateval in item_states: + cb = urwid.CheckBox(item, state = stateval, on_state_change = on_state_change, user_data = user_data) + self.button_dict[item] = cb + mapped_cb = urwid.AttrMap(cb, 'body', 'focus') + mapped_cb_list.append(mapped_cb) + + super(CheckBoxListWalker, self).__init__(mapped_cb_list) + 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']) + item_states = [(i,d['enabled']) for i,d in opt_dict.iteritems()] + item_states.sort() + + super(OptionListWalker, self).__init__(item_states, statechange_cb) + return + + def set_focus(self, pos): + dbg.msg(0, 'OptionListWalker:set_focus({0}) -> emit focus_change'.format(pos)) + urwid.emit_signal(self, 'focus_change', pos) + return super(OptionListWalker, self).set_focus(pos) + + +# ClosedListBox implements a ListBox which prevents selection outside of the +# list using the 'up' or 'down' keys +class ClosedListBox(urwid.ListBox): + + def keypress(self, size, key): + """Prevents navigating outside of a ClosedListBox with the up and down arrow keys""" + pos = self.get_focus()[1] + ll = len(self.body) + if (pos <= 0 and key == 'up') or (pos >= ll-1 and key == 'down'): + return + else: + return super(ClosedListBox, self).keypress(size, key) + + +# List of Checkboxes +class OptionListBox(ClosedListBox): + def __init__(self, listwalker): + super(OptionListBox, self).__init__(listwalker) + return + +# List of RadioButtons +class ImpListBox(ClosedListBox): + def __init__(self, listwalker): + super(ImpListBox, self).__init__(listwalker) + return + + def use_listwalker(self, liswalker): + self.body.contents[:] = liswalker + return + + +class InstConfigView(urwid.Pile): + + def __init__(self, cf_dat, cf_man, dbmsg): + self.cf_dat = cf_dat + self.cf_man = cf_man + option_ListBoxes = [ + self.cf_man.config_lb, + self.cf_man.opt_lb, + self.cf_man.imp_lb, + dbmsg] + super(InstConfigView, self).__init__(option_ListBoxes) + return + + def keyinput(self, key): + if key == 'meta q': + raise urwid.ExitMainLoop() + elif key == 'w': + self.cf_dat.backup_files() + self.cf_dat.write_config_file() + elif key in ['right', 'tab']: + if self.get_focus() == self.cf_man.config_lb: + self.set_focus(self.cf_man.opt_lb) + elif self.get_focus() == self.cf_man.opt_lb: + self.set_focus(self.cf_man.imp_lb) + else: + self.set_focus(self.cf_man.config_lb) + elif key in ['left', 'shift tab']: + if self.get_focus() == self.cf_man.config_lb: + self.set_focus(self.cf_man.imp_lb) + elif self.get_focus() == self.cf_man.opt_lb: + self.set_focus(self.cf_man.config_lb) + else: + self.set_focus(self.cf_man.opt_lb) + return + + class InstConfigData: msg_index = 4 # configuration_dict: dict of instrument configurations as defined below, @@ -166,81 +288,6 @@ class InstConfigData: self.opt_dict[opt]['selected_imp'] = selected_imp -## TODO Configuration Editor -## Configuration Viewer -Palette = [ - ('body', 'dark cyan', '', 'standout'), - ('focus', 'dark red', '', 'standout'), - ('head', 'light red', 'black'), -] - - -#FIXME Replace the [(name,stateval)] list imp_states with list of item names -class RadioButtonListWalker(urwid.SimpleListWalker): - button_dict = {} - def __init__(self, item_states, on_state_change=None, user_data=None): - radio_grp = [] - mapped_rb_list = [] - for item,stateval in item_states: - rb = urwid.RadioButton(radio_grp, item, state=stateval, on_state_change=on_state_change, user_data=user_data) - self.button_dict[item] = rb - mapped_rb = urwid.AttrMap(rb, 'body', 'focus') - mapped_rb_list.append(mapped_rb) - - super(RadioButtonListWalker, self).__init__(mapped_rb_list) - return - - -class CheckBoxListWalker(urwid.SimpleListWalker): - button_dict = {} - def __init__(self, item_states, on_state_change = None, user_data = None): - mapped_cb_list = [] - for item,stateval in item_states: - cb = urwid.CheckBox(item, state = stateval, on_state_change = on_state_change, user_data = user_data) - self.button_dict[item] = cb - mapped_cb = urwid.AttrMap(cb, 'body', 'focus') - mapped_cb_list.append(mapped_cb) - - super(CheckBoxListWalker, self).__init__(mapped_cb_list) - 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']) - item_states = [(i,d['enabled']) for i,d in opt_dict.iteritems()] - item_states.sort() - - super(OptionListWalker, self).__init__(item_states, statechange_cb) - return - - def set_focus(self, pos): - dbg.msg(0, 'OptionListWalker:set_focus({0}) -> emit focus_change'.format(pos)) - urwid.emit_signal(self, 'focus_change', pos) - return super(OptionListWalker, self).set_focus(pos) - - -# ClosedListBox implements a ListBox which prevents selection outside of the -# list using the 'up' or 'down' keys -class ClosedListBox(urwid.ListBox): - - def keypress(self, size, key): - """Prevents navigating outside of a ClosedListBox with the up and down arrow keys""" - pos = self.get_focus()[1] - ll = len(self.body) - if (pos <= 0 and key == 'up') or (pos >= ll-1 and key == 'down'): - return - else: - return super(ClosedListBox, self).keypress(size, key) - - -# List of Checkboxes -class OptionListBox(ClosedListBox): - def __init__(self, listwalker): - super(OptionListBox, self).__init__(listwalker) - return - # Contains OptionListWalker dict indexed by option # Contains ImpListBox # Connects OptionListWalker 'focus_change' signal to update_imp_lb handler @@ -333,52 +380,6 @@ class InstConfigManager: return -# List of RadioButtons -class ImpListBox(ClosedListBox): - def __init__(self, listwalker): - super(ImpListBox, self).__init__(listwalker) - return - - def use_listwalker(self, liswalker): - self.body.contents[:] = liswalker - return - - -class InstConfigView(urwid.Pile): - - def __init__(self, cf_dat, cf_man, dbmsg): - self.cf_dat = cf_dat - self.cf_man = cf_man - option_ListBoxes = [ - self.cf_man.config_lb, - self.cf_man.opt_lb, - self.cf_man.imp_lb, - dbmsg] - super(InstConfigView, self).__init__(option_ListBoxes) - return - - def keyinput(self, key): - if key == 'meta q': - raise urwid.ExitMainLoop() - elif key == 'w': - self.cf_dat.backup_files() - self.cf_dat.write_config_file() - elif key in ['right', 'tab']: - if self.get_focus() == self.cf_man.config_lb: - self.set_focus(self.cf_man.opt_lb) - elif self.get_focus() == self.cf_man.opt_lb: - self.set_focus(self.cf_man.imp_lb) - else: - self.set_focus(self.cf_man.config_lb) - elif key in ['left', 'shift tab']: - if self.get_focus() == self.cf_man.config_lb: - self.set_focus(self.cf_man.imp_lb) - elif self.get_focus() == self.cf_man.opt_lb: - self.set_focus(self.cf_man.config_lb) - else: - self.set_focus(self.cf_man.opt_lb) - return - import pdb class DEBUG: msgTextDict = {}