Coverage for src/python/ensembl/vep/workflow/build/registry.py: 100%
31 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 workflow builders."""
16from __future__ import annotations
18from collections.abc import Callable
20from ensembl.vep.config import Config
21from ensembl.vep.workflow.base import Workflow
22from ensembl.vep.workflow.tracks.base import TrackResources
24__all__ = [
25 "available_workflows",
26 "build_workflow",
27 "register_workflow",
28 "workflow_builder",
29 "workflow_for_command",
30]
32WorkflowBuilder = Callable[[Config, TrackResources], Workflow]
33"""Build one runtime workflow from a configuration and the resources its tracks share."""
35_WORKFLOW_BUILDERS: dict[str, WorkflowBuilder] = {}
37_DEFAULT_COMMANDS = frozenset(("", "run.default"))
38"""The commands the default workflow serves. An empty command is direct Python construction."""
41def register_workflow(name: str) -> Callable[[WorkflowBuilder], WorkflowBuilder]:
42 """Register a workflow builder under ``name``."""
44 def decorator(builder: WorkflowBuilder) -> WorkflowBuilder:
45 _WORKFLOW_BUILDERS[name] = builder
46 return builder
48 return decorator
51def workflow_builder(name: str) -> WorkflowBuilder:
52 """Return the workflow builder registered under ``name``.
54 Raises:
55 KeyError: If no builder is registered under that name.
56 """
57 try:
58 return _WORKFLOW_BUILDERS[name]
59 except KeyError as error:
60 available = ", ".join(available_workflows()) or "<none>"
61 raise KeyError(f"Unknown workflow '{name}'. Available workflows: {available}") from error
64def workflow_for_command(command: str) -> str:
65 """Return the name of the workflow a command annotates with.
67 Cheap enough to call before any worker starts, which is where an unimplemented command should
68 be reported rather than inside every worker process.
70 Raises:
71 ValueError: If the command has no implemented annotation workflow.
72 """
73 if command not in _DEFAULT_COMMANDS:
74 raise ValueError(
75 f"Cannot build an annotation workflow for command '{command}'. "
76 "Only 'run.default' is implemented."
77 )
78 return "default"
81def build_workflow(config: Config, resources: TrackResources, *, name: str | None = None) -> Workflow:
82 """Build the workflow a configuration describes.
84 Args:
85 config (Config): The run's configuration.
86 resources (TrackResources): What every track in the workflow shares.
87 name (str | None): A registered workflow name, or None for the configured command's own.
89 Returns:
90 Workflow: The runtime workflow.
91 """
92 chosen = name if name is not None else workflow_for_command(config.command)
93 return workflow_builder(chosen)(config, resources)
96def available_workflows() -> tuple[str, ...]:
97 """Return registered workflow names in sorted order."""
98 return tuple(sorted(_WORKFLOW_BUILDERS))