arch-design-agent-skill-das.../backend/app/modules/editor/infrastructure/file_io.py
openclaw 11f59c6073 feat(editor): add domain entities and file I/O infrastructure
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-23 16:59:21 +00:00

36 lines
1.1 KiB
Python

from datetime import datetime, timezone
from pathlib import Path
from app.modules.editor.domain.entities import EditableFile
from app.shared.infrastructure.filesystem import read_text, write_text
def detect_format(file_path: Path) -> str:
suffix = file_path.suffix.lower()
if suffix == ".csv":
return "csv"
elif suffix == ".md":
return "md"
elif suffix in (".yaml", ".yml"):
if "openapi" in file_path.name or "api-contracts" in file_path.name:
return "openapi"
return "yaml"
return "unknown"
def read_file(base_dir: Path, relative_path: str) -> EditableFile:
full_path = base_dir / relative_path
content = read_text(full_path)
stat = full_path.stat()
return EditableFile(
path=relative_path,
format=detect_format(full_path),
content=content,
last_modified=datetime.fromtimestamp(stat.st_mtime, tz=timezone.utc),
)
def write_file(base_dir: Path, relative_path: str, content: str) -> None:
full_path = base_dir / relative_path
write_text(full_path, content)