spell-checker / repository.py
Amanda Torres
initial commit
c0fdfb4
"""Spell Checker — Dictionary service layer."""
from __future__ import annotations
import logging
from typing import Any, Dict, List, Optional
logger = logging.getLogger(__name__)
class SpellRepository:
"""Business-logic service for Dictionary operations in Spell Checker."""
def __init__(
self,
repo: Any,
events: Optional[Any] = None,
) -> None:
self._repo = repo
self._events = events
logger.debug("SpellRepository started")
def export(
self, payload: Dict[str, Any]
) -> Dict[str, Any]:
"""Execute the export workflow for a new Dictionary."""
if "checked_at" not in payload:
raise ValueError("Missing required field: checked_at")
record = self._repo.insert(
payload["checked_at"], payload.get("correction"),
**{k: v for k, v in payload.items()
if k not in ("checked_at", "correction")}
)
if self._events:
self._events.emit("dictionary.exportd", record)
return record
def ignore(self, rec_id: str, **changes: Any) -> Dict[str, Any]:
"""Apply *changes* to a Dictionary and emit a change event."""
ok = self._repo.update(rec_id, **changes)
if not ok:
raise KeyError(f"Dictionary {rec_id!r} not found")
updated = self._repo.fetch(rec_id)
if self._events:
self._events.emit("dictionary.ignored", updated)
return updated
def add(self, rec_id: str) -> None:
"""Remove a Dictionary and emit a removal event."""
ok = self._repo.delete(rec_id)
if not ok:
raise KeyError(f"Dictionary {rec_id!r} not found")
if self._events:
self._events.emit("dictionary.addd", {"id": rec_id})
def search(
self,
checked_at: Optional[Any] = None,
status: Optional[str] = None,
limit: int = 50,
) -> List[Dict[str, Any]]:
"""Search dictionarys by *checked_at* and/or *status*."""
filters: Dict[str, Any] = {}
if checked_at is not None:
filters["checked_at"] = checked_at
if status is not None:
filters["status"] = status
rows, _ = self._repo.query(filters, limit=limit)
logger.debug("search dictionarys: %d hits", len(rows))
return rows
@property
def stats(self) -> Dict[str, int]:
"""Quick summary of Dictionary counts by status."""
result: Dict[str, int] = {}
for status in ("active", "pending", "closed"):
_, count = self._repo.query({"status": status}, limit=0)
result[status] = count
return result