131 lines
4.4 KiB
Python
Executable File
131 lines
4.4 KiB
Python
Executable File
#!/usr/bin/env python
|
|
# vim: tabstop=8 softtabstop=2 shiftwidth=2 nocin si et ft=python
|
|
|
|
import os
|
|
import sys
|
|
|
|
def main(config_filename):
|
|
import urwid
|
|
import ConfigParser
|
|
|
|
config = ConfigParser.SafeConfigParser()
|
|
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))
|
|
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):
|
|
cascade_list = config.get(user_data, 'cascade').split(",")
|
|
for cb in cb_list:
|
|
lbl = cb.get_label().split(":")[0]
|
|
if lbl in cascade_list:
|
|
cb.set_state(new_state)
|
|
|
|
text_cb_list = list(config.sections())
|
|
cb_list = []
|
|
for txt in sorted(text_cb_list):
|
|
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 'desc' in config.options(txt):
|
|
cb_text += ": (" + config.get(txt, 'desc').strip("\"") + ")"
|
|
cb = urwid.CheckBox(cb_text,
|
|
state=state,
|
|
on_state_change=checkbox_change,
|
|
user_data=txt)
|
|
cb_list.append(cb)
|
|
|
|
def fcc(focus):
|
|
frame.footer = urwid.AttrWrap(urwid.Text(
|
|
[u"Focus: " + str(focus)]), 'header')
|
|
|
|
|
|
blank = urwid.Divider()
|
|
listbox_content = [
|
|
blank,
|
|
urwid.Padding(urwid.GridFlow(
|
|
[urwid.AttrWrap(cb, 'buttn', 'buttnf') for cb in cb_list],
|
|
60, 1, 0, 'left'),
|
|
left=4, right=3, min_width=60),
|
|
blank,
|
|
blank
|
|
]
|
|
|
|
if False:
|
|
listbox = urwid.Pile(cb_list)
|
|
else:
|
|
if "SimpleFocusListWalker" in dir(urwid):
|
|
sflw = urwid.SimpleFocusListWalker(listbox_content)
|
|
sflw.set_focus_changed_callback(fcc)
|
|
else:
|
|
sflw = urwid.SimpleListWalker(listbox_content)
|
|
|
|
header = urwid.AttrWrap(urwid.Text(text_header), 'header')
|
|
listbox = urwid.ListBox(sflw)
|
|
frame = urwid.Frame(urwid.AttrWrap(listbox, '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'),
|
|
]
|
|
|
|
def unhandled(key):
|
|
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 ['e', 'E', 'f4']:
|
|
f = sflw.get_focus()
|
|
frame.footer = urwid.AttrWrap(urwid.Text([u"EditE: " + str(dir(f[0]))]), 'header')
|
|
elif key in ['q', 'Q', 'f8']:
|
|
raise urwid.ExitMainLoop()
|
|
# elif key in ['b', 'B']:
|
|
# l = sflw[1].base_widget
|
|
# f = l.focus_cell
|
|
# b = l.cells.index(f)
|
|
# l.cells.insert(b + 1, urwid.AttrWrap(urwid.Edit(edit_text=u"Hello"), 'header'))
|
|
# 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')
|
|
|
|
urwid.MainLoop(frame, palette, unhandled_input=unhandled).run()
|
|
|
|
|
|
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")
|
|
args = parser.parse_args()
|
|
default_ini = os.path.abspath(args.path)
|
|
main(default_ini)
|
|
|