feat:纳入物料初始化脚本

scripts/ 下其余本地脚本仍被忽略,只放行 seed_materials.py,新克隆的仓库可直接导入物料目录
This commit is contained in:
2026-10-12 00:07:07 -07:00
parent afc07ba695
commit 199855becb
2 changed files with 54 additions and 0 deletions
+19
View File
@@ -9,3 +9,22 @@ __pycache__/
.pytest_cache/
.ruff_cache/
*.log
tests/*
app/**/*.md
logs/
.uv-cache/
docs/
.claude
.agents
.hermes
examples
skills-lock.json
app/resources/catalog
scripts/*
!scripts/seed_materials.py
evals/
results/
# DATA_FILE_DIR 的默认位置
data/
.omc
+35
View File
@@ -0,0 +1,35 @@
"""把物料定义导入 MongoDB:uv run python -m scripts.seed_materials [物料定义目录]
默认读取 app/resources/materials,导入后需重启服务生效
"""
import asyncio
import sys
from pymongo import AsyncMongoClient
from app.config import settings
from app.resources.component import MATERIALS_DIR, load_material_definitions
from app.services.material_mongo import MongoMaterialRepository
async def main(directory: str) -> None:
if not settings.mongodb_uri:
raise SystemExit("未配置 MONGODB_URI")
# 先完整校验定义,校验失败不会改动数据库
catalog = load_material_definitions(directory)
client = AsyncMongoClient(settings.mongodb_uri, serverSelectionTimeoutMS=5000)
try:
await MongoMaterialRepository(client[settings.mongodb_db]).replace_all(catalog)
finally:
await client.close()
print(
f"已导入 {len(catalog.components)} 个物料、"
f"{len(catalog.field_groups)} 个共享字段分组,版本 {catalog.version}"
)
if __name__ == "__main__":
# Windows 控制台默认编码无法输出中文
sys.stdout.reconfigure(encoding="utf-8")
asyncio.run(main(sys.argv[1] if len(sys.argv) > 1 else str(MATERIALS_DIR)))