Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- # version v14;
- # input valid done
- # #Readme; umount /run/user/131/gvfs ; rm -r /run/user/131/gvfs#
- # Problem: folder size problem
- from Tkinter import *
- import tkMessageBox
- import subprocess
- import thread
- import ttk
- import re
- root = Tk()
- root.title("Find Commmand GUI")
- # MessageBox to show program info
- def myabout():
- tkMessageBox.showinfo("About this program","Name:To Wing Pong\n"\
- "Student No.: 170034880\n" \
- "IVE Student"
- )
- # MessageBox to show the help info of the program
- def myhelp():
- tkMessageBox.showinfo("fping help",'Find Command GUI\n'\
- '"Name":\n is for the file or folder you want to search,\n\n'\
- '"type":\n is for the target you want to search,\n\n'\
- '"Mindepth":\n means start from which layer you want to search,\n\n'\
- '"Maxdepth":\n means the maximum layer you want the program to search, \n\n'\
- 'Size:\nmeans the target size,(you cannot search the folder size, because of the find command limit, custom location means the location you want to specify)"\n')
- # Exit the GUI program
- def myexit():
- root.destroy()
- #Menu bar
- menubar=Menu(root)
- filemenu = Menu(menubar, tearoff=0)
- filemenu.add_command(label="About",command=myabout)
- filemenu.add_command(label="Help Find ", command=myhelp)
- filemenu.add_separator()
- filemenu.add_command(label="Exit",command=myexit)
- menubar.add_cascade(label="Menu",menu=filemenu)
- '''
- # Second menu bar button #addition
- editmenu = Menu(menubar, tearoff=0)
- editmenu.add_command(label="Copy")
- editmenu.add_command(label="paste")
- menubar.add_cascade(label="Edit",menu=editmenu)
- '''
- root.config(menu=menubar)
- def thread_RunFindCommand():
- thread.start_new_thread(RunFindCommand,())
- def RunFindCommand():
- print "Run button is clicked"
- #Result = runcommand()
- txtarea_output.insert(END,runcommand())
- txtarea_output.insert(END,'-------------------------------\n')
- # Generate the command and return, Run by runcommand() function
- def command_gen():
- print "\nGenerating the find command"
- command= "find "
- # Add location of the finding (combobox)
- print "Checked Location " + str(checked_Location.get())
- if checked_Location.get():
- #command += tb_Location.get()
- command += nx.get()
- # checked MinDepth and MaxDepth are both checked and Min should not larger or equal to Max
- if checked_MinDepth.get() and checked_MaxDepth.get():
- if (int(tb_MinDepth.get())) >= (int(tb_MaxDepth.get())):
- tkMessageBox.showinfo('DepthErro','The MinDepth should not larger or equal to MaxDepth')
- return ''
- # MinDepth added to command, input validation
- print "Checked option MinDepth " + str(checked_MinDepth.get())
- if checked_MinDepth.get():
- if (re.match('$\d^',tb_MinDepth.get())) and len(tb_MinDepth.get()) > 0:
- command += " -mindepth " + tb_MinDepth.get()
- else:
- tkMessageBox.showinfo("InputError","The Mindepth should not empty and only accept integer")
- print "terminated to generate command, due to invalid input MinDepth"
- return ""
- # MaxDepth added to command, input validation
- print "Checked option MaxDepth " + str(checked_MaxDepth.get())
- if checked_MaxDepth.get():
- if (re.match('^\d+$',tb_MaxDepth.get())) and len (tb_MinDepth.get()) > 0:
- command += " -maxdepth " + tb_MaxDepth.get()
- else:
- tkMessageBox.showinfo("InputError","The Maxdepth should not empty and only accept integer")
- print "terminated to generate command, due to invali input MaxDepth"
- return ""
- # Size added to command, input valid
- print "Checked Size " + str(checked_Size.get())
- # if checked_Size.get(): <--This will appear bug if searching directory
- if checked_Size.get() and FileType.get() != 2:
- if re.match('^\d+$',tb_Size.get()) and ((int)(tb_Size.get())) > 0:
- command += " -size -" + tb_Size.get()+ "b"
- else:
- tkMessageBox.showinfo("SizeError","The Size shoud not be empty and only accept integer that greater than 0")
- print "terminated to generate command, due to invalid input size"
- return ""
- # Added File Type to command
- print "\nChecked option Type " + str(FileType.get())
- if FileType.get()==0:
- command = command
- elif FileType.get()==1:
- command += " -type f"
- elif FileType.get()==2:
- command += " -type d"
- # Fuzzy name search (case insentasive)
- command += " -iname " + "\"*" + tb_Name.get() + "*\""
- # Finally # Return the generated command
- return command
- # Run by main RunFindCommand() function
- def runcommand():
- print "\n\nRun command and get return!"
- input_command=command_gen()
- print "\n" + input_command + "\n"
- #SaveResult = subprocess.check_output ('pwd',shell=True) #Debug
- pb.grid(row=7,column=0,padx=5,pady=10,columnspan=10)
- thread.start_new_thread(pb.start,())
- SaveResult = subprocess.check_output (input_command,shell=True)
- result = "Output: This is the found result\n...\n...\n..."
- #print SaveResult #print out result in terminal #debug
- print "...\n...\nPrinted result\n\n"
- #Return the result
- pb.stop()
- pb.grid_forget()
- return SaveResult
- # Clear the find result
- def ClearResult():
- txtarea_output.delete(1.0,END)
- def ResetAll():
- # txtarea reset is start from 0 (0th index), entry reset is start from 1.0 (1st line),
- # decimal means how many index delete once
- #Entry reset
- tb_Name.delete(0,END)
- #TxtArea reset #Txtarea
- #txtarea_output.delete(1.0,END)
- ClearResult()
- #Location reset # Option Menu
- checked_Location.set(1)
- nx.set("~/Desktop")
- #tb_Location.delete(0,END)
- #Size Reset
- checked_Size.set(0)
- tb_Size.delete(0,END)
- # Depth Reset
- checked_MaxDepth.set(0)
- checked_MinDepth.set(0)
- tb_MaxDepth.delete(0,END)
- tb_MinDepth.delete(0,END)
- # Reset type to 'All Type'
- FileType.set(0)
- # Label of the 'Name', this is the file user want to search
- lb_Name = Label(root, text="Name",justify=LEFT)
- lb_Name.grid(row=0, column=0,padx=0,pady=7)
- # Entry of the 'Name'
- tb_Name = Entry(root,bd=3,width=35)
- tb_Name.grid(row=0, column=1, columnspan=2, padx=0,pady=7)
- # Option of file Type
- # File type radio button (value 1=All 2=File, 3=Dir)
- FileType=IntVar()
- rb_optionA = Radiobutton(root,text="All type",variable=FileType,value=0)
- rb_optionB = Radiobutton(root,text="File",variable=FileType,value=1)
- rb_optionC = Radiobutton(root,text="Directory",variable=FileType,value=2)
- rb_optionA.grid(row=1, column=0)
- rb_optionB.grid(row=1, column=1)
- rb_optionC.grid(row=1, column=2)
- # row = 2
- # Mindepth level
- # label of 'MinDepth'
- lb_MinDepth = Label(root, text="Mindepth")
- lb_MinDepth.grid(row=2,column=0)
- # Entry of 'MinDepth'
- tb_MinDepth=Entry(root,bd=2,width=5)
- tb_MinDepth.grid(row=2,column=1,columnspan=1,pady=1,sticky=W)
- # Enable button
- checked_MinDepth=IntVar()
- cb_MinDepth=Checkbutton(root,variable=checked_MinDepth)
- cb_MinDepth.grid(row=2,column=2)
- # row = 3
- # MaxDeptih levelss
- # Label of 'MaxDepth'
- lb_MaxDepth = Label(root, text="MaxDepth")
- lb_MaxDepth.grid(row=3,column=0)
- # Entry of 'MaxDepth'
- tb_MaxDepth=Entry(root,bd=2,width=5)
- tb_MaxDepth.grid(row=3,column=1,columnspan=1,pady=1,sticky=W)
- # Enable button
- checked_MaxDepth=IntVar()
- cb_MaxDepth=Checkbutton(root,variable=checked_MaxDepth)
- cb_MaxDepth.grid(row=3,column=2)
- # row = 4
- # Option of size
- #label of size
- lb_Size=Label(text="Size (byte), <")
- lb_Size.grid(row=4,column=0)
- # Entry of size
- tb_Size=Entry(root,bd=2,width=10)
- tb_Size.grid(row=4,column=1,pady=1,sticky=W)
- # Enable 'Size' button
- checked_Size=IntVar()
- cb_Size=Checkbutton(root,variable=checked_Size)
- cb_Size.grid(row=4,column=2)
- # row = 5
- # Custom Location
- lb_Location = Label(root, text="Custom Location")
- lb_Location.grid(row=5,column=0)
- # Combobox of location
- nx=StringVar(root)
- nx.set("~/Desktop")
- tb_Location=OptionMenu(root,nx,"~/Dekstop","~/Documents","~/Downloads","~/Pictures","~/Videos","~/Music","~/","/")
- tb_Location.config(width=10)
- tb_Location.grid(row=5,column=1,columnspan=1,pady=1,sticky=W)
- # Enable button of custom location
- checked_Location=IntVar()
- cb_Location=Checkbutton(root,variable=checked_Location)
- cb_Location.grid(row=5,column=2)
- checked_Location.set(1)
- # row = 6
- # Clear find result button
- btn_Reset = Button(root, text="ClearResult",bd=3,command=ClearResult)
- btn_Reset.grid(row=6, column=0)
- # Reset Button
- btn_Reset = Button(root, text="Reset",bd=3,command=ResetAll)
- btn_Reset.grid(row=6, column=1)
- # Run button
- btn_run = Button(root, text="Run!",bd=3,command=thread_RunFindCommand)
- btn_run.grid(row=6, column=2)
- # Process bar
- pb=ttk.Progressbar(root,orient="horizontal",length=250,mode="indeterminate")
- # A label of result "Command Output:"
- lb_commandoutput = Label(root, text="Command Output:")
- lb_commandoutput.grid(row=8, column=0)
- # A txtbox to display the result of the find command
- txtarea_output = Text(root, height=10, width=50)
- txtarea_output.grid(row=9, column=0, columnspan=4,padx=9,pady=12)
- # Add Scroll bar to the left of the txtarea result box
- scr_output = Scrollbar(root,orient='vertical')
- scr_output.grid(row=9,column=3,sticky=S+N+E,padx=9,pady=12)
- txtarea_output.config(yscrollcommand=scr_output.set)
- scr_output.config(command=txtarea_output.yview)
- # keep the GUI alive
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement