114 lines
3.2 KiB
Python
114 lines
3.2 KiB
Python
"""
|
|
测试同步逻辑
|
|
"""
|
|
from pathlib import Path
|
|
from template_scanner import scan_docx_placeholders
|
|
from config_model import AppConfig, PlaceholderConfig
|
|
|
|
# 模板路径
|
|
template_path = Path("configs/600泵/template.docx")
|
|
config_path = Path("configs/600泵/config.json")
|
|
|
|
print("=" * 60)
|
|
print("测试配置同步逻辑")
|
|
print("=" * 60)
|
|
|
|
# 第一步:扫描模板
|
|
print("\n第一步:扫描模板占位符")
|
|
scan_result = scan_docx_placeholders(template_path)
|
|
template_placeholders = (
|
|
scan_result.texts |
|
|
scan_result.tables |
|
|
scan_result.charts |
|
|
scan_result.manual_tables |
|
|
scan_result.script_tables |
|
|
scan_result.script_charts |
|
|
scan_result.db_texts
|
|
)
|
|
print(f"模板中找到 {len(template_placeholders)} 个占位符:")
|
|
print(f" {sorted(template_placeholders)}")
|
|
|
|
# 第二步:加载配置
|
|
print("\n第二步:加载配置文件")
|
|
config = AppConfig.load(config_path)
|
|
existing_keys = set(config.placeholders.keys())
|
|
print(f"配置中有 {len(existing_keys)} 个占位符:")
|
|
print(f" {sorted(existing_keys)}")
|
|
|
|
# 第三步:比较
|
|
print("\n第三步:比较差异")
|
|
new_keys = template_placeholders - existing_keys
|
|
removed_keys = existing_keys - template_placeholders
|
|
|
|
print(f"需要添加的占位符 ({len(new_keys)}):")
|
|
if new_keys:
|
|
print(f" {sorted(new_keys)}")
|
|
else:
|
|
print(" (无)")
|
|
|
|
print(f"需要删除的占位符 ({len(removed_keys)}):")
|
|
if removed_keys:
|
|
print(f" {sorted(removed_keys)}")
|
|
else:
|
|
print(" (无)")
|
|
|
|
# 第四步:执行同步
|
|
print("\n第四步:执行同步")
|
|
|
|
# 添加新占位符
|
|
if new_keys:
|
|
print(f"添加 {len(new_keys)} 个新占位符...")
|
|
for key in new_keys:
|
|
if key in scan_result.texts or key in scan_result.db_texts:
|
|
ph_type = "text"
|
|
elif key in scan_result.tables:
|
|
ph_type = "table"
|
|
elif key in scan_result.charts:
|
|
ph_type = "chart"
|
|
elif key in scan_result.manual_tables:
|
|
ph_type = "manualTable"
|
|
elif key in scan_result.script_tables:
|
|
ph_type = "scriptTable"
|
|
elif key in scan_result.script_charts:
|
|
ph_type = "scriptChart"
|
|
else:
|
|
ph_type = "text"
|
|
|
|
config.placeholders[key] = PlaceholderConfig(
|
|
type=ph_type,
|
|
label=key,
|
|
title="",
|
|
value="",
|
|
dbQuery="",
|
|
chart={}
|
|
)
|
|
|
|
# 删除旧占位符
|
|
if removed_keys:
|
|
print(f"删除 {len(removed_keys)} 个旧占位符...")
|
|
for key in removed_keys:
|
|
del config.placeholders[key]
|
|
|
|
# 保存配置
|
|
if new_keys or removed_keys:
|
|
print(f"保存配置文件...")
|
|
config.save(config_path)
|
|
print("✓ 配置文件已更新")
|
|
else:
|
|
print("配置已是最新,无需更新")
|
|
|
|
# 验证结果
|
|
print("\n第五步:验证结果")
|
|
config_after = AppConfig.load(config_path)
|
|
final_keys = set(config_after.placeholders.keys())
|
|
print(f"同步后配置中有 {len(final_keys)} 个占位符:")
|
|
print(f" {sorted(final_keys)}")
|
|
|
|
if final_keys == template_placeholders:
|
|
print("\n✓ 同步成功!配置与模板完全匹配")
|
|
else:
|
|
print("\n✗ 同步失败!配置与模板不匹配")
|
|
print(f" 差异: {final_keys ^ template_placeholders}")
|
|
|
|
print("\n" + "=" * 60)
|