OpenFiles.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. from tkinter import filedialog
  2. from pathlib import Path
  3. import os
  4. import shutil
  5. def uploadTCLFile(self):
  6. files = filedialog.askopenfilenames(
  7. initialdir=os.getcwd(),
  8. title="Choose TCL files",
  9. filetypes=(("TCL Files", "*.tcl"),)
  10. )
  11. try:
  12. for tf in files:
  13. if tf == "":
  14. self.tclUploadVar.set("")
  15. return
  16. tf = open(tf) # or tf = open(tf, 'r')
  17. # copy file over to tcl locations
  18. shutil.copy(tf.name, os.getcwd() + os.path.join("\\lab_tcl"))
  19. tf.close()
  20. except:
  21. self.tclStatusLabel.configure(style="Red.TLabel")
  22. self.tclUploadVar.set("An Error Occurred")
  23. return
  24. if len(files) == 1:
  25. # Generate an animation for the result
  26. self.tclStatusLabel.configure(style="Green.TLabel")
  27. self.tclUploadVar.set("Copied " + files[0][files[0].rfind("/") + 1:len(files[0])])
  28. elif len(files) > 1:
  29. self.tclStatusLabel.configure(style="Green.TLabel")
  30. self.tclUploadVar.set("Copied " + str(len(files)) + " Files")
  31. elif len(files) == 0:
  32. self.tclStatusLabel.configure(style="TLabel")
  33. self.tclUploadVar.set("")
  34. def uploadVHDFile(self):
  35. files = filedialog.askopenfilenames(
  36. initialdir=os.getcwd(),
  37. title="Choose VHD files",
  38. filetypes=(("VHD Files", "*.vhd"),)
  39. )
  40. try:
  41. for tf in files:
  42. if tf == "":
  43. self.tbUploadVar.set("")
  44. return
  45. tf = open(tf) # or tf = open(tf, 'r')
  46. filename = tf.name[tf.name.rfind("/") + 1:len(tf.name)]
  47. # copy file over to tcl locations
  48. if filename.endswith("true_testbench.vhd"):
  49. shutil.copy(tf.name, os.getcwd() + os.path.join("\\testbenches"))
  50. else:
  51. shutil.copy(tf.name, os.getcwd() + os.path.join(f'''\\testbenches\\{filename[0:len(filename)-4]}_true_testbench.vhd''' ))
  52. tf.close()
  53. except:
  54. self.tbUploadLabel.configure(style="Red.TLabel")
  55. self.tbUploadVar.set("An Error Occurred")
  56. return
  57. if len(files) == 1:
  58. # Generate an animation for the result
  59. self.tbUploadLabel.configure(style="Green.TLabel")
  60. self.tbUploadVar.set("Copied " + files[0][files[0].rfind("/") + 1:len(files[0])])
  61. elif len(files) > 1:
  62. self.tbUploadLabel.configure(style="Green.TLabel")
  63. self.tbUploadVar.set("Copied " + str(len(files)) + " Files")
  64. elif len(files) == 0:
  65. self.tbUploadLabel.configure(style="TLabel")
  66. self.tbUploadVar.set("")
  67. def uploadTextFile(self):
  68. files = filedialog.askopenfilenames(
  69. initialdir=os.getcwd(),
  70. title="Choose Text files",
  71. filetypes=(("Text Files", "*.txt"),)
  72. )
  73. try:
  74. for tf in files:
  75. if tf == "":
  76. self.textUploadVar.set("")
  77. return
  78. tf = open(tf) # or tf = open(tf, 'r')
  79. # copy file over to tcl locations
  80. shutil.copy(tf.name, os.getcwd() + os.path.join("\\studentlists"))
  81. tf.close()
  82. except:
  83. self.studentListLabel.configure(style="Red.TLabel")
  84. self.textUploadVar.set("An Error Occurred")
  85. return
  86. if len(files) == 1:
  87. # Generate an animation for the result
  88. self.studentListLabel.configure(style="Green.TLabel")
  89. self.textUploadVar.set("Copied " + files[0][files[0].rfind("/") + 1:len(files[0])])
  90. elif len(files) > 1:
  91. self.studentListLabel.configure(style="Green.TLabel")
  92. self.textUploadVar.set("Copied " + str(len(files)) + " Files")
  93. elif len(files) == 0:
  94. self.studentListLabel.configure(style="TLabel")
  95. self.textUploadVar.set("")
  96. def uploadZipFile(self):
  97. tf = filedialog.askopenfilename(
  98. initialdir=str(Path.home() / "Downloads"),
  99. title="Choose Zip file",
  100. filetypes=(("Zip Files", "*.zip"),)
  101. )
  102. if tf == "":
  103. self.zipVar.set("")
  104. self.zipLabel['text'] = ""
  105. return
  106. tf = open(tf) # or tf = open(tf, 'r')
  107. self.zipVar.set(tf.name)
  108. self.zipLabel['text'] = tf.name[tf.name.rfind("/") + 1:len(tf.name)]
  109. tf.close()
  110. def openTCLFile(self):
  111. tf = filedialog.askopenfilename(
  112. initialdir=os.getcwd() + os.path.join("\\lab_tcl"),
  113. title="Choose TCL file",
  114. filetypes=(("TCL Files", "*.tcl"),)
  115. )
  116. if tf == "":
  117. self.tclVar.set("")
  118. self.chooseTclLabel['text'] = ""
  119. return
  120. tf = open(tf)
  121. self.tclVar.set(tf.name)
  122. self.chooseTclLabel['text'] = tf.name[tf.name.rfind("/") + 1:len(tf.name)]
  123. tf.close()
  124. def openStudentListFile(self):
  125. tf = filedialog.askopenfilename(
  126. initialdir=os.getcwd() + os.path.join("\\studentlists"),
  127. title="Choose Student List file",
  128. filetypes=(("Text Files", "*.txt"),)
  129. )
  130. if tf == "":
  131. self.studentListVar.set("")
  132. self.chooseStudentsLabel['text'] = ""
  133. return
  134. tf = open(tf) # or tf = open(tf, 'r')
  135. self.studentListVar.set(tf.name)
  136. self.chooseStudentsLabel['text'] = tf.name[tf.name.rfind("/") + 1:len(tf.name)]
  137. tf.close()
  138. def chooseVHDFiles(self):
  139. files = filedialog.askopenfilenames(
  140. initialdir=os.getcwd() + '\\testbenches\\',
  141. title="Choose VHD files",
  142. filetypes=(("VHD Files", "*.vhd"),)
  143. )
  144. labelText = ""
  145. self.selectedTestbenches = []
  146. try:
  147. for tf in files:
  148. if tf == "":
  149. self.tbUploadVar.set("")
  150. return
  151. tf = open(tf) # or tf = open(tf, 'r')
  152. self.selectedTestbenches.append(tf.name)
  153. labelText += tf.name[tf.name.rfind("/") + 1:len(tf.name)] + '\n'
  154. tf.close()
  155. except:
  156. self.tbSelectLabel['text'] = 'An Error Occured'
  157. return
  158. self.tbSelectLabel['text'] = labelText