"""Read and validate genotype file-statistics collector jobs.""" import json from typing import Any, Mapping class InvalidCollectorJob(Exception): """Raised when a job is not a valid, completed genotype-stats job.""" def file_stats( job: Mapping[str, Any], species_id: int, population_id: int, dataset_id: int ) -> dict: """Return the FileStats recorded by a completed collector job. Raises ``InvalidCollectorJob`` if the job is not a completed ``genotype-file-stats`` job for the given species/population/dataset, or if its recorded output cannot be parsed. """ metadata = job.get("metadata", {}) if metadata.get("job-type") != "genotype-file-stats": raise InvalidCollectorJob( "The job is not a genotype file statistics collection job.") if metadata.get("status") != "completed": raise InvalidCollectorJob( "The file statistics collection job has not completed.") for key, expected in (("species_id", species_id), ("population_id", population_id), ("dataset_id", dataset_id)): if str(metadata.get(key)) != str(expected): raise InvalidCollectorJob( "The file statistics collection job belongs to a different " "dataset.") try: return json.loads(job.get("stdout") or "") except (TypeError, ValueError) as _exc: raise InvalidCollectorJob( f"Could not read the file statistics from the job: {_exc}") from _exc