98 lines
4.2 KiB
Python
98 lines
4.2 KiB
Python
from PySide6.QtWidgets import QWidget, QApplication, QFileDialog
|
|
from Ui_新增基础知识梳理 import Ui_Form
|
|
import os
|
|
from database_tools_2 import *
|
|
|
|
class MyWindow_tjjc(QWidget,Ui_Form):
|
|
def __init__(self,database_name):
|
|
super().__init__()
|
|
self.database_name = database_name
|
|
self.setupUi(self)
|
|
self.bind()
|
|
|
|
def setdbname(self,string):
|
|
self.database_name = string
|
|
# print(self.database_name)
|
|
def bind(self):
|
|
self.errorflag = True
|
|
self.pushButton_exec.setDisabled(True)
|
|
self.pushButton_selectfilepath.clicked.connect(self.selectfilepath)
|
|
self.pushButton_exec.clicked.connect(self.execstep1)
|
|
self.pushButton_commit.clicked.connect(self.commitstep)
|
|
self.pushButton_commit.setDisabled(True)
|
|
|
|
|
|
def selectfilepath(self):
|
|
self.filepath = QFileDialog.getOpenFileName(self,"选择.tex文件",os.getcwd(),"tex文件(*.tex);;所有文件(*)")[0]
|
|
self.lineEdit_filepath.setText(self.filepath)
|
|
self.pushButton_exec.setEnabled(True)
|
|
def execstep1(self):
|
|
self.mydb = connect(hostname = db_host, port = db_port, username=db_user, pwd=db_pwd, db = self.database_name)
|
|
mycursor = self.mydb.cursor()
|
|
mycursor.execute("SELECT bn_id FROM basic_knowledges;")
|
|
bnids = [ret[0] for ret in mycursor.fetchall()]
|
|
bkdata_raw = ReadTextFile(self.filepath)
|
|
bkdatabody = re.findall(r"\\begin\{enumerate\}([\s\S]*?)\\end\{enumerate\}",bkdata_raw)[0].strip().split("\\item")
|
|
bkdata = [line.strip() for line in bkdatabody if "[" in line and "]" in line]
|
|
print(f"目前数据库中基础知识编号的最大值: {int(max(bnids)[1:])}")
|
|
currentid = int(max(bnids)[1:])+1
|
|
self.errorflag = False
|
|
try:
|
|
for line in bkdata:
|
|
if not line.strip() == "":
|
|
bnid = f"B{str(currentid).zfill(5)}"
|
|
pos = line.index("]")
|
|
head = line[1:pos]
|
|
content = line[pos+1:].strip()
|
|
lesson,objs = head.split("/")
|
|
obj_list = objs.split(",")
|
|
print(f"{bnid} {obj_list} {content}")
|
|
sql = "INSERT INTO basic_knowledges (bn_id,bn_content) VALUE (%s,%s);"
|
|
val = (bnid,content)
|
|
mycursor.execute(sql,val)
|
|
print(f"已导入基础知识 编号: {bnid},\n 内容: {content}.")
|
|
for objid in obj_list:
|
|
sql = "SELECT * FROM lessonobj WHERE objid = %s AND NOT obsolete;"
|
|
val = (objid,)
|
|
mycursor.execute(sql,val)
|
|
ret =mycursor.fetchall()
|
|
if ret == []:
|
|
print(f"!!!目标编号: {objid} 有误")
|
|
raise ValueError
|
|
else:
|
|
print(f"对应目标编号 {objid}, 目标内容 {ret[0][1]}")
|
|
sql = "INSERT INTO bn_obj_corresp (bn_id,obj_id) VALUE (%s,%s);"
|
|
val = (bnid,objid)
|
|
mycursor.execute(sql,val)
|
|
sql = "INSERT INTO logs (DATE,TIME,username,action,db_content) VALUE (%s,%s,%s,%s,%s);"
|
|
val = (GetDate(),GetTime(),get_git_username(),"新增基础",f"{bnid} {obj_list} {content}")
|
|
mycursor.execute(sql,val)
|
|
currentid += 1
|
|
print("---------- ---------- ----------")
|
|
self.pushButton_commit.setEnabled(True)
|
|
except Exception as e:
|
|
print(f"错误: {type(e).__name__}")
|
|
print(f"内容为 {line} 的输入有误.")
|
|
self.errorflag = True
|
|
self.mydb.rollback()
|
|
print(f"数据库 {self.database_name} 已回滚")
|
|
|
|
def commitstep(self):
|
|
if self.errorflag == False:
|
|
self.mydb.commit()
|
|
self.mydb.close()
|
|
print(f"提交至数据库 {self.database_name} 已完成.")
|
|
self.pushButton_commit.setDisabled(True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
app = QApplication([])
|
|
windows = MyWindow()
|
|
windows.show()
|
|
app.exec()
|
|
|