Coverage for src/python/ensembl/vep/output/factory/write/registry.py: 100%
29 statements
« prev ^ index » next coverage.py v7.17.2, created at 2026-11-18 14:41 +0000
« prev ^ index » next coverage.py v7.17.2, created at 2026-11-18 14:41 +0000
1# See the NOTICE file distributed with this work for additional information
2# regarding copyright ownership.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14"""Annotated-variant output writer registry."""
16from __future__ import annotations
18from collections.abc import Callable
20from ensembl.vep.output.factory.write.base import OutputWriter
21from ensembl.vep.output.factory.write.json import JSONOutputWriter
22from ensembl.vep.output.factory.write.vcf import VCFOutputWriter
24__all__ = [
25 "available_output_writers",
26 "get_output_writer",
27 "register_output_writer",
28]
30OutputWriterFactory = Callable[[str], OutputWriter]
31"""Build an output writer using the run's command line."""
33_OUTPUT_WRITER_REGISTRY: dict[str, OutputWriterFactory] = {}
36def register_output_writer(name: str) -> Callable[[OutputWriterFactory], OutputWriterFactory]:
37 """Register an output writer factory under a format name."""
39 def decorator(factory: OutputWriterFactory) -> OutputWriterFactory:
40 _OUTPUT_WRITER_REGISTRY[name] = factory
41 return factory
43 return decorator
46def available_output_writers() -> tuple[str, ...]:
47 """Return the sorted set of registered output formats."""
48 return tuple(sorted(_OUTPUT_WRITER_REGISTRY))
51def get_output_writer(output_format: str, *, command_line: str = "") -> OutputWriter:
52 """Return the writer for one output format.
54 Raises:
55 ValueError: If no writer supports ``output_format``.
56 """
57 try:
58 factory = _OUTPUT_WRITER_REGISTRY[output_format]
59 except KeyError as error:
60 supported_formats = ", ".join(available_output_writers()) or "<none>"
61 raise ValueError(f"no writer for {output_format!r}; supported: {supported_formats}") from error
62 return factory(command_line)
65@register_output_writer("json")
66def _json_writer(_command_line: str) -> OutputWriter:
67 """Build the JSON-lines writer."""
68 return JSONOutputWriter()
71@register_output_writer("vcf")
72def _vcf_writer(command_line: str) -> OutputWriter:
73 """Build the VCF writer with its command-line header."""
74 return VCFOutputWriter(command_line=command_line)