Files
state-hub/api/routers/policy.py

35 lines
1005 B
Python

import re
from pathlib import Path
from fastapi import HTTPException
from hub_core.routers.policy import create_policy_router
from hub_core.schemas.policy import PolicyRead
POLICY_DIR = Path(__file__).parent.parent.parent / "policies"
_VALID_NAME = re.compile(r"^[a-z0-9][a-z0-9-]{0,63}$")
def _policy_path(name: str) -> Path:
if not _VALID_NAME.match(name):
raise HTTPException(status_code=400, detail="Invalid policy name")
path = POLICY_DIR / f"{name}.md"
if not path.exists():
raise HTTPException(status_code=404, detail=f"Policy '{name}' not found")
return path
def _load_policy(name: str) -> PolicyRead:
path = _policy_path(name)
return PolicyRead(name=name, content=path.read_text())
def _update_policy(name: str, content: str) -> PolicyRead:
path = _policy_path(name)
path.write_text(content)
return PolicyRead(name=name, content=content)
router = create_policy_router(_load_policy, update_policy=_update_policy)
__all__ = ["router"]