Write input file with backups

This commit is contained in:
Douglas Clowes
2013-08-19 11:29:29 +10:00
parent 45a20823ba
commit dcca4385b3

View File

@@ -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')
cb_list.append(cb)
print cb_list
def fcc(focus):
frame.footer = urwid.AttrWrap(urwid.Text(
@@ -45,7 +50,9 @@ 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'),
@@ -61,9 +68,16 @@ def main():
]
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")