main.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. import hashlib
  2. import sys
  3. from tkinter import *
  4. import helpers
  5. root = Tk()
  6. titleLabel = StringVar()
  7. text = StringVar()
  8. filesInfo = None
  9. csvFiles = None
  10. state = None
  11. acceptedDataCSV = None
  12. rejectedDataCSV = None
  13. nextCount = 0
  14. def findDuplicate (hashDict, title):
  15. if title in hashDict:
  16. return True
  17. else:
  18. return False
  19. def acceptPaper (file, csvRow, index):
  20. global state
  21. global acceptedDataCSV
  22. fileFilter = helpers.getFilter(file)
  23. data = fileFilter.getCSVData(csvRow)
  24. digest = hashlib.sha256(str.encode(fileFilter.getTitle(csvRow))).hexdigest()
  25. duplicate = findDuplicate(rejectedDataCSV, digest) or findDuplicate(acceptedDataCSV, digest)
  26. if duplicate:
  27. state['duplicate'] += 1
  28. else:
  29. data.append("{0} - {1}".format(file['path'],index))
  30. acceptedDataCSV[digest] = data
  31. state['accepted'] += 1
  32. return
  33. def rejectPaper (file, csvRow, index):
  34. global rejectedDataCSV
  35. global state
  36. fileFilter = helpers.getFilter(file)
  37. data = fileFilter.getCSVData(csvRow)
  38. digest = hashlib.sha256(str.encode(fileFilter.getTitle(csvRow))).hexdigest()
  39. duplicate = findDuplicate(rejectedDataCSV, digest) or findDuplicate(acceptedDataCSV, digest)
  40. if duplicate:
  41. state['duplicate'] += 1
  42. else:
  43. data.append("{0} - {1}".format(file['path'],index))
  44. rejectedDataCSV[digest] = data
  45. state['rejected'] += 1
  46. return
  47. def rejectButtonHandler (*arg):
  48. global state
  49. global csvFiles
  50. index = state['row_index']
  51. fileIndex = state['file_index']
  52. fileTuple = csvFiles[fileIndex]
  53. rejectPaper(fileTuple[1], fileTuple[0][index], index)
  54. nextPaper()
  55. return
  56. def acceptButtonHandler (*arg):
  57. global state
  58. global csvFiles
  59. index = state['row_index']
  60. fileIndex = state['file_index']
  61. fileTuple = csvFiles[fileIndex]
  62. acceptPaper(fileTuple[1], fileTuple[0][index], index)
  63. nextPaper()
  64. return
  65. def updateAndClear (textField, *args):
  66. textField.config(state=NORMAL)
  67. textField.delete('1.0', END)
  68. textField.insert(INSERT, text.get())
  69. textField.tag_add("center", '1.0', END)
  70. textField.config(state=DISABLED)
  71. def loadFiles ():
  72. global filesInfo
  73. global csvFiles
  74. global state
  75. global root
  76. global acceptedDataCSV
  77. global rejectedDataCSV
  78. filesInfo = helpers.loadConfigFile()
  79. if len(filesInfo) == 0:
  80. sys.exit("csvconfig.txt not found. You need this file to run the program")
  81. state = helpers.loadState(root)
  82. acceptedDataCSV = helpers.restoreAcceptedCSV() #load default file, if present append else create new one
  83. rejectedDataCSV = helpers.restoreRejectedCSV()
  84. if len(acceptedDataCSV) == 0:
  85. acceptedDataCSV['header'] = helpers.FINAL_CSV_HEADERS
  86. if len(rejectedDataCSV) == 0:
  87. rejectedDataCSV['header'] = helpers.FINAL_CSV_HEADERS
  88. csvFiles = [(list(f[0]), f[1]) for f in helpers.loadFiles(filesInfo)]
  89. def setPaperFromState ():
  90. global titleLabel
  91. global text
  92. fileIndex = state['file_index']
  93. if fileIndex >= len(filesInfo):
  94. return #finished processing
  95. fileList = csvFiles[fileIndex][0]
  96. index = state['row_index']
  97. if index >= len(fileList):
  98. state['file_index'] += 1
  99. state['row_index'] = 0
  100. setPaperFromState()
  101. else:
  102. file = fileList[index]
  103. fileFilter = helpers.getFilter(csvFiles[fileIndex][1])
  104. title = fileFilter.getTitle(file)
  105. abstract = fileFilter.getAbstract(file)
  106. titleLabel.set(title)
  107. text.set(abstract)
  108. def nextPaper ():
  109. global titleLabel
  110. global text
  111. global root
  112. global nextCount
  113. fileIndex = state['file_index']
  114. if fileIndex >= len(filesInfo):
  115. helpers.removeStateFile()
  116. helpers.writeFinalLog(state)
  117. helpers.saveAcceptedCSV(acceptedDataCSV)
  118. helpers.saveRejectedCSV(rejectedDataCSV)
  119. root.destroy()#acabou, fecha essa porra
  120. return
  121. fileList = csvFiles[fileIndex][0]
  122. index = state['row_index'] = state['row_index'] + 1
  123. if index >= len(fileList):
  124. state['file_index'] += 1
  125. state['row_index'] = -1
  126. nextPaper()
  127. else:
  128. nextCount += 1
  129. if nextCount%10 == 0:
  130. saveCurrentData()
  131. file = fileList[index]
  132. fileFilter = helpers.getFilter(csvFiles[fileIndex][1])
  133. title = fileFilter.getTitle(file)
  134. abstract = fileFilter.getAbstract(file)
  135. titleLabel.set(title)
  136. text.set(abstract)
  137. def onClosing ():
  138. global root
  139. saveCurrentData()
  140. root.destroy()
  141. def saveCurrentData ():
  142. global state
  143. global acceptedDataCSV
  144. global rejectedDataCSV
  145. helpers.saveState(state)
  146. helpers.saveAcceptedCSV(acceptedDataCSV)
  147. helpers.saveRejectedCSV(rejectedDataCSV)
  148. def updateDup ():
  149. global acceptedDataCSV
  150. global rejectedDataCSV
  151. global state
  152. tempA = {}
  153. tempR = {}
  154. dataR = list(rejectedDataCSV.values())
  155. for i in range(1,len(dataR)):
  156. digest = hashlib.sha256(str.encode(dataR[i][0].lower())).hexdigest()
  157. if digest in tempR:
  158. print("Found duplicate in rejected: {0}".format(dataR[i][0]))
  159. state['duplicate'] += 1
  160. else:
  161. tempR[digest] = dataR[i]
  162. dataA = list(acceptedDataCSV.values())
  163. for i in range(1, len(dataA)):
  164. digest = hashlib.sha256(str.encode(dataA[i][0].lower())).hexdigest()
  165. if digest in tempA or digest in tempR:
  166. print("Found duplicate in accepted: {0}".format(dataA[i][0]))
  167. state['duplicate'] += 1
  168. else:
  169. tempA[digest] = dataA[i]
  170. state['accepted'] = len(tempA)
  171. state['rejected'] = len(tempR)
  172. tempA['header'] = dataA[0]
  173. tempR['header'] = dataR[0]
  174. acceptedDataCSV = tempA
  175. rejectedDataCSV = tempR
  176. saveCurrentData()
  177. def main ():
  178. global root
  179. root.title('Reviewer')
  180. root.geometry('800x600')
  181. topFrame = Frame(root)
  182. bottomFrame = Frame(root)
  183. bottomFrame.pack(side=BOTTOM)
  184. topFrame.pack(side=TOP, fill=BOTH, expand=True)
  185. label = Label(root, textvariable=titleLabel, relief=RAISED)
  186. label.pack(in_=topFrame, fill=BOTH, side=TOP)
  187. textField = Text(root)
  188. textField.tag_configure("center", wrap=WORD)
  189. textField.pack(in_=topFrame, fill=BOTH, side=TOP)
  190. accept = Button(root, text ="Accept Paper", command = acceptButtonHandler)
  191. root.bind('<Right>', lambda e: acceptButtonHandler())
  192. accept.pack(in_=bottomFrame, side=LEFT)
  193. reject = Button(root, text ="Reject Paper", command = rejectButtonHandler)
  194. root.bind('<Down>', lambda e: rejectButtonHandler())
  195. reject.pack(in_=bottomFrame, side=LEFT)
  196. textField.insert(INSERT, text.get())
  197. text.trace('w', lambda *arg: updateAndClear(textField))
  198. textField.config(state=DISABLED)
  199. root.protocol("WM_DELETE_WINDOW", onClosing)
  200. setPaperFromState()
  201. #updateDup()
  202. print("Duplicate count {0}".format(state['duplicate']))
  203. print("Accepted count {0}".format(state['accepted']))
  204. print("Rejected count {0}".format(state['rejected']))
  205. root.mainloop()
  206. if __name__ == '__main__':
  207. loadFiles()
  208. main()