Guest User

Untitled

a guest
Nov 21st, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.86 KB | None | 0 0
  1. import csv
  2. import sqlite3
  3. from tkinter import *
  4. from tkinter import filedialog
  5.  
  6. """Tool to compare two reports and provide specific information from matching lines"""
  7.  
  8. class MatchTool:
  9. UNPLACED_RSL_TEXT = [
  10. "Copy Required Report",
  11. "Ad Copy Status Report",
  12. "Unplaced Spots",
  13. "Required Spots",
  14. ]
  15.  
  16. def __init__(self, master):
  17. self.master = master
  18. master.geometry("400x300")
  19. master.title("Discrepancy Match Tool")
  20.  
  21. self.top_frame = Frame(master)
  22. self.bottom_frame = Frame(master, width=400)
  23.  
  24. self.novar_button_var = IntVar()
  25. self.novar_button_var.set(0)
  26. self.novar_button = Checkbutton(self.top_frame, variable=self.novar_button_var, command=self.enableNovar)
  27.  
  28. self.eclipse_button_var = IntVar()
  29. self.eclipse_button_var.set(0)
  30. self.eclipse_button = Checkbutton(self.top_frame, variable=self.eclipse_button_var, command=self.enableEclipse)
  31.  
  32. self.missing_button_var = IntVar()
  33. self.missing_button_var.set(0)
  34. self.missing_button = Checkbutton(self.top_frame, state=DISABLED, variable=self.missing_button_var, command=self.missingCopy)
  35.  
  36. self.unplaced_button_var = IntVar()
  37. self.unplaced_button_var.set(0)
  38. self.unplaced_button = Checkbutton(self.top_frame, state=DISABLED, variable=self.unplaced_button_var, command=self.unplacedRSL)
  39.  
  40. self.novar_label = Label(self.top_frame, text="Novar")
  41. self.eclipse_label = Label(self.top_frame, text="Eclipse/XG")
  42. self.missing_label = Label(self.top_frame, text="Missing Copy")
  43.  
  44. self.unplaced_label_text = StringVar()
  45. self.unplaced_label_text.set("Unplaced or Required Spots")
  46. self.unplaced_label = Label(self.top_frame, textvariable=self.unplaced_label_text, width=22, anchor=constants.W)
  47.  
  48. self.load_discrep = Button(self.bottom_frame, text="Load Discrepancy Report", width=25, command=self.loadDiscrep)
  49. self.load_discrep_file_name_text = StringVar()
  50. self.load_discrep_file_name = Label(self.bottom_frame, textvariable=self.load_discrep_file_name_text)
  51.  
  52. self.submit = Button(self.bottom_frame, text="Submit", command=self.submit)
  53.  
  54. self.load_unplaced_text = StringVar()
  55. self.load_unplaced_text.set("Load Report")
  56. self.load_unplaced = Button(self.bottom_frame, textvariable=self.load_unplaced_text, width=25, command=self.loadReports)
  57.  
  58. self.load_unplaced_file_name_text = StringVar()
  59. self.load_unplaced_file_name = Label(self.bottom_frame, textvariable=self.load_unplaced_file_name_text)
  60.  
  61.  
  62. #Layout
  63. self.top_frame.grid()
  64. self.bottom_frame.grid(row=1)
  65.  
  66. self.novar_button.grid()
  67. self.eclipse_button.grid(row=1)
  68. self.missing_button.grid(row=2)
  69. self.unplaced_button.grid(row=3)
  70.  
  71. self.novar_label.grid(row=0, column=1, sticky=W)
  72. self.eclipse_label.grid(row=1, column=1, sticky=W)
  73. self.missing_label.grid(row=2, column=1, sticky=W)
  74. self.unplaced_label.grid(row=3, column=1, sticky=W)
  75.  
  76. self.load_discrep.grid(row=0, pady=3, ipadx=5)
  77. self.load_discrep_file_name.grid(row=1, pady=3, ipadx=5)
  78. self.load_unplaced.grid(row=2, pady=3, ipadx=5)
  79. self.load_unplaced_file_name.grid(row=3, pady=3, ipadx=5)
  80.  
  81. #Functions
  82.  
  83. def enableNovar(self):
  84. """Activates the Missing Copy and Unplaced Spots checkboxes, and disables the Novar checkbox"""
  85. if self.novar_button_var.get() == 1:
  86. self.eclipse_button["state"] = DISABLED
  87. self.missing_button["state"] = ACTIVE
  88. self.unplaced_button["state"] = ACTIVE
  89. self.unplaced_label_text.set(self.UNPLACED_RSL_TEXT[3])
  90. else:
  91. self.eclipse_button["state"] = ACTIVE
  92. self.missing_button["state"] = DISABLED
  93. self.unplaced_button["state"] = DISABLED
  94. self.unplaced_label_text.set("Unplaced or Required Spots")
  95.  
  96. def enableEclipse(self):
  97. """Activates the Missing Copy and Required Spots checkboxes, and disables the Eclipse checkbox"""
  98. if self.eclipse_button_var.get() == 1:
  99. self.novar_button["state"] = DISABLED
  100. self.missing_button["state"] = ACTIVE
  101. self.unplaced_button["state"] = ACTIVE
  102. self.unplaced_label_text.set(self.UNPLACED_RSL_TEXT[2])
  103. else:
  104. self.novar_button["state"] = ACTIVE
  105. self.missing_button["state"] = DISABLED
  106. self.unplaced_button["state"] = DISABLED
  107. self.unplaced_label_text.set("Unplaced or Required Spots")
  108.  
  109. def missingCopy(self):
  110. """Changes the value of missing_button_var to 1, changes text of unplaced_text, shows Submit button"""
  111. if self.missing_button_var.get() == 1:
  112. self.unplaced_button["state"] = DISABLED
  113. self.submit.grid(row=4, pady=5)
  114. if self.novar_button_var.get() == 1:
  115. self.load_unplaced_text.set("Load " + self.UNPLACED_RSL_TEXT[1])
  116. elif self.eclipse_button_var.get() == 1:
  117. self.load_unplaced_text.set("Load " + self.UNPLACED_RSL_TEXT[0])
  118. else:
  119. self.unplaced_button["state"] = ACTIVE
  120. self.load_unplaced_text.set("Load Report")
  121. self.submit.grid_forget()
  122.  
  123. def unplacedRSL(self):
  124. """changes the value of unplaced_button_var to 1, changes text of unplaced_text, shows Submit button"""
  125. if self.unplaced_button_var.get() == 1:
  126. self.missing_button["state"] = DISABLED
  127. self.submit.grid(row=4, pady=5)
  128. if self.novar_button_var.get() == 1:
  129. self.load_unplaced_text.set("Load " + self.UNPLACED_RSL_TEXT[3])
  130. elif self.eclipse_button_var.get() == 1:
  131. self.load_unplaced_text.set("Load " + self.UNPLACED_RSL_TEXT[2])
  132. else:
  133. self.missing_button["state"] = ACTIVE
  134. self.load_unplaced_text.set("Load Report")
  135. self.submit.grid_forget()
  136.  
  137. def unplacedEdit(self, loaded_file):
  138. """Opens the CSV file and edits the date and time for the Unplaced Spots report"""
  139. with open(loaded_file) as csv_file:
  140. unplaced_reader = csv.reader(csv_file, delimiter=',')
  141. unplaced_list = [row for row in unplaced_reader]
  142. unplaced_list.pop(0)
  143. unplaced_list[0].extend(['Date', 'Time'])
  144. for i in range(1, len(unplaced_list)):
  145. date_time = unplaced_list[i][1].split(' ')
  146. unplaced_list[i].append(date_time[0])
  147. time_of_day = int(date_time[1][:date_time[1].index(":")])
  148. if time_of_day < 13:
  149. date_time[1] = date_time[1] + " AM"
  150. else:
  151. time_of_day = time_of_day - 12
  152. date_time[1] = str(time_of_day) + date_time[1][date_time[1].index(":"):] + " PM"
  153. unplaced_list[i].append(date_time[1])
  154. return unplaced_list
  155.  
  156. def rslEdit(self, loaded_file):
  157. """Edits the RSL report's Date and Time"""
  158. with open(loaded_file) as csv_file:
  159. rsl_reader = csv.reader(csv_file, delimiter=',')
  160. rsl_list = [row for row in rsl_reader]
  161. rsl_list[0].extend(['Date', 'Time'])
  162. for i in range(1, len(rsl_list)):
  163. date = rsl_list[i][16]
  164. date = date[:date.index("-")]
  165. new_date = date.split('/') #add 20 to the beginning of the year
  166. new_date[2] = "20" + new_date[2]
  167. date = new_date[0] + '/' + new_date[1] + '/' + new_date[2]
  168. time = rsl_list[i][17]
  169. time = time[:time.index("-")]
  170. rsl_list[i].append(date)
  171. rsl_list[i].append(time)
  172. for x in range(1, len(rsl_list)):
  173. digits = rsl_list[x][31]
  174. digits = int(digits[:digits.index(":")])
  175. if digits < 10:
  176. rsl_list[x][31] = rsl_list[x][31][1:] + " AM"
  177. elif digits < 13:
  178. rsl_list[x][31] = rsl_list[x][31] + " AM"
  179. else:
  180. digits = digits - 12
  181. rsl_list[x][31] = str(digits) + rsl_list[x][31][rsl_list[x][31].index(":"):] + " PM"
  182. return rsl_list
  183.  
  184. def copyRequiredEdit(self, loaded_file):
  185. """Removes the first row from the Copy Required Report"""
  186. with open(loaded_file) as csv_file:
  187. cr_reader = csv.reader(csv_file, delimiter=',')
  188. cr_list = [row for row in cr_reader]
  189. cr_list.pop(0)
  190. return cr_list
  191.  
  192. def discrepEdit(self, loaded_file):
  193. """Splits up the contract ID's into a list"""
  194. with open(loaded_file) as csv_file:
  195. discrep_reader = csv.reader(csv_file, delimiter=',')
  196. discrep_list = [row for row in discrep_reader]
  197. for i in range(1, len(discrep_list)):
  198. discrep_list[i][11] = discrep_list[i][11].split(';')
  199. return discrep_list
  200.  
  201. def adCopyStatusEdit(self, loaded_file):
  202. """Changes the AdCopyStatus report to a list"""
  203. with open(loaded_file) as csv_file:
  204. adCopy_reader = csv.reader(csv_file, delimiter=',')
  205. adCopy_list = [row for row in adCopy_reader]
  206. return adCopy_list
  207.  
  208. def copyRequired_DiscrepDB(self, copy_req, discrep_rep):
  209. """Compares the Copy Required report to the Discrepancy Report to find matches"""
  210. matches = [["Client Name", "Client ID", "ContractID", "Event", "Episode", "Date", "Time"]]
  211. for row in self.final_report1:
  212. for node in self.final_discrep1:
  213. if row[3] in node[11]:
  214. matches.append([node[10], node[9], row[2], row[3], row[4], row[5]])
  215. return matches
  216.  
  217. def loadDiscrep(self):
  218. """Opens file directory for user to load report in xls format"""
  219. discrepReport = filedialog.askopenfilename(
  220. filetypes=[("CSV File", "*.csv"), ("All Files", "*.*")]
  221. )
  222. if not discrepReport:
  223. return
  224. else:
  225. self.load_discrep_file_name_text.set("Discrepancy Report loaded successfully")
  226. final_discrep = self.discrepEdit(discrepReport)
  227. return final_discrep
  228.  
  229. def loadReports(self):
  230. """Opens file directory for user to load file, file type depends on prior selections"""
  231. #Copy Required (Eclipse/Missing Copy)
  232. if self.eclipse_button_var.get() == 1 and self.missing_button_var.get() == 1:
  233. copyRequired = filedialog.askopenfilename(
  234. filetypes=[("CSV File", "*.csv"), ("All Files", "*.*")]
  235. )
  236. if not copyRequired:
  237. return
  238. else:
  239. self.load_unplaced_file_name_text.set("Copy Required loaded successfully")
  240. final_report = self.copyRequiredEdit(copyRequired)
  241. return final_report
  242. #AdCopyStatus (Novar/Missing Copy)
  243. elif self.novar_button_var.get() == 1 and self.missing_button_var.get() == 1:
  244. adCopyStatus = filedialog.askopenfilename(
  245. filetypes=[("CSV File", "*.csv"), ("All Files", "*.*")]
  246. )
  247. if not adCopyStatus:
  248. return
  249. else:
  250. self.load_unplaced_file_name_text.set("AdCopyStatus Report loaded successfully")
  251. final_report = self.adCopyStatusEdit(adCopyStatus)
  252. return final_report
  253. #Unplaced Spots (Eclipse/Unplaced)
  254. elif self.eclipse_button_var.get() == 1 and self.unplaced_button_var.get() == 1:
  255. unplacedSpots = filedialog.askopenfilename(
  256. filetypes=[("CSV File", "*.csv"), ("All Files", "*.*")]
  257. )
  258. if not unplacedSpots:
  259. return
  260. else:
  261. self.load_unplaced_file_name_text.set("Unplaced Spots Report loaded successfully")
  262. final_report = self.unplacedEdit(unplacedSpots)
  263. return final_report
  264. #RSL (Novar/Unplaced)
  265. elif self.novar_button_var.get() == 1 and self.unplaced_button_var.get() == 1:
  266. requiredSpots = filedialog.askopenfilename(
  267. filetypes=[("CSV File", "*.csv"), ("All Files", "*.*")]
  268. )
  269. if not requiredSpots:
  270. return
  271. else:
  272. self.load_unplaced_file_name_text.set("Required Spots loaded successfully")
  273. final_report = self.rslEdit(requiredSpots)
  274. return final_report
  275.  
  276. def submit(self):
  277. #final_report = self.loadReports()
  278. #final_discrep = self.loadDiscrep()
  279. matches = self.copyRequired_DiscrepDB()
  280. print(matches)
  281.  
  282.  
  283. # Add functionality for the Submit button: finds the matches between the two db's opened up and returns them as CSV
  284. # How can I write back to a CSV?
  285. # Format the tool better
  286. #Remove checks if button gets disabled
  287.  
  288. root = Tk()
  289. interface = MatchTool(root)
  290. root.mainloop()
Add Comment
Please, Sign In to add comment