This repository has been archived on 2024-06-23. You can view files and clone it, but cannot push or open issues or pull requests.
mathdeptv2/工具v4/获取小闲平台使用数据.py

171 lines
7.1 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from PySide6.QtWidgets import QWidget, QApplication, QFileDialog, QTableWidgetItem
from PySide6.QtGui import QColor
from Ui_获取小闲平台使用数据 import Ui_Form
from database_tools_2 import *
import shutil,zipfile
def getFilename(string):
filename = re.findall(r"\d{21}_([\s\S]*?)_高[一二三]_数学",string)[0]
return filename
def getDate(string):
date = re.findall(r"\((\d{8})\).zip",string)
if len(date) > 0:
return date[0]
else:
return ""
class MyWindow_xxdr(QWidget,Ui_Form):
def __init__(self,database_name):
super().__init__()
self.setupUi(self)
self.database_name = database_name
self.bind()
def bind(self):
self.tempdir = "临时文件/zips"
self.statsfilename = "小题分_按学号数学.xlsx"
# self.answersheetseekingpath = "../备课组"
self.lineEdit_threshold.setText("0.75")
self.tableWidget.setColumnWidth(0,200)
self.tableWidget.setColumnWidth(1,280)
self.tableWidget.setColumnWidth(2,80)
self.pushButton_file.clicked.connect(self.getFilePath)
self.pushButton_folder.clicked.connect(self.getDirPath)
self.pushButton_validcheck.clicked.connect(self.checkpathanddate)
self.pushButton_exec.clicked.connect(self.exec)
self.tablevalid = True
def getFilePath(self): #选取文件并放置在tableWidget的第一行
self.tableWidget.setRowCount(1)
pathlist = QFileDialog.getOpenFileName(self,"选择文件",".","zip文件(*.zip);;所有文件(*)")
self.zipfilepath = pathlist[0]
self.tableWidget.setItem(0,0,QTableWidgetItem(pathlist[0]))
# self.singlefile = True
filename = getFilename(pathlist[0])
self.tableWidget.setItem(0,1,QTableWidgetItem(filename))
self.tableWidget.setItem(0,2,QTableWidgetItem(getDate(pathlist[0])))
def setdbname(self,string):
self.database_name = string
# print(self.database_name)
def getDirPath(self): #选取文件夹中的所有符合答题纸特征的.zip文件并放置在tableWidget中
dirpath = QFileDialog.getExistingDirectory(self,"选择文件夹")
self.filelist = [os.path.join(dirpath,f) for f in os.listdir(dirpath) if ".zip" in f and re.findall(r"\d{21}",f)!=[]]
# self.singlefile = False
count = 0
for f in self.filelist:
if count + 1 > self.tableWidget.rowCount():
self.tableWidget.insertRow(self.tableWidget.rowCount())
self.tableWidget.setItem(count,0,QTableWidgetItem(f))
self.tableWidget.setItem(count,1,QTableWidgetItem(getFilename(f)))
self.tableWidget.setItem(count,2,QTableWidgetItem(getDate(f)))
count += 1
def checkpathanddate(self): #检查文件名和日期是否有效, 文件名无效标黄色, 日期无效标红色
if not float(self.lineEdit_threshold.text()) >= 0 or not float(self.lineEdit_threshold.text()) <= 1:
self.lineEdit_threshold.setStyleSheet("background-color: red;")
self.tablevalid = False
self.using_rows = []
self.tableWidget.clearSelection()
for t in range(self.tableWidget.rowCount()):
for i in range(3):
self.tableWidget.item(t,i).setBackground(QColor("transparent"))
for t in range(self.tableWidget.rowCount()):
if re.findall(r"\d{21}",self.tableWidget.item(t,0).text()) == [] or not len(self.tableWidget.item(t,2).text().strip()) == 8:
for i in range(3):
self.tableWidget.item(t,i).setBackground(QColor("yellow"))
else:
self.using_rows.append(t)
for row in range(self.tableWidget.rowCount()):
date = self.tableWidget.item(row,2).text()
if not len(date.strip()) == 8:
self.tableWidget.item(row,2).setBackground(QColor("red"))
def exec(self): #执行操作并将使用数据汇入metadata.txt
self.checkpathanddate()
if len(self.using_rows) >= 2:
SaveTextFile("","./临时文件/metadata.txt")
for row in self.using_rows:
date = self.tableWidget.item(row,2).text()
threshold = float(self.lineEdit_threshold.text())
output = self.generateUsingInfo(zipfilepath=self.tableWidget.item(row,0).text(),threshold=threshold,date=date)
if output == 1 or CheckUsagesValidity(output) != 0:
for i in range(3):
self.tableWidget.item(row,i).setBackground(QColor("red"))
if output == 1:
print(f"{self.tableWidget.item(row,1).text()} 有问题, 未能生成使用数据")
else:
print(f"{self.tableWidget.item(row,1).text()} 满分设置有误, 请检查")
else:
for i in range(3):
self.tableWidget.item(row,i).setBackground(QColor("green"))
print(f"{self.tableWidget.item(row,1).text()} 使用数据生成成功")
if not self.checkBox.isChecked() and len(self.using_rows) == 1:
SaveTextFile(output,"./临时文件/metadata.txt")
else:
AppendTextFile(output,"./临时文件/metadata.txt")
os.system("code 临时文件/metadata.txt")
def generateUsingInfo(self,zipfilepath,threshold,date): #根据zipfilepath文件路径, threshold最低提交比例阈值, date日期产生对应一张答题纸的使用记录
try:
shutil.rmtree(self.tempdir)
except:
pass
makedir(self.tempdir)
mydb = connect(hostname = db_host, port = db_port, username=db_user, pwd=db_pwd, db = self.database_name)
mycursor = mydb.cursor()
try:
xiaoxianpid = ParseZipname(zipfilepath)
paperinfo = FindPaper(xiaoxianpid, mycursor)
gradename = paperinfo[1]
idlist = paperinfo[2]
excludejson = paperinfo[4]
print(paperinfo)
# print(paperinfo)
zf = zipfile.ZipFile(zipfilepath)
zf.extractall(self.tempdir) #解压zip文件中的所有内容到tempdir
# # papertype = CheckPaperType(tempdir,statsfilename)
statsfilepathlist = FindFile(self.tempdir,self.statsfilename)
validcols,marks = generateColIndexandMarks(statsfilepathlist,self.statsfilename,paperinfo)
dfcurrent = pd.read_excel(os.path.join(statsfilepathlist[0],self.statsfilename))
correspondence_dict = generateIDtoUsageCorrespondence(idlist,validcols,dfcurrent.iloc[1,validcols])
output = CalculateUsages(statsfilepathlist,self.statsfilename,gradename,threshold,marks,correspondence_dict,validcols,date,exclude=RefineExclude(excludejson))
mydb.close()
return output #返回由usages开始的使用记录文本
except:
mydb.close()
return 1 #无法生成就返回1
if __name__ == '__main__':
app = QApplication([])
windows = MyWindow_xxdr("tikutest")
windows.show()
app.exec()