#!/usr/bin/python
# -*- coding:utf-8 -*-
import tkFileDialog, Tkinter, tkMessageBox
import py_compile
# ascii utf-8
import sys
reload(sys)
sys.setdefaultencoding('utf8')
def gui():
#
def filedir():
filedire = tkFileDialog.askdirectory()
if filedire == '':
textdir.set('')
else:
textdir.set(filedire)
# Config
def filenam():
filename = tkFileDialog.askopenfilename()
if filename == '':
textfile.set('')
else:
textfile.set(filename)
#
def panduan():
if textdir.get() == '':
tkMessageBox.showinfo("Info", " ")
return
elif textfile.get() == '':
tkMessageBox.showinfo("Info", "Config ")
return
else:
test.destroy()
#
test = Tkinter.Tk()
test.geometry('400x180')
test.title(' ')
# host
label = Tkinter.Label(test, text = " host :")
label.pack()
textfile = Tkinter.StringVar()
textfile.set("")
entryfile = Tkinter.Entry(test, textvariable = textfile)
entryfile.pack()
buttonfile = Tkinter.Button(test, text = " Config ", command = filenam)
buttonfile.pack()
# config
label = Tkinter.Label(test, text = " :")
label.pack()
textdir = Tkinter.StringVar()
textdir.set("C:/Config")
entrydir = Tkinter.Entry(test, textvariable = textdir)
entrydir.pack()
buttondir = Tkinter.Button(test, text = " ", command = filedir)
buttondir.pack()
#
Confirmbutton = Tkinter.Button(test, text = " ", command = panduan)
Confirmbutton.pack()
test.mainloop()
return (textdir.get(), textfile.get())
if __name__ == '__main__':
# gui
(dirname, filename) = gui()
# py
# readline: config.txt
# writeline: [new].py
# mainfile: main.txt
readfile = open(filename, 'r')
mainfile = open("main.txt", 'r')
writefile = open(filename + "_config.py", 'w')
writefile.write('''#!/usr/bin/python
# -*- coding:utf-8 -*-
import paramiko
import os,sys,time
import re
''')
writefile.write("dirname = \"" + dirname + '\"
')
writefile.write("device = []
")
for line in readfile:
if not line.strip() == '':
writefile.write("device.append(\"" + line.strip('
') + "\")
")
for line in mainfile:
writefile.write(line)
readfile.close()
mainfile.close()
writefile.close()
#
test = Tkinter.Tk()
test.geometry("0x0")
tkMessageBox.showinfo("Info", " " + filename + "_config.pyc")
test.destroy()
py_compile.compile(filename + "_config.py")
# 【debug】
# if os.path.exists(filename + "_config.py"):
# os.remove(filename + "_config.py")