Advertisement
Guest User

Untitled

a guest
Jul 16th, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.77 KB | None | 0 0
  1. #!/usr/bin/python
  2. # version v14;
  3. # input valid done
  4.  
  5. # #Readme; umount /run/user/131/gvfs ; rm -r /run/user/131/gvfs#
  6. # Problem: folder size problem
  7.  
  8. from Tkinter import *
  9. import tkMessageBox
  10. import subprocess
  11. import thread
  12. import ttk
  13. import re
  14.  
  15. root = Tk()
  16. root.title("Find Commmand GUI")
  17.  
  18. # MessageBox to show program info
  19. def myabout():
  20. tkMessageBox.showinfo("About this program","Name:To Wing Pong\n"\
  21. "Student No.: 170034880\n" \
  22. "IVE Student"
  23. )
  24. # MessageBox to show the help info of the program
  25. def myhelp():
  26. tkMessageBox.showinfo("fping help",'Find Command GUI\n'\
  27. '"Name":\n is for the file or folder you want to search,\n\n'\
  28. '"type":\n is for the target you want to search,\n\n'\
  29. '"Mindepth":\n means start from which layer you want to search,\n\n'\
  30. '"Maxdepth":\n means the maximum layer you want the program to search, \n\n'\
  31. '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')
  32.  
  33. # Exit the GUI program
  34. def myexit():
  35. root.destroy()
  36.  
  37.  
  38.  
  39. #Menu bar
  40. menubar=Menu(root)
  41.  
  42. filemenu = Menu(menubar, tearoff=0)
  43. filemenu.add_command(label="About",command=myabout)
  44. filemenu.add_command(label="Help Find ", command=myhelp)
  45. filemenu.add_separator()
  46. filemenu.add_command(label="Exit",command=myexit)
  47. menubar.add_cascade(label="Menu",menu=filemenu)
  48.  
  49.  
  50.  
  51. '''
  52. # Second menu bar button #addition
  53. editmenu = Menu(menubar, tearoff=0)
  54. editmenu.add_command(label="Copy")
  55. editmenu.add_command(label="paste")
  56. menubar.add_cascade(label="Edit",menu=editmenu)
  57. '''
  58.  
  59. root.config(menu=menubar)
  60.  
  61.  
  62. def thread_RunFindCommand():
  63. thread.start_new_thread(RunFindCommand,())
  64.  
  65. def RunFindCommand():
  66. print "Run button is clicked"
  67. #Result = runcommand()
  68. txtarea_output.insert(END,runcommand())
  69. txtarea_output.insert(END,'-------------------------------\n')
  70.  
  71.  
  72. # Generate the command and return, Run by runcommand() function
  73. def command_gen():
  74. print "\nGenerating the find command"
  75.  
  76. command= "find "
  77.  
  78. # Add location of the finding (combobox)
  79. print "Checked Location " + str(checked_Location.get())
  80. if checked_Location.get():
  81. #command += tb_Location.get()
  82. command += nx.get()
  83.  
  84. # checked MinDepth and MaxDepth are both checked and Min should not larger or equal to Max
  85. if checked_MinDepth.get() and checked_MaxDepth.get():
  86. if (int(tb_MinDepth.get())) >= (int(tb_MaxDepth.get())):
  87. tkMessageBox.showinfo('DepthErro','The MinDepth should not larger or equal to MaxDepth')
  88. return ''
  89.  
  90. # MinDepth added to command, input validation
  91. print "Checked option MinDepth " + str(checked_MinDepth.get())
  92. if checked_MinDepth.get():
  93. if (re.match('$\d^',tb_MinDepth.get())) and len(tb_MinDepth.get()) > 0:
  94. command += " -mindepth " + tb_MinDepth.get()
  95. else:
  96. tkMessageBox.showinfo("InputError","The Mindepth should not empty and only accept integer")
  97. print "terminated to generate command, due to invalid input MinDepth"
  98. return ""
  99.  
  100. # MaxDepth added to command, input validation
  101. print "Checked option MaxDepth " + str(checked_MaxDepth.get())
  102. if checked_MaxDepth.get():
  103. if (re.match('^\d+$',tb_MaxDepth.get())) and len (tb_MinDepth.get()) > 0:
  104. command += " -maxdepth " + tb_MaxDepth.get()
  105. else:
  106. tkMessageBox.showinfo("InputError","The Maxdepth should not empty and only accept integer")
  107. print "terminated to generate command, due to invali input MaxDepth"
  108. return ""
  109.  
  110. # Size added to command, input valid
  111. print "Checked Size " + str(checked_Size.get())
  112. # if checked_Size.get(): <--This will appear bug if searching directory
  113. if checked_Size.get() and FileType.get() != 2:
  114. if re.match('^\d+$',tb_Size.get()) and ((int)(tb_Size.get())) > 0:
  115. command += " -size -" + tb_Size.get()+ "b"
  116. else:
  117. tkMessageBox.showinfo("SizeError","The Size shoud not be empty and only accept integer that greater than 0")
  118. print "terminated to generate command, due to invalid input size"
  119. return ""
  120.  
  121. # Added File Type to command
  122. print "\nChecked option Type " + str(FileType.get())
  123. if FileType.get()==0:
  124. command = command
  125. elif FileType.get()==1:
  126. command += " -type f"
  127. elif FileType.get()==2:
  128. command += " -type d"
  129.  
  130. # Fuzzy name search (case insentasive)
  131. command += " -iname " + "\"*" + tb_Name.get() + "*\""
  132.  
  133. # Finally # Return the generated command
  134.  
  135. return command
  136.  
  137. # Run by main RunFindCommand() function
  138. def runcommand():
  139. print "\n\nRun command and get return!"
  140. input_command=command_gen()
  141.  
  142. print "\n" + input_command + "\n"
  143.  
  144. #SaveResult = subprocess.check_output ('pwd',shell=True) #Debug
  145.  
  146.  
  147. pb.grid(row=7,column=0,padx=5,pady=10,columnspan=10)
  148. thread.start_new_thread(pb.start,())
  149. SaveResult = subprocess.check_output (input_command,shell=True)
  150.  
  151. result = "Output: This is the found result\n...\n...\n..."
  152. #print SaveResult #print out result in terminal #debug
  153. print "...\n...\nPrinted result\n\n"
  154. #Return the result
  155. pb.stop()
  156. pb.grid_forget()
  157. return SaveResult
  158.  
  159.  
  160.  
  161. # Clear the find result
  162. def ClearResult():
  163. txtarea_output.delete(1.0,END)
  164. def ResetAll():
  165.  
  166. # txtarea reset is start from 0 (0th index), entry reset is start from 1.0 (1st line),
  167. # decimal means how many index delete once
  168.  
  169. #Entry reset
  170. tb_Name.delete(0,END)
  171. #TxtArea reset #Txtarea
  172. #txtarea_output.delete(1.0,END)
  173. ClearResult()
  174.  
  175. #Location reset # Option Menu
  176. checked_Location.set(1)
  177. nx.set("~/Desktop")
  178.  
  179. #tb_Location.delete(0,END)
  180.  
  181. #Size Reset
  182. checked_Size.set(0)
  183. tb_Size.delete(0,END)
  184.  
  185. # Depth Reset
  186. checked_MaxDepth.set(0)
  187. checked_MinDepth.set(0)
  188.  
  189. tb_MaxDepth.delete(0,END)
  190. tb_MinDepth.delete(0,END)
  191.  
  192. # Reset type to 'All Type'
  193. FileType.set(0)
  194.  
  195.  
  196.  
  197.  
  198. # Label of the 'Name', this is the file user want to search
  199. lb_Name = Label(root, text="Name",justify=LEFT)
  200. lb_Name.grid(row=0, column=0,padx=0,pady=7)
  201.  
  202. # Entry of the 'Name'
  203. tb_Name = Entry(root,bd=3,width=35)
  204. tb_Name.grid(row=0, column=1, columnspan=2, padx=0,pady=7)
  205.  
  206.  
  207.  
  208. # Option of file Type
  209. # File type radio button (value 1=All 2=File, 3=Dir)
  210.  
  211. FileType=IntVar()
  212. rb_optionA = Radiobutton(root,text="All type",variable=FileType,value=0)
  213. rb_optionB = Radiobutton(root,text="File",variable=FileType,value=1)
  214. rb_optionC = Radiobutton(root,text="Directory",variable=FileType,value=2)
  215. rb_optionA.grid(row=1, column=0)
  216. rb_optionB.grid(row=1, column=1)
  217. rb_optionC.grid(row=1, column=2)
  218.  
  219.  
  220. # row = 2
  221. # Mindepth level
  222.  
  223. # label of 'MinDepth'
  224. lb_MinDepth = Label(root, text="Mindepth")
  225. lb_MinDepth.grid(row=2,column=0)
  226.  
  227. # Entry of 'MinDepth'
  228. tb_MinDepth=Entry(root,bd=2,width=5)
  229. tb_MinDepth.grid(row=2,column=1,columnspan=1,pady=1,sticky=W)
  230.  
  231. # Enable button
  232. checked_MinDepth=IntVar()
  233. cb_MinDepth=Checkbutton(root,variable=checked_MinDepth)
  234. cb_MinDepth.grid(row=2,column=2)
  235.  
  236.  
  237.  
  238. # row = 3
  239. # MaxDeptih levelss
  240.  
  241. # Label of 'MaxDepth'
  242. lb_MaxDepth = Label(root, text="MaxDepth")
  243. lb_MaxDepth.grid(row=3,column=0)
  244.  
  245. # Entry of 'MaxDepth'
  246. tb_MaxDepth=Entry(root,bd=2,width=5)
  247. tb_MaxDepth.grid(row=3,column=1,columnspan=1,pady=1,sticky=W)
  248.  
  249. # Enable button
  250. checked_MaxDepth=IntVar()
  251. cb_MaxDepth=Checkbutton(root,variable=checked_MaxDepth)
  252. cb_MaxDepth.grid(row=3,column=2)
  253.  
  254.  
  255.  
  256. # row = 4
  257. # Option of size
  258.  
  259. #label of size
  260. lb_Size=Label(text="Size (byte), <")
  261. lb_Size.grid(row=4,column=0)
  262.  
  263. # Entry of size
  264. tb_Size=Entry(root,bd=2,width=10)
  265. tb_Size.grid(row=4,column=1,pady=1,sticky=W)
  266.  
  267. # Enable 'Size' button
  268. checked_Size=IntVar()
  269. cb_Size=Checkbutton(root,variable=checked_Size)
  270. cb_Size.grid(row=4,column=2)
  271.  
  272.  
  273. # row = 5
  274. # Custom Location
  275. lb_Location = Label(root, text="Custom Location")
  276. lb_Location.grid(row=5,column=0)
  277.  
  278. # Combobox of location
  279. nx=StringVar(root)
  280. nx.set("~/Desktop")
  281. tb_Location=OptionMenu(root,nx,"~/Dekstop","~/Documents","~/Downloads","~/Pictures","~/Videos","~/Music","~/","/")
  282. tb_Location.config(width=10)
  283. tb_Location.grid(row=5,column=1,columnspan=1,pady=1,sticky=W)
  284.  
  285. # Enable button of custom location
  286. checked_Location=IntVar()
  287. cb_Location=Checkbutton(root,variable=checked_Location)
  288. cb_Location.grid(row=5,column=2)
  289. checked_Location.set(1)
  290.  
  291. # row = 6
  292. # Clear find result button
  293. btn_Reset = Button(root, text="ClearResult",bd=3,command=ClearResult)
  294. btn_Reset.grid(row=6, column=0)
  295.  
  296. # Reset Button
  297. btn_Reset = Button(root, text="Reset",bd=3,command=ResetAll)
  298. btn_Reset.grid(row=6, column=1)
  299.  
  300.  
  301. # Run button
  302. btn_run = Button(root, text="Run!",bd=3,command=thread_RunFindCommand)
  303. btn_run.grid(row=6, column=2)
  304.  
  305. # Process bar
  306. pb=ttk.Progressbar(root,orient="horizontal",length=250,mode="indeterminate")
  307.  
  308. # A label of result "Command Output:"
  309. lb_commandoutput = Label(root, text="Command Output:")
  310. lb_commandoutput.grid(row=8, column=0)
  311.  
  312. # A txtbox to display the result of the find command
  313. txtarea_output = Text(root, height=10, width=50)
  314. txtarea_output.grid(row=9, column=0, columnspan=4,padx=9,pady=12)
  315.  
  316. # Add Scroll bar to the left of the txtarea result box
  317. scr_output = Scrollbar(root,orient='vertical')
  318. scr_output.grid(row=9,column=3,sticky=S+N+E,padx=9,pady=12)
  319. txtarea_output.config(yscrollcommand=scr_output.set)
  320. scr_output.config(command=txtarea_output.yview)
  321.  
  322.  
  323.  
  324.  
  325. # keep the GUI alive
  326. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement