我编写了一个非常简单的应用程序,它根据用户输入生成配置文件。然而,数据从 StringIO 转储到实际 conf 文件的顺序对于使用该文件的程序很重要。我在代码中解决这个问题的方法是自上而下的数据输入模型。但是如果用户乱序输入数据,这会导致程序失败或者生成的conf文件将变得无用。有没有办法重新协调随机数据输入顺序并确保来自 StringIO 的数据以特定顺序插入?
目前代码看起来像这样(在你们的大力帮助下,它到了这个阶段!)
self.output = StringIO.StringIO()
context = self.toolbar.get_style_context()
context.add_class(Gtk.STYLE_CLASS_PRIMARY_TOOLBAR)
def on_servername_activate(self, widget):
output = StringIO.StringIO()
servername = widget.get_text()
self.output.write("USHARE_NAME="+servername+'\n')
def on_netif_changed(self, widget):
netif = widget.get_active_text()
self.output.write("USHARE_IFACE="+netif+'\n')
def on_port_activate(self, widget):
port = widget.get_text()
self.output.write("USHARE_PORT="+port+'\n')
def on_telprt_activate(self, widget):
telprt = widget.get_text()
self.output.write("USHARE_TELNET_PORT="+telprt+'\n')
def on_dirs_activate(self, widget):
dirs = widget.get_text()
self.output.write("USHARE_DIR="+dirs+'\n')
def on_iconv_toggled(self, widget):
iconv = widget.get_active()
if iconv == True:
self.output.write("USHARE_OVERRIDE_ICONV_ERR="+"True"+'\n')
else:
self.output.write("USHARE_OVERRIDE_ICONV_ERR="+"False"+'\n')
def on_webif_toggled(self, widget):
webif = widget.get_active()
if webif == True:
self.output.write("USHARE_ENABLE_WEB="+"yes"+'\n')
else:
self.output.write("USHARE_ENABLE_WEB="+"no"+'\n')
def on_telif_toggled(self, widget):
telif = widget.get_active()
if telif == True:
self.output.write("USHARE_ENABLE_TELNET="+"yes"+'\n')
else:
self.output.write("USHARE_ENABLE_TELNET="+"no"+'\n')
def on_xbox_toggled(self, widget):
xbox = widget.get_active()
if xbox == True:
self.output.write("USHARE_ENABLE_XBOX="+"yes"+'\n')
else:
self.output.write("USHARE_ENABLE_XBOX="+"no"+'\n')
def on_dlna_toggled(self, widget):
dlna = widget.get_active()
if dlna == True:
self.output.write("USHARE_ENABLE_DLNA="+"yes"+'\n')
else:
self.output.write("USHARE_ENABLE_DLNA="+"no"+'\n')
def on_commit_clicked(self, widget):
commit = self.output.getvalue()
logfile = open('/home/boywithaxe/Desktop/ushare.conf','w')
logfile.write(commit)
def on_endprogram_clicked(self, widget):
sys.exit(0)