convert domainCheck to regular directory
This commit is contained in:
284
domainCheck/app/ui/sensitive_words.py
Normal file
284
domainCheck/app/ui/sensitive_words.py
Normal file
@@ -0,0 +1,284 @@
|
||||
# -*- coding: UTF-8 -*-
|
||||
'''
|
||||
@Project :domainScanDemo
|
||||
@File :sensitive_words.py
|
||||
@IDE :PyCharm
|
||||
@Author :梦伴
|
||||
@Date :2026/4/8 23:49
|
||||
@explain : 敏感词配置界面
|
||||
'''
|
||||
|
||||
from PySide6.QtWidgets import QWidget, QVBoxLayout, QHBoxLayout, QPushButton, QLabel, QTextEdit, QFileDialog
|
||||
from PySide6.QtCore import Qt, QThread, Signal
|
||||
from loguru import logger
|
||||
from app.utils.database import Database
|
||||
|
||||
|
||||
class SaveWordsThread(QThread):
|
||||
"""
|
||||
保存敏感词线程
|
||||
"""
|
||||
finished = Signal(bool, str, int)
|
||||
|
||||
def __init__(self, words):
|
||||
super().__init__()
|
||||
self.words = words
|
||||
|
||||
def run(self):
|
||||
try:
|
||||
db = Database()
|
||||
|
||||
# 先清空现有敏感词
|
||||
db.execute("DELETE FROM sensitive_words")
|
||||
|
||||
# 批量添加敏感词
|
||||
word_tuples = [(word, 'default', 1) for word in self.words]
|
||||
if word_tuples:
|
||||
db.batch_add_sensitive_words(word_tuples)
|
||||
|
||||
db.close()
|
||||
self.finished.emit(True, "成功保存敏感词", len(self.words))
|
||||
except Exception as e:
|
||||
self.finished.emit(False, str(e), 0)
|
||||
|
||||
|
||||
class LoadWordsThread(QThread):
|
||||
"""
|
||||
加载敏感词线程
|
||||
"""
|
||||
finished = Signal(bool, list, str)
|
||||
|
||||
def run(self):
|
||||
try:
|
||||
db = Database()
|
||||
sensitive_words = db.get_sensitive_words()
|
||||
words = [word['word'] for word in sensitive_words]
|
||||
db.close()
|
||||
self.finished.emit(True, words, f"成功加载 {len(words)} 个敏感词")
|
||||
except Exception as e:
|
||||
self.finished.emit(False, [], str(e))
|
||||
|
||||
|
||||
class SensitiveWordsWidget(QWidget):
|
||||
"""
|
||||
敏感词配置界面
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
"""
|
||||
初始化敏感词配置界面
|
||||
"""
|
||||
super().__init__()
|
||||
|
||||
# 创建布局
|
||||
layout = QVBoxLayout(self)
|
||||
|
||||
|
||||
|
||||
# 创建文本编辑框
|
||||
self.text_edit = QTextEdit()
|
||||
self.text_edit.setPlaceholderText("请输入敏感词,一行一个")
|
||||
self.text_edit.setStyleSheet("""
|
||||
QTextEdit {
|
||||
font-size: 14px;
|
||||
padding: 10px;
|
||||
border: 1px solid #ddd;
|
||||
border-radius: 4px;
|
||||
background-color: #f9f9f9;
|
||||
min-height: 300px;
|
||||
}
|
||||
""")
|
||||
layout.addWidget(self.text_edit)
|
||||
|
||||
# 创建按钮布局
|
||||
button_layout = QHBoxLayout()
|
||||
|
||||
# 导入按钮
|
||||
self.import_btn = QPushButton("导入")
|
||||
self.import_btn.clicked.connect(self.import_words)
|
||||
self.import_btn.setStyleSheet("""
|
||||
QPushButton {
|
||||
font-size: 14px;
|
||||
padding: 8px 16px;
|
||||
background-color: #2196F3;
|
||||
color: white;
|
||||
border: none;
|
||||
border-radius: 4px;
|
||||
}
|
||||
QPushButton:hover {
|
||||
background-color: #0b7dda;
|
||||
}
|
||||
""")
|
||||
button_layout.addWidget(self.import_btn)
|
||||
|
||||
# 导出按钮
|
||||
self.export_btn = QPushButton("导出")
|
||||
self.export_btn.clicked.connect(self.export_words)
|
||||
self.export_btn.setStyleSheet("""
|
||||
QPushButton {
|
||||
font-size: 14px;
|
||||
padding: 8px 16px;
|
||||
background-color: #ff9800;
|
||||
color: white;
|
||||
border: none;
|
||||
border-radius: 4px;
|
||||
margin-left: 10px;
|
||||
}
|
||||
QPushButton:hover {
|
||||
background-color: #f57c00;
|
||||
}
|
||||
""")
|
||||
button_layout.addWidget(self.export_btn)
|
||||
|
||||
# 保存按钮
|
||||
self.save_btn = QPushButton("保存")
|
||||
self.save_btn.clicked.connect(self.save_words)
|
||||
self.save_btn.setStyleSheet("""
|
||||
QPushButton {
|
||||
font-size: 14px;
|
||||
padding: 8px 16px;
|
||||
background-color: #4CAF50;
|
||||
color: white;
|
||||
border: none;
|
||||
border-radius: 4px;
|
||||
margin-left: 10px;
|
||||
}
|
||||
QPushButton:hover {
|
||||
background-color: #45a049;
|
||||
}
|
||||
""")
|
||||
button_layout.addWidget(self.save_btn)
|
||||
|
||||
# 加载按钮
|
||||
self.load_btn = QPushButton("加载")
|
||||
self.load_btn.clicked.connect(self.load_words)
|
||||
self.load_btn.setStyleSheet("""
|
||||
QPushButton {
|
||||
font-size: 14px;
|
||||
padding: 8px 16px;
|
||||
background-color: #9c27b0;
|
||||
color: white;
|
||||
border: none;
|
||||
border-radius: 4px;
|
||||
margin-left: 10px;
|
||||
}
|
||||
QPushButton:hover {
|
||||
background-color: #7b1fa2;
|
||||
}
|
||||
""")
|
||||
button_layout.addWidget(self.load_btn)
|
||||
button_layout.setContentsMargins(0, 15, 0, 15)
|
||||
layout.addLayout(button_layout)
|
||||
|
||||
# 创建状态标签
|
||||
self.status_label = QLabel("")
|
||||
self.status_label.setAlignment(Qt.AlignCenter)
|
||||
self.status_label.setStyleSheet("font-size: 14px; color: #333; padding: 10px; background-color: #f0f8ff; border-radius: 4px;")
|
||||
layout.addWidget(self.status_label)
|
||||
|
||||
# 初始化线程
|
||||
self.save_thread = None
|
||||
self.load_thread = None
|
||||
|
||||
# 加载敏感词
|
||||
self.load_words()
|
||||
|
||||
logger.info("敏感词配置界面创建完成")
|
||||
|
||||
def import_words(self):
|
||||
"""
|
||||
导入敏感词
|
||||
"""
|
||||
file_path, _ = QFileDialog.getOpenFileName(self, "选择文件", "", "文本文件 (*.txt)")
|
||||
if file_path:
|
||||
try:
|
||||
with open(file_path, 'r', encoding='utf-8') as f:
|
||||
words = f.readlines()
|
||||
words = [word.strip() for word in words if word.strip()]
|
||||
self.text_edit.setText('\n'.join(words))
|
||||
self.status_label.setText(f"成功导入 {len(words)} 个敏感词")
|
||||
logger.info(f"成功导入敏感词文件: {file_path}, 共 {len(words)} 个敏感词")
|
||||
except Exception as e:
|
||||
self.status_label.setText(f"导入失败: {str(e)}")
|
||||
logger.error(f"导入敏感词失败: {e}")
|
||||
|
||||
def export_words(self):
|
||||
"""
|
||||
导出敏感词
|
||||
"""
|
||||
words = self.text_edit.toPlainText().split('\n')
|
||||
words = [word.strip() for word in words if word.strip()]
|
||||
|
||||
if not words:
|
||||
self.status_label.setText("没有敏感词可导出")
|
||||
return
|
||||
|
||||
file_path, _ = QFileDialog.getSaveFileName(self, "保存文件", "", "文本文件 (*.txt)")
|
||||
if file_path:
|
||||
try:
|
||||
with open(file_path, 'w', encoding='utf-8') as f:
|
||||
for word in words:
|
||||
f.write(word + '\n')
|
||||
self.status_label.setText(f"成功导出 {len(words)} 个敏感词")
|
||||
logger.info(f"成功导出 {len(words)} 个敏感词到 {file_path}")
|
||||
except Exception as e:
|
||||
self.status_label.setText(f"导出失败: {str(e)}")
|
||||
logger.error(f"导出敏感词失败: {e}")
|
||||
|
||||
def save_words(self):
|
||||
"""
|
||||
保存敏感词
|
||||
"""
|
||||
words = self.text_edit.toPlainText().split('\n')
|
||||
words = [word.strip() for word in words if word.strip()]
|
||||
|
||||
# 禁用按钮,防止重复点击
|
||||
self.save_btn.setEnabled(False)
|
||||
self.status_label.setText("正在保存敏感词...")
|
||||
|
||||
# 创建并启动保存线程
|
||||
self.save_thread = SaveWordsThread(words)
|
||||
self.save_thread.finished.connect(self.on_save_finished)
|
||||
self.save_thread.start()
|
||||
|
||||
def on_save_finished(self, success, message, count):
|
||||
"""
|
||||
保存完成的回调函数
|
||||
"""
|
||||
if success:
|
||||
self.status_label.setText(f"成功保存 {count} 个敏感词")
|
||||
logger.info(f"成功保存 {count} 个敏感词到数据库")
|
||||
else:
|
||||
self.status_label.setText(f"保存失败: {message}")
|
||||
logger.error(f"保存敏感词失败: {message}")
|
||||
|
||||
# 重新启用按钮
|
||||
self.save_btn.setEnabled(True)
|
||||
|
||||
def load_words(self):
|
||||
"""
|
||||
加载敏感词
|
||||
"""
|
||||
# 禁用按钮,防止重复点击
|
||||
self.load_btn.setEnabled(False)
|
||||
self.status_label.setText("正在加载敏感词...")
|
||||
|
||||
# 创建并启动加载线程
|
||||
self.load_thread = LoadWordsThread()
|
||||
self.load_thread.finished.connect(self.on_load_finished)
|
||||
self.load_thread.start()
|
||||
|
||||
def on_load_finished(self, success, words, message):
|
||||
"""
|
||||
加载完成的回调函数
|
||||
"""
|
||||
if success:
|
||||
self.text_edit.setText('\n'.join(words))
|
||||
self.status_label.setText(message)
|
||||
logger.info(message)
|
||||
else:
|
||||
self.status_label.setText(f"加载失败: {message}")
|
||||
logger.error(f"加载敏感词失败: {message}")
|
||||
|
||||
# 重新启用按钮
|
||||
self.load_btn.setEnabled(True)
|
||||
Reference in New Issue
Block a user