overpass/storage.py

60 lines
1.5 KiB
Python

import logging
from models import POI
from abc import ABC, abstractmethod
from dataclasses import asdict
from pathlib import Path
import json
logger = logging.getLogger(__name__)
class StorageError(Exception):
pass
class StorageWriter(ABC):
"""Write-only Interface. Klassen MÜSSEN explizit erben."""
@abstractmethod
def store(self, results: list[POI]) -> str:
"""
Speichert eine Liste von POIs.
Args:
results: Liste von POI-Objekten.
Returns:
str: Identifier der gespeicherten Ressource
(Dateipfad, DB-Tabelle, URL, etc.)
Raises:
StorageError: Bei Schreibfehlern.
"""
raise NotImplementedError
class JsonStorage(StorageWriter):
"""
"""
def __init__(self, output_dir: Path = Path(".")):
self.output_dir = output_dir
self._path = self.output_dir / "pois.json"
def store(self, results: list[POI]) -> str:
try:
with self._path.open("w", encoding="utf-8") as f:
json.dump([asdict(poi) for poi in results], f, indent=2, ensure_ascii=False)
except OSError as exc:
logger.error(f"Fehler beim Speichern: {exc}")
raise StorageError("Fehler beim Speichern der POIs") from exc
return str(self._path.resolve())
class PostgresStorage(StorageWriter):
def __init__(self, connection_string: str, table: str = "pois"):
pass
def store(self, results: list[POI]) -> str:
raise NotImplementedError