Files
document-haness/scripts/apply-command-pedagogy-patch.py
T

49 lines
1.7 KiB
Python
Executable File

#!/usr/bin/env python3
"""Frozen command analysis와 CommandPatchSet을 사용해 command span만 교체한다."""
from __future__ import annotations
import argparse
import json
import os
import sys
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from command_pedagogy import apply_command_patch_set # noqa: E402
def load_json(path: str) -> dict:
with open(path, encoding="utf-8") as fh:
value = json.load(fh)
if not isinstance(value, dict):
raise ValueError(f"JSON object가 아니다: {path}")
return value
def main() -> int:
ap = argparse.ArgumentParser(description="CommandPatchSet을 원래 command span 안에만 적용한다")
ap.add_argument("record", help="원본 Markdown")
ap.add_argument("analysis", help="check-command-pedagogy.py가 만든 initial analysis JSON")
ap.add_argument("patch_set", help="command-pedagogy-editor가 만든 patch JSON")
ap.add_argument("-o", "--output", required=True, help="수정된 Markdown 출력 경로")
args = ap.parse_args()
try:
with open(args.record, encoding="utf-8") as fh:
text = fh.read()
analysis = load_json(args.analysis)
patch_set = load_json(args.patch_set)
repaired = apply_command_patch_set(text=text, analysis=analysis, patch_set=patch_set)
except (OSError, json.JSONDecodeError, ValueError) as exc:
print(f"command patch를 적용하지 못했다: {exc}", file=sys.stderr)
return 2
os.makedirs(os.path.dirname(args.output) or ".", exist_ok=True)
with open(args.output, "w", encoding="utf-8") as fh:
fh.write(repaired)
print(f"command span만 적용했다: {args.output}")
return 0
if __name__ == "__main__":
raise SystemExit(main())