githubexplorer / app /services /index_service.py
Kareman's picture
initial commit: full implemntation of git explorer project
acc643d
Raw
History Blame Contribute Delete
1.16 kB
from pathlib import Path
import json
from pydantic import BaseModel
class IndexService:
INDEX_FOLDER = "index"
def _get_index_path(self, repo_path: Path) -> Path:
index_path = repo_path / self.INDEX_FOLDER
index_path.mkdir(exist_ok=True)
return index_path
def save(self, repo_path: Path, filename: str, data):
index_path = self._get_index_path(repo_path)
output = index_path / filename
if isinstance(data, BaseModel):
payload = data.model_dump()
elif isinstance(data, list):
payload = [
item.model_dump() if isinstance(item, BaseModel) else item
for item in data
]
else:
payload = data
with open(output, "w", encoding="utf-8") as f:
json.dump(
payload,
f,
indent=4,
ensure_ascii=False,
)
def load(self, repo_path: Path, filename: str):
index_path = self._get_index_path(repo_path)
with open(index_path / filename, encoding="utf-8") as f:
return json.load(f)