mac-compute-space / app /core /storage.py
josephrw's picture
Upload folder using huggingface_hub
c4916b2 verified
Raw
History Blame Contribute Delete
1.54 kB
import os
import json
from datetime import datetime
from typing import Any, Dict, List, Optional
def ensure_storage_dirs() -> None:
os.makedirs("./data", exist_ok=True)
def write_jsonl(path: str, record: Dict[str, Any]) -> None:
ensure_storage_dirs()
with open(path, "a", encoding="utf-8") as f:
f.write(json.dumps(record, default=_json_default) + "\n")
def read_jsonl(path: str) -> List[Dict[str, Any]]:
records: List[Dict[str, Any]] = []
if not os.path.exists(path):
return records
with open(path, "r", encoding="utf-8") as f:
for line in f:
line = line.strip()
if line:
records.append(json.loads(line))
return records
def save_receipt(receipt: Dict[str, Any]) -> None:
write_jsonl("./data/receipts.jsonl", receipt)
def load_receipt_by_id(receipt_id: str) -> Optional[Dict[str, Any]]:
for record in read_jsonl("./data/receipts.jsonl"):
if record.get("receipt_id") == receipt_id:
return record
return None
def save_lease(lease: Dict[str, Any]) -> None:
write_jsonl("./data/leases.jsonl", lease)
def save_job_event(event: Dict[str, Any]) -> None:
write_jsonl("./data/job_events.jsonl", event)
def save_windsurf_packet(packet: Dict[str, Any]) -> None:
write_jsonl("./data/windsurf_packets.jsonl", packet)
def _json_default(obj: Any) -> Any:
if isinstance(obj, datetime):
return obj.isoformat()
raise TypeError(f"Object of type {type(obj)} is not JSON serializable")