32 lines
869 B
Python
32 lines
869 B
Python
from __future__ import annotations
|
|
|
|
import json
|
|
from pathlib import Path
|
|
|
|
|
|
class SentOffersStore:
|
|
def __init__(self, file_path: Path) -> None:
|
|
self._file_path = file_path
|
|
self._file_path.parent.mkdir(parents=True, exist_ok=True)
|
|
self._sent_keys = self._load()
|
|
|
|
def has(self, key: str) -> bool:
|
|
return key in self._sent_keys
|
|
|
|
def add(self, key: str) -> None:
|
|
self._sent_keys.add(key)
|
|
self._persist()
|
|
|
|
def _load(self) -> set[str]:
|
|
if not self._file_path.exists():
|
|
return set()
|
|
|
|
with self._file_path.open("r", encoding="utf-8") as file:
|
|
data = json.load(file)
|
|
|
|
return set(data)
|
|
|
|
def _persist(self) -> None:
|
|
with self._file_path.open("w", encoding="utf-8") as file:
|
|
json.dump(sorted(self._sent_keys), file, ensure_ascii=False, indent=2)
|