163 lines
6.8 KiB
Python
163 lines
6.8 KiB
Python
from PySide6.QtWidgets import QWidget, QApplication, QFileDialog
|
||
from Ui_获取小闲平台使用数据 import Ui_Form
|
||
from database_tools import *
|
||
import shutil,zipfile
|
||
|
||
class MyWindow(QWidget,Ui_Form):
|
||
def __init__(self):
|
||
super().__init__()
|
||
self.setupUi(self)
|
||
self.bind()
|
||
self.zipfilepath = ""
|
||
self.lineEdit_threshold.setText("0.75")
|
||
self.singlefile = True
|
||
|
||
def bind(self):
|
||
self.pushButton_selectfilepath.clicked.connect(self.getFilePath)
|
||
self.pushButton_selectdirpath.clicked.connect(self.getDirPath)
|
||
self.pushButton_exec.clicked.connect(self.exec)
|
||
|
||
def getFilePath(self):
|
||
pathlist = QFileDialog.getOpenFileName(self,"选择文件",".","zip文件(*.zip);;所有文件(*)")
|
||
self.label_filepath.setText(pathlist[0])
|
||
self.zipfilepath = pathlist[0]
|
||
self.singlefile = True
|
||
date_list = re.findall(r"\((\d{8})\).zip",self.zipfilepath)
|
||
if len(date_list)>0:
|
||
date = date_list[0]
|
||
self.lineEdit_date.setText(date)
|
||
|
||
|
||
|
||
def getDirPath(self):
|
||
dirpath = QFileDialog.getExistingDirectory(self,"选择文件夹")
|
||
self.label_filepath.setText(dirpath)
|
||
self.singlefile = False
|
||
self.lineEdit_date.setText("自适应")
|
||
|
||
|
||
def exec(self):
|
||
if self.singlefile:
|
||
self.execsinglefile()
|
||
else:
|
||
self.execmultifile()
|
||
|
||
def execmultifile(self):
|
||
directory = self.label_filepath.text()
|
||
files = [f for f in os.listdir(directory) if ".zip" in f]
|
||
datedfiles = []
|
||
tempdir = "临时文件/zips"
|
||
statsfilename = "小题分_按学号(数学).xlsx"
|
||
answersheetseekingpath = "../备课组"
|
||
glossoutput = ""
|
||
threshold = float(self.lineEdit_threshold.text())
|
||
for f in files:
|
||
fpath = os.path.join(directory,f)
|
||
if len(re.findall(r"\(\d{8}\)",f)) == 0:
|
||
print(f"文件 {f} 未标注日期.")
|
||
else:
|
||
datedfiles.append(fpath)
|
||
for zipfpath in datedfiles:
|
||
try:
|
||
try:
|
||
shutil.rmtree(tempdir)
|
||
except:
|
||
pass
|
||
makedir(tempdir)
|
||
date = re.findall(r"\((\d{8})\)",zipfpath)[0]
|
||
xiaoxianpid = ParseZipname(zipfpath)
|
||
paperinfo = FindPaper(xiaoxianpid, answersheetseekingpath)
|
||
gradename = paperinfo[1]
|
||
idlist = paperinfo[2]
|
||
zf = zipfile.ZipFile(zipfpath)
|
||
zf.extractall(tempdir) #解压zip文件中的所有内容到tempdir
|
||
statsfilepathlist = FindFile(tempdir,statsfilename)
|
||
validcols,marks = generateColIndexandMarks(statsfilepathlist,statsfilename,paperinfo)
|
||
dfcurrent = pd.read_excel(os.path.join(statsfilepathlist[0],statsfilename))
|
||
correspondence_dict = generateIDtoUsageCorrespondence(idlist,validcols,dfcurrent.iloc[1,validcols])
|
||
output = CalculateUsages(statsfilepathlist,statsfilename,gradename,threshold,marks,correspondence_dict,validcols,date)
|
||
if CheckUsagesValidity(output) == 0:
|
||
glossoutput += output
|
||
else:
|
||
print(f"{zipfpath} 数据有误, 可能需要检查每一题的满分数据")
|
||
self.label_filepath.setText(f"{zipfpath} 数据有误, 可能需要检查每一题的满分数据")
|
||
print(f"文件 {zipfpath} 处理完成")
|
||
except:
|
||
print(f"{zipfpath} 操作中有错误, 需要检查源数据")
|
||
self.label_filepath.setText(f"{zipfpath} 操作中有错误, 需要检查源数据")
|
||
SaveTextFile(glossoutput,"文本文件/metadata.txt")
|
||
os.system("code 文本文件/metadata.txt")
|
||
# print(datedfiles)
|
||
|
||
|
||
|
||
|
||
def execsinglefile(self):
|
||
date = self.lineEdit_date.text()
|
||
threshold = float(self.lineEdit_threshold.text())
|
||
if not len(date.strip()) == 8:
|
||
self.lineEdit_date.setText("日期格式有误")
|
||
elif not threshold >= 0 or not threshold <= 1:
|
||
self.lineEdit_threshold.setText("阈值有误")
|
||
else:
|
||
tempdir = "临时文件/zips"
|
||
statsfilename = "小题分_按学号(数学).xlsx"
|
||
answersheetseekingpath = "../备课组"
|
||
try:
|
||
shutil.rmtree(tempdir)
|
||
except:
|
||
pass
|
||
makedir(tempdir)
|
||
xiaoxianpid = ParseZipname(self.zipfilepath)
|
||
paperinfo = FindPaper(xiaoxianpid, answersheetseekingpath)
|
||
gradename = paperinfo[1]
|
||
idlist = paperinfo[2]
|
||
|
||
zf = zipfile.ZipFile(self.zipfilepath)
|
||
zf.extractall(tempdir) #解压zip文件中的所有内容到tempdir
|
||
|
||
# papertype = CheckPaperType(tempdir,statsfilename)
|
||
statsfilepathlist = FindFile(tempdir,statsfilename)
|
||
validcols,marks = generateColIndexandMarks(statsfilepathlist,statsfilename,paperinfo)
|
||
dfcurrent = pd.read_excel(os.path.join(statsfilepathlist[0],statsfilename))
|
||
correspondence_dict = generateIDtoUsageCorrespondence(idlist,validcols,dfcurrent.iloc[1,validcols])
|
||
output = CalculateUsages(statsfilepathlist,statsfilename,gradename,threshold,marks,correspondence_dict,validcols,date)
|
||
if CheckUsagesValidity(output) == 0:
|
||
if not self.checkBox_appendflag.isChecked():
|
||
SaveTextFile(output,"文本文件/metadata.txt")
|
||
print("数据文件已输出至metadata.txt")
|
||
self.label_filepath.setText("数据文件已输出至metadata.txt")
|
||
os.system("code 文本文件/metadata.txt")
|
||
else:
|
||
AppendTextFile("\n\n"+output,"文本文件/metadata.txt")
|
||
print("数据文件已添加至metadata.txt")
|
||
self.label_filepath.setText("数据文件已添加至metadata.txt")
|
||
os.system("code 文本文件/metadata.txt")
|
||
|
||
|
||
else:
|
||
print("数据有误, 可能需要检查每一题的满分数据")
|
||
self.label_filepath.setText("数据有误, 可能需要检查每一题的满分数据")
|
||
|
||
|
||
|
||
|
||
# def ShowFilePath(self): # 选择文件
|
||
# pathlist = QFileDialog.getOpenFileName(self, "选择文件这是标题", ".", "All Files (*);;py文件(*.py *.pyd))")
|
||
# self.label_FilePath.setText(pathlist[0])
|
||
# def getDir(self): # 选择文件夹
|
||
# dir = QFileDialog.getExistingDirectory(None, "选择文件夹")
|
||
# self.label_FilePath.setText(dir)
|
||
|
||
|
||
|
||
|
||
|
||
|
||
if __name__ == '__main__':
|
||
app = QApplication([])
|
||
windows = MyWindow()
|
||
windows.show()
|
||
app.exec()
|
||
|