PCM_Report/restore_config_backup.py

54 lines
1.6 KiB
Python

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
配置文件恢复工具
用于从备份恢复 default.json 配置文件
"""
import shutil
from pathlib import Path
def restore_config_backup():
"""从备份恢复配置文件"""
app_dir = Path(__file__).parent
config_file = app_dir / "default.json"
backup_file = app_dir / "default.json.bak"
if not backup_file.exists():
print(f"❌ 备份文件不存在: {backup_file}")
return False
if config_file.exists():
# 先备份当前文件
current_backup = app_dir / "default.json.before_restore"
shutil.copy2(config_file, current_backup)
print(f"✅ 当前配置已备份到: {current_backup}")
# 从备份恢复
shutil.copy2(backup_file, config_file)
print(f"✅ 配置已从备份恢复: {backup_file} -> {config_file}")
# 显示恢复的配置内容
print("\n恢复的配置内容预览:")
print("=" * 60)
with open(config_file, 'r', encoding='utf-8') as f:
content = f.read()
lines = content.split('\n')
for i, line in enumerate(lines[:20], 1): # 只显示前20行
print(f"{i:3d}: {line}")
if len(lines) > 20:
print(f"... (还有 {len(lines) - 20} 行)")
print("=" * 60)
return True
if __name__ == "__main__":
print("=" * 60)
print("配置文件恢复工具")
print("=" * 60)
if restore_config_backup():
print("\n✅ 配置恢复成功!")
print("请重启程序以加载恢复的配置。")
else:
print("\n❌ 配置恢复失败!")