Coverage for src/python/ensembl/vep/runner/registry.py: 100%
35 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"""Runner registry keyed by ``<domain>.<verb>`` names."""
16from __future__ import annotations
18from collections.abc import Callable
19from typing import TypeVar
21from ensembl.vep.runner.base import Runner
23__all__ = [
24 "available_runners",
25 "get_runner",
26 "register_runner",
27 "runner_default",
28 "runner_group",
29 "runner_groups",
30]
32RunnerFactory = type[Runner]
33"""Runner classes."""
35RunnerT = TypeVar("RunnerT", bound=RunnerFactory)
36"""A particular runner class, so the decorator hands back what it was given."""
38_RUNNER_REGISTRY: dict[str, RunnerFactory] = {}
39_RUNNER_DEFAULTS: dict[str, str] = {}
42def register_runner(name: str, *, default_for_group: bool = False) -> Callable[[RunnerT], RunnerT]:
43 """Register a runner class under a name."""
45 def decorator(factory: RunnerT) -> RunnerT:
46 _RUNNER_REGISTRY[name] = factory
47 if default_for_group:
48 _RUNNER_DEFAULTS[runner_group(name)] = name
49 return factory
51 return decorator
54def get_runner(name: str) -> RunnerFactory:
55 """Return the registered runner class."""
56 try:
57 return _RUNNER_REGISTRY[name]
58 except KeyError as error:
59 available = ", ".join(sorted(_RUNNER_REGISTRY)) or "<none>"
60 raise KeyError(f"Unknown command '{name}'. Available commands: {available}") from error
63def runner_default(group: str) -> str | None:
64 """Return a group's default runner, if it declares one."""
65 return _RUNNER_DEFAULTS.get(group)
68def available_runners() -> tuple[str, ...]:
69 """Return the sorted set of registered runner names.
71 Returns:
72 tuple[str, ...]: Every registered runner name, sorted.
73 """
74 return tuple(sorted(_RUNNER_REGISTRY))
77def runner_group(name: str) -> str:
78 """Return the domain part of a runner name.
80 Args:
81 name (str): A runner name, e.g. "bench.matrix".
83 Returns:
84 str: The part before the first dot, e.g. "bench". A name with no dot is its own group.
85 """
86 return name.split(".", 1)[0]
89def runner_groups() -> dict[str, tuple[str, ...]]:
90 """Return the registered runners grouped by domain.
92 Returns:
93 dict[str, tuple[str, ...]]: Domain -> its runner names, sorted. A domain with exactly one
94 runner named after it is a subcommand in its own right; one with several is a group
95 whose verbs are the subcommands.
96 """
97 groups: dict[str, list[str]] = {}
98 for name in available_runners():
99 groups.setdefault(runner_group(name), []).append(name)
100 return {group: tuple(sorted(names)) for group, names in sorted(groups.items())}