fix:添加niceGui库美化页面
This commit is contained in:
@@ -5,13 +5,15 @@ from loguru import logger
|
||||
import zipfile
|
||||
|
||||
|
||||
def export_templates_folder(output_folder="backup"):
|
||||
def export_templates_folder(output_folder, stop_event, progress_callback=None):
|
||||
"""
|
||||
将指定文件夹压缩为 zip 包
|
||||
:param source_folder: 要压缩的文件夹路径 (默认 'data')
|
||||
:param output_folder: 压缩包存放的文件夹路径 (默认 'backup')
|
||||
:param stop_event: 停止事件
|
||||
:param progress_callback : 进度条回调
|
||||
"""
|
||||
source_folder = "data"
|
||||
output_folder = output_folder if output_folder else "backup"
|
||||
try:
|
||||
# 1. 检查源文件夹是否存在
|
||||
if not os.path.exists(source_folder):
|
||||
@@ -55,11 +57,12 @@ def export_templates_folder(output_folder="backup"):
|
||||
logger.error(traceback.format_exc())
|
||||
|
||||
|
||||
def export_data(save_dir, root_dir="."):
|
||||
def export_data(save_dir, root_dir=".", progress_callback=None):
|
||||
"""
|
||||
导出 data 和 output 两个文件夹到同一个 zip 包中
|
||||
:param save_dir: 用户在 GUI 弹窗中选择的保存目录 (例如: D:/Backup)
|
||||
:param root_dir: 项目根目录 (用于找到 data 和 output)
|
||||
:param progress_callback: 进度条回调函数,接收一个 float (0.0~1.0)
|
||||
"""
|
||||
|
||||
# 1. 定义要打包的目标文件夹
|
||||
@@ -68,7 +71,18 @@ def export_data(save_dir, root_dir="."):
|
||||
# 2. 检查保存目录
|
||||
if not os.path.exists(save_dir):
|
||||
logger.error(f"保存目录不存在: {save_dir}")
|
||||
return
|
||||
return None
|
||||
|
||||
# --- 【新增步骤 A】预先计算文件总数 ---
|
||||
total_files = 0
|
||||
for target in targets:
|
||||
target_abs_path = os.path.join(root_dir, target)
|
||||
if os.path.exists(target_abs_path):
|
||||
for _, _, files in os.walk(target_abs_path):
|
||||
total_files += len(files)
|
||||
|
||||
logger.info(f"待压缩文件总数: {total_files}")
|
||||
# ------------------------------------
|
||||
|
||||
# 3. 生成压缩包路径
|
||||
timestamp = time.strftime("%Y%m%d_%H%M%S")
|
||||
@@ -78,15 +92,15 @@ def export_data(save_dir, root_dir="."):
|
||||
logger.info(f"开始备份,目标文件: {zip_path}")
|
||||
|
||||
try:
|
||||
# 4. 创建压缩包 (使用 'w' 写入模式,ZIP_DEFLATED 表示压缩)
|
||||
# 4. 创建压缩包
|
||||
with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as zf:
|
||||
|
||||
processed_count = 0 # 当前处理的文件数
|
||||
has_files = False # 标记是否真的压缩了文件
|
||||
|
||||
for target in targets:
|
||||
target_abs_path = os.path.join(root_dir, target)
|
||||
|
||||
# 检查 data 或 output 是否存在
|
||||
if not os.path.exists(target_abs_path):
|
||||
logger.warning(f"⚠️ 跳过: 找不到文件夹 '{target}'")
|
||||
continue
|
||||
@@ -94,26 +108,30 @@ def export_data(save_dir, root_dir="."):
|
||||
logger.info(f"正在压缩: {target} ...")
|
||||
|
||||
# 5. 遍历文件夹写入 ZIP
|
||||
# os.walk 会递归遍历子文件夹
|
||||
for root, dirs, files in os.walk(target_abs_path):
|
||||
for file in files:
|
||||
# 获取文件的绝对路径
|
||||
file_abs_path = os.path.join(root, file)
|
||||
|
||||
# 【关键】计算在压缩包里的相对路径
|
||||
# 例如: D:/Project/data/images/1.jpg -> data/images/1.jpg
|
||||
# 计算相对路径
|
||||
arcname = os.path.relpath(file_abs_path, root_dir)
|
||||
|
||||
# 写入压缩包
|
||||
zf.write(file_abs_path, arcname)
|
||||
has_files = True
|
||||
# 更新进度条
|
||||
if progress_callback:
|
||||
progress_callback(processed_count + 1, total_files, "导出数据中...")
|
||||
|
||||
if has_files:
|
||||
# 确保进度条最后能走到 100%
|
||||
if progress_callback:
|
||||
progress_callback(total_files, total_files, "导出数据成功")
|
||||
|
||||
logger.success(f"✅ 备份成功! 文件已保存至:\n{zip_path}")
|
||||
return zip_path
|
||||
else:
|
||||
logger.error("❌ 备份失败: data 和 output 文件夹均为空或不存在。")
|
||||
# 如果生成了空文件,建议删除
|
||||
if os.path.exists(zip_path):
|
||||
os.remove(zip_path)
|
||||
return None
|
||||
@@ -121,14 +139,15 @@ def export_data(save_dir, root_dir="."):
|
||||
except Exception as e:
|
||||
logger.error(f"导出过程出错: {str(e)}")
|
||||
import traceback
|
||||
|
||||
logger.error(traceback.format_exc())
|
||||
return None
|
||||
|
||||
|
||||
def initialize_project(root_dir="."):
|
||||
def initialize_project(root_dir=".", progress_callback=None):
|
||||
"""
|
||||
初始化项目:清空 data,重建目录,复制模板
|
||||
:param root_dir: 项目根目录
|
||||
:param progress_callback : 进度条回调
|
||||
"""
|
||||
# 定义路径
|
||||
data_dir = os.path.join(root_dir, "data")
|
||||
@@ -192,13 +211,9 @@ def initialize_project(root_dir="."):
|
||||
f"⚠️ 警告: 模板文件不存在 ({src_excel}),data 文件夹内将没有 Excel 文件。"
|
||||
)
|
||||
|
||||
|
||||
def check_file_exists(file_path):
|
||||
"""
|
||||
判断文件是否存在
|
||||
"""
|
||||
if (file_path and isinstance(file_path, str) and os.path.exists(file_path)):
|
||||
logger.info(f"✅ 文件存在: {file_path}")
|
||||
return True
|
||||
else:
|
||||
logger.error(f"❌ 文件不存在: {file_path}")
|
||||
return False
|
||||
return file_path and isinstance(file_path, str) and os.path.exists(file_path)
|
||||
|
||||
Reference in New Issue
Block a user