- Refactor codebase into src/ (preprocessing, translation, assembly) - Add pipeline/ scripts for individual stages - Externalize configuration to config/config.yaml - Fix Cover Image preservation - Update documentation and manuals
63 lines
2.0 KiB
Python
63 lines
2.0 KiB
Python
"""模拟翻译流程,定位数据丢失问题"""
|
|
import asyncio
|
|
from src.manifest_manager import ManifestManager
|
|
|
|
async def simulate_worker(manifest, chunks):
|
|
"""模拟 worker 行为"""
|
|
for chunk in chunks:
|
|
for item in chunk:
|
|
# 模拟 LLM 返回
|
|
fake_translation = f"翻译_{item.global_id}"
|
|
# 模拟 worker 的 update_item 调用
|
|
manifest.update_item(
|
|
item.global_id,
|
|
fake_translation,
|
|
translation_with_placeholders=fake_translation,
|
|
status="translated"
|
|
)
|
|
print(f"Worker 完成,内存中 translated 数量: {len(manifest.get_items(status='translated'))}")
|
|
|
|
async def simulate_restoration(manifest):
|
|
"""模拟 process_format_restoration"""
|
|
items = manifest.get_items() # 不带参数,获取所有
|
|
print(f"Restoration 获取到 {len(items)} 个 items")
|
|
|
|
processed = 0
|
|
skipped = 0
|
|
for item in items:
|
|
# 这是关键的过滤条件
|
|
if item.status != "translated" or not item.translation_with_placeholders:
|
|
skipped += 1
|
|
continue
|
|
processed += 1
|
|
|
|
print(f"Restoration: 处理 {processed} 个,跳过 {skipped} 个")
|
|
|
|
async def main():
|
|
# 初始化
|
|
manifest = ManifestManager("test_flow.json")
|
|
manifest.init_manifest("test", {})
|
|
|
|
# 添加测试 items
|
|
for i in range(5):
|
|
manifest.add_item(f"test{i}.html", f"<p>Text {i}</p>", f"Text {i}", "p")
|
|
|
|
# 模拟 create_chunks_from_manifest
|
|
pending = manifest.get_items(status="pending")
|
|
chunks = [pending] # 一个 chunk 包含所有
|
|
print(f"Chunks 创建,pending 数量: {len(pending)}")
|
|
print(f"chunks[0][0] is manifest._items_by_id['p_00001']: {chunks[0][0] is manifest._items_by_id['p_00001']}")
|
|
|
|
# 模拟 worker
|
|
await simulate_worker(manifest, chunks)
|
|
|
|
# 模拟 restoration
|
|
await simulate_restoration(manifest)
|
|
|
|
# 清理
|
|
import os
|
|
if os.path.exists("test_flow.json"):
|
|
os.remove("test_flow.json")
|
|
|
|
asyncio.run(main())
|