Implement command to reread the configuration file
This commit is contained in:
@@ -4,12 +4,28 @@
|
||||
import os
|
||||
import sys
|
||||
|
||||
global config
|
||||
|
||||
def read_config_file(config_filename):
|
||||
import ConfigParser
|
||||
config = ConfigParser.SafeConfigParser()
|
||||
config.read(config_filename)
|
||||
return config
|
||||
|
||||
def reread_config_file(config_filename, cb_list):
|
||||
config = read_config_file(config_filename)
|
||||
for cb in cb_list:
|
||||
lbl = cb.get_label()
|
||||
if ":" in lbl:
|
||||
lbl = lbl.split(":")[0]
|
||||
state = False
|
||||
if 'enabled' in config.options(lbl):
|
||||
if config.get(lbl, 'enabled').lower() in ['1', 'yes', 'true']:
|
||||
state = True
|
||||
config.set(lbl, 'enabled', str(state))
|
||||
cb.set_state(state, False)
|
||||
return config
|
||||
|
||||
def write_config_file(config, config_filename):
|
||||
for idx in range(8, 0, -1):
|
||||
if os.path.exists(config_filename + "." + str(idx)):
|
||||
@@ -29,19 +45,23 @@ 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().split(":")[0].lower()
|
||||
lbl = cb.get_label().lower()
|
||||
if ":" in lbl:
|
||||
lbl = lbl.split(":")[0]
|
||||
if lbl in cascade_list:
|
||||
cb.set_state(new_state)
|
||||
|
||||
def main(config_filename):
|
||||
import urwid
|
||||
global config
|
||||
|
||||
config = read_config_file(config_filename)
|
||||
text_header = (
|
||||
u"SICS Config Editor! w/W/F12 Saves & Exits\n"
|
||||
u"UP / DOWN / PAGE UP / PAGE DOWN scroll. q/Q/F8 Exits.")
|
||||
u"SICS Config Editor! SPACE/ENTER: Toggle, w/W/F12: Write+Exit\n"
|
||||
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
|
||||
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))
|
||||
@@ -68,27 +88,30 @@ def main(config_filename):
|
||||
if group not in cb_map:
|
||||
cb_map[group] = []
|
||||
cb_map[group].append(txt)
|
||||
lb_list = []
|
||||
cb_list = []
|
||||
for key in sorted(cb_map.keys()):
|
||||
#cb_list.append(urwid.AttrWrap(urwid.Text("Group: %s" % key), 'bright'))
|
||||
lb_list.append(urwid.AttrWrap(urwid.Text("Group: %s" % key), 'bright'))
|
||||
for txt in sorted(cb_map[key]):
|
||||
state = False
|
||||
if 'enabled' in config.options(txt):
|
||||
if config.get(txt, 'enabled').lower() in ['1', 'yes', 'true']:
|
||||
state = True
|
||||
config.set(txt, 'enabled', str(state))
|
||||
cb_text = ""
|
||||
cb_text += txt + ": ("
|
||||
if 'group' in config.options(txt):
|
||||
cb_text += config.get(txt, 'group').strip("\"") + ": "
|
||||
if 'desc' in config.options(txt):
|
||||
cb_text += config.get(txt, 'desc').strip("\"")
|
||||
cb_text += ")"
|
||||
cb_text = txt
|
||||
if 'group' in config.options(txt) or 'desc' in config.options(txt):
|
||||
cb_text += ": ("
|
||||
if 'group' in config.options(txt):
|
||||
cb_text += config.get(txt, 'group').strip("\"") + ": "
|
||||
if 'desc' in config.options(txt):
|
||||
cb_text += config.get(txt, 'desc').strip("\"")
|
||||
cb_text += ")"
|
||||
cb = urwid.AttrWrap(urwid.CheckBox(cb_text,
|
||||
state=state,
|
||||
on_state_change=checkbox_change,
|
||||
user_data=txt), 'buttn', 'buttnf')
|
||||
cb_list.append(cb)
|
||||
lb_list.append(cb)
|
||||
|
||||
def fcc(focus):
|
||||
frame.footer = urwid.AttrWrap(urwid.Text(
|
||||
@@ -133,9 +156,12 @@ def main(config_filename):
|
||||
]
|
||||
|
||||
def unhandled(key):
|
||||
global config
|
||||
if key in ['w', 'W', 'f12']:
|
||||
write_config_file(config, config_filename)
|
||||
raise urwid.ExitMainLoop()
|
||||
elif key in ['r', 'R']:
|
||||
config = reread_config_file(config_filename, cb_list)
|
||||
elif key in ['e', 'E', 'f4']:
|
||||
f = sflw.get_focus()
|
||||
frame.footer = urwid.AttrWrap(urwid.Text([u"EditE: " + str(dir(f[0]))]), 'header')
|
||||
@@ -157,9 +183,14 @@ def main(config_filename):
|
||||
if '__main__'==__name__:
|
||||
import argparse
|
||||
|
||||
default_ini = "my_config_parser.ini"
|
||||
parser = argparse.ArgumentParser(description = "Edit a configuration (*.ini) file using python urwin")
|
||||
parser.add_argument("path", nargs="?", default = default_ini, help="name of file to edit")
|
||||
# set default_ini to the name of the file to edit when no name is supplied
|
||||
default_ini = "experiment_configuration.ini"
|
||||
parser = argparse.ArgumentParser(description = """
|
||||
Edit a configuration (*.ini) file using python urwid widget library.
|
||||
Options can be enabled or disabled with mouse or spacebar.
|
||||
The default configuration filename is %s.
|
||||
""" % default_ini)
|
||||
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)
|
||||
main(default_ini)
|
||||
|
||||
Reference in New Issue
Block a user