tejasashinde's picture
Initial commit
1e34d32
Raw
History Blame Contribute Delete
1.43 kB
from __future__ import annotations
import json
from pathlib import Path
from backend.models import SpaceItem
ROOT_DIR = Path(__file__).resolve().parents[1]
DATA_DIR = ROOT_DIR / "data"
def _ensure_parent(path: Path) -> None:
path.parent.mkdir(parents=True, exist_ok=True)
def load_spaces_cache(path: str = "data/spaces_cache.json") -> list[SpaceItem]:
cache_path = Path(path)
if not cache_path.exists():
return []
try:
payload = json.loads(cache_path.read_text(encoding="utf-8"))
except Exception:
return []
if not isinstance(payload, list):
return []
return [SpaceItem.from_dict(item) for item in payload if isinstance(item, dict)]
def save_spaces_cache(spaces: list[SpaceItem], path: str = "data/spaces_cache.json") -> None:
cache_path = Path(path)
_ensure_parent(cache_path)
cache_path.write_text(
json.dumps([space.to_dict() for space in spaces], indent=2, ensure_ascii=False),
encoding="utf-8",
)
def load_sample_spaces(path: str = "data/sample_spaces.json") -> list[SpaceItem]:
sample_path = Path(path)
if not sample_path.exists():
return []
try:
payload = json.loads(sample_path.read_text(encoding="utf-8"))
except Exception:
return []
if not isinstance(payload, list):
return []
return [SpaceItem.from_dict(item) for item in payload if isinstance(item, dict)]