from pathlib import Path from typing import Any, Callable from cross.service import InputService, OutputService from database.connection import DatabasePool input_factories: dict[str, Callable[[DatabasePool, dict[str, Any]], InputService]] = {} output_factories: dict[str, Callable[[DatabasePool, dict[str, Any]], OutputService]] = {} def create_input_service(db: DatabasePool, data: dict[str, Any]) -> InputService: if "type" not in data: raise ValueError("No `type` field in input data!") type: str = str(data["type"]) del data["type"] factory = input_factories.get(type) if not factory: raise KeyError(f"No such input service {type}!") return factory(db, data) def create_output_service(db: DatabasePool, data: dict[str, Any]) -> OutputService: if "type" not in data: raise ValueError("No `type` field in input data!") type: str = str(data["type"]) del data["type"] factory = output_factories.get(type) if not factory: raise KeyError(f"No such output service {type}!") return factory(db, data)