Coverage for src/python/ensembl/vep/corpus/exporter/registry.py: 100%
25 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"""Registry for corpus exporters."""
16from __future__ import annotations
18from collections.abc import Callable
19from typing import TypeVar
21from ensembl.vep.corpus.exporter.base import CorpusExporter
23__all__ = ["available_exporters", "get_exporter", "register_exporter"]
25ExporterFactory = Callable[[], CorpusExporter]
26"""Construct a corpus exporter."""
28ExporterFactoryT = TypeVar("ExporterFactoryT", bound=ExporterFactory)
29"""A particular exporter factory for the registration decorator."""
31_EXPORTER_REGISTRY: dict[str, ExporterFactory] = {}
34def register_exporter(name: str) -> Callable[[ExporterFactoryT], ExporterFactoryT]:
35 """Register an exporter factory under a name."""
37 def decorator(factory: ExporterFactoryT) -> ExporterFactoryT:
38 if name in _EXPORTER_REGISTRY:
39 raise ValueError(f"Corpus exporter {name!r} is already registered")
40 _EXPORTER_REGISTRY[name] = factory
41 return factory
43 return decorator
46def get_exporter(name: str) -> CorpusExporter:
47 """Return a new registered exporter.
49 Raises:
50 KeyError: If no exporter is registered under ``name``.
51 """
52 try:
53 return _EXPORTER_REGISTRY[name]()
54 except KeyError as error:
55 available = ", ".join(sorted(_EXPORTER_REGISTRY)) or "<none>"
56 raise KeyError(f"Unknown corpus exporter '{name}'. Available exporters: {available}") from error
59def available_exporters() -> tuple[str, ...]:
60 """Return the sorted set of registered exporter names."""
61 return tuple(sorted(_EXPORTER_REGISTRY))