From 362e490121ef34ffede0d4a550db4f708fc91805 Mon Sep 17 00:00:00 2001 From: Douglas Clowes Date: Wed, 28 Aug 2013 15:33:01 +1000 Subject: [PATCH] Add order=, recursion detection, --verbose --- site_ansto/instrument/util/config_edit.py | 81 ++++++++++++++++------- 1 file changed, 57 insertions(+), 24 deletions(-) diff --git a/site_ansto/instrument/util/config_edit.py b/site_ansto/instrument/util/config_edit.py index 3c5fb3e1..4e34ac65 100755 --- a/site_ansto/instrument/util/config_edit.py +++ b/site_ansto/instrument/util/config_edit.py @@ -5,6 +5,12 @@ import os import sys global config +global verbose +global depth_list, line_list +config = None +verbose = False +depth_list = [] +line_list = [] def read_config_file(config_filename): import ConfigParser @@ -42,14 +48,22 @@ def write_config_file(config, config_filename): #config.write(configfile) def do_cascade(config, name, cb_list, label, new_state): - if label in config.options(name): - cascade_list = config.get(name, label).lower().split(",") - for cb in cb_list: - lbl = cb.get_label().lower() - if ":" in lbl: - lbl = lbl.split(":")[0] - if lbl in cascade_list: - cb.set_state(new_state) + global depth_list + try: + if name in depth_list: + depth_list.append(name) + raise Exception("Recursion on " + str(depth_list)) + depth_list.append(name) + if label in config.options(name): + cascade_list = config.get(name, label).lower().split(",") + for cb in cb_list: + lbl = cb.get_label().lower() + if ":" in lbl: + lbl = lbl.split(":")[0] + if lbl in cascade_list: + cb.set_state(new_state) + finally: + depth_list.pop() def main(config_filename): import urwid @@ -61,22 +75,35 @@ def main(config_filename): u"UP/DOWN/PAGE UP/PAGE DOWN: scroll, r/R: Reread, q/Q/F8: Quit") def checkbox_change(check_box, new_state, user_data): - global config + global config, verbose, depth_list, line_list + if len(depth_list) == 0: + line_list = [] + frame.footer = urwid.AttrWrap(urwid.Text(""), 'header') config.set(user_data, 'enabled', str(new_state)) - line = u"Checkbox: name=%s, new_state=%s, user_data=%s" % ( - check_box.get_label(), str(new_state), str(user_data)) - frame.footer = urwid.AttrWrap(urwid.Text( - line), 'header') - if 'cascade' in config.options(user_data): - do_cascade(config, user_data, cb_list, 'cascade', new_state) - if new_state and 'onifon' in config.options(user_data): - do_cascade(config, user_data, cb_list, 'onifon', True) - if new_state and 'offifon' in config.options(user_data): - do_cascade(config, user_data, cb_list, 'offifon', False) - if (False == new_state) and 'onifoff' in config.options(user_data): - do_cascade(config, user_data, cb_list, 'onifoff', True) - if (False == new_state) and 'offifoff' in config.options(user_data): - do_cascade(config, user_data, cb_list, 'offifoff', False) + if verbose: + line = u"Checkbox: name=%s, new_state=%s, user_data=%s" % ( + check_box.get_label(), str(new_state), str(user_data)) + line_list.append(line + "\n") + frame.footer = urwid.AttrWrap(urwid.Text(line_list), 'header') + try: + order = ['offifoff', 'offifon', 'onifon', 'onifoff', 'cascade'] + if 'order' in config.options(user_data): + order = config.get(user_data, 'order').lower().split(",") + for item in order: + if 'cascade' == item: + do_cascade(config, user_data, cb_list, 'cascade', new_state) + if (True == new_state) and 'onifon' == item: + do_cascade(config, user_data, cb_list, 'onifon', True) + if (True == new_state) and 'offifon' == item: + do_cascade(config, user_data, cb_list, 'offifon', False) + if (False == new_state) and 'onifoff' == item: + do_cascade(config, user_data, cb_list, 'onifoff', True) + if (False == new_state) and 'offifoff' == item: + do_cascade(config, user_data, cb_list, 'offifoff', False) + except Exception as prang: + line = "Exception: %s" % prang + line_list.append(line + "\n") + frame.footer = urwid.AttrWrap(urwid.Text(line_list), 'header') text_cb_list = list(config.sections()) cb_map = {} @@ -135,6 +162,7 @@ def main(config_filename): sflw = urwid.SimpleListWalker(lb_list) listbox = urwid.ListBox(sflw) frame = urwid.Frame(urwid.AttrWrap(listbox, 'body'), header=header) + frame.footer = urwid.AttrWrap(urwid.Text(""), 'header') palette = [ ('body', 'black', 'light gray', 'standout'), @@ -169,12 +197,14 @@ def main(config_filename): # l.set_focus(b + 1) # frame.footer = urwid.AttrWrap(urwid.Text([u"EditB: " + str(b)]), 'header') else: - frame.footer = urwid.AttrWrap(urwid.Text([u"Press: " + str(key)]), 'header') + if not key[0] == 'mouse release': + frame.footer = urwid.AttrWrap(urwid.Text([u"Press: " + str(key)]), 'header') urwid.MainLoop(frame, palette, unhandled_input=unhandled).run() if '__main__'==__name__: + global verbose import argparse # set default_ini to the name of the file to edit when no name is supplied @@ -184,8 +214,11 @@ if '__main__'==__name__: Options can be enabled or disabled with mouse or spacebar. The default configuration filename is %s. """ % default_ini) + parser.add_argument("-v", "--verbose", action="store_true", help="give more infor in the footer") parser.add_argument("path", nargs="?", default = default_ini, help="name of file to edit [%s]" % default_ini) args = parser.parse_args() default_ini = os.path.abspath(args.path) + if args.verbose: + verbose = True main(default_ini)