58 lines
1.5 KiB
Python
58 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 StorageAdapter(ABC):
|
|
"""Abstrakte Basisklasse für POI-Storage-Backends."""
|
|
|
|
@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(StorageAdapter):
|
|
def __init__(self, output_dir: Path = Path(".")):
|
|
self.output_dir = output_dir
|
|
|
|
def store(self, results: list[POI]) -> str:
|
|
output_path = self.output_dir / "pois.json"
|
|
try:
|
|
with output_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(output_path.resolve())
|
|
|
|
|
|
|
|
class PostgresStorage(StorageAdapter):
|
|
def __init__(self, connection_string: str, table: str = "pois"):
|
|
pass
|
|
|
|
def store(self, results: list[POI]) -> str:
|
|
raise NotImplementedError |