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/工具v3/新增基础知识梳理.py

67 lines
2.5 KiB
Python

from PySide6.QtWidgets import QWidget, QApplication, QFileDialog
from Ui_新增基础知识梳理 import Ui_Form
import os
from database_tools_2 import *
class MyWindow(QWidget,Ui_Form):
def __init__(self):
super().__init__()
self.setupUi(self)
self.bind()
def bind(self):
self.pushButton_exec.setDisabled(True)
self.pushButton_selectfilepath.clicked.connect(self.selectfilepath)
self.pushButton_exec.clicked.connect(self.exec)
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 exec(self):
mydb = connect(hostname = db_host, port = db_port, username=db_user, pwd=db_pwd, db = db_database)
mycursor = 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]
currentid = int(max(bnids)[1:])+1
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)
for objid in obj_list:
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
mydb.commit()
mydb.close()
print("已录入完成")
if __name__ == '__main__':
app = QApplication([])
windows = MyWindow()
windows.show()
app.exec()