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