From dcca4385b3ace13cef9d807fcd1dcdfa4e664ad3 Mon Sep 17 00:00:00 2001 From: Douglas Clowes Date: Mon, 19 Aug 2013 11:29:29 +1000 Subject: [PATCH] Write input file with backups --- site_ansto/instrument/util/config_edit.py | 60 ++++++++++++++--------- 1 file changed, 36 insertions(+), 24 deletions(-) diff --git a/site_ansto/instrument/util/config_edit.py b/site_ansto/instrument/util/config_edit.py index 00ea7a3d..331917c3 100644 --- a/site_ansto/instrument/util/config_edit.py +++ b/site_ansto/instrument/util/config_edit.py @@ -1,15 +1,18 @@ #!/usr/bin/env python # vim: tabstop=8 softtabstop=2 shiftwidth=2 nocindent smartindent +import os import sys import urwid import ConfigParser -def main(): +def main(config_filename): config = ConfigParser.SafeConfigParser() - config.read("my_config_parser.ini") - text_header = (u"SICS Config Editor!\n" - u"UP / DOWN / PAGE UP / PAGE DOWN scroll. F8 exits.") + config.read(config_filename) + #print config_filename + #config.write(sys.stdout) + text_header = (u"SICS Config Editor! w/W/F12 Saves & Exits\n" + u"UP / DOWN / PAGE UP / PAGE DOWN scroll. q/Q/F8 Exits.") def checkbox_change(check_box, new_state, user_data): config.set(user_data, 'enabled', str(new_state)) @@ -19,7 +22,6 @@ def main(): line), 'header') text_cb_list = list(config.sections()) - print text_cb_list cb_list = [] for txt in text_cb_list: state = False @@ -27,14 +29,17 @@ def main(): if config.get(txt, 'enabled').lower() in ['1', 'yes', 'true']: state = True config.set(txt, 'enabled', str(state)) + cb_text = "" + cb_text += txt + if 'name' in config.options(txt): + cb_text += ": (" + config.get(txt, 'name') + ")" cb = urwid.AttrWrap( - urwid.CheckBox(txt, + urwid.CheckBox(cb_text, state=state, on_state_change=checkbox_change, user_data=txt), - 'buttn','buttnf') + 'buttn', 'buttnf') cb_list.append(cb) - print cb_list def fcc(focus): frame.footer = urwid.AttrWrap(urwid.Text( @@ -45,25 +50,34 @@ def main(): listbox = urwid.ListBox(sflw) header = urwid.AttrWrap(urwid.Text(text_header), 'header') - frame = urwid.Frame(urwid.AttrWrap(listbox, 'body'), header=header) + frame = urwid.Frame( + urwid.AttrWrap(urwid.Padding(listbox, left=2, right=2), 'body'), + header=header) palette = [ - ('body','black','light gray', 'standout'), - ('reverse','light gray','black'), - ('header','white','dark red', 'bold'), - ('important','dark blue','light gray',('standout','underline')), - ('editfc','white', 'dark blue', 'bold'), - ('editbx','light gray', 'dark blue'), - ('editcp','black','light gray', 'standout'), - ('bright','dark gray','light gray', ('bold','standout')), - ('buttn','black','dark cyan'), - ('buttnf','white','dark blue','bold'), + ('body', 'black', 'light gray', 'standout'), + ('reverse', 'light gray', 'black'), + ('header', 'white', 'dark red', 'bold'), + ('important', 'dark blue', 'light gray', ('standout', 'underline')), + ('editfc', 'white', 'dark blue', 'bold'), + ('editbx', 'light gray', 'dark blue'), + ('editcp', 'black', 'light gray', 'standout'), + ('bright', 'dark gray', 'light gray', ('bold', 'standout')), + ('buttn', 'black', 'dark cyan'), + ('buttnf', 'white', 'dark blue', 'bold'), ] def unhandled(key): - if key in ['w', 'W', 'f10']: - with open("junk.ini", "wb") as configfile: + if key in ['w', 'W', 'f12']: + for idx in range(8, 0, -1): + if os.path.exists(config_filename + "." + str(idx)): + os.rename(config_filename + "." + str(idx), + config_filename + "." + str(idx - 1)) + if os.path.exists(config_filename): + os.rename(config_filename, config_filename + ".1") + with open(config_filename, "wb") as configfile: config.write(configfile) + raise urwid.ExitMainLoop() elif key in ['q', 'Q', 'f8']: raise urwid.ExitMainLoop() else: @@ -72,9 +86,7 @@ def main(): urwid.MainLoop(frame, palette, unhandled_input=unhandled).run() -def setup(): - main() if '__main__'==__name__: - setup() + main("my_config_parser.ini")