Coverage for lintro / tools / core / install_strategies / registry.py: 86%
14 statements
« prev ^ index » next coverage.py v7.13.0, created at 2026-04-03 18:53 +0000
« prev ^ index » next coverage.py v7.13.0, created at 2026-04-03 18:53 +0000
1"""Strategy registry mapping install_type strings to strategy instances."""
3from __future__ import annotations
5from lintro.tools.core.install_strategies.base import InstallStrategy
7_STRATEGIES: dict[str, InstallStrategy] = {}
10def register_strategy(strategy: InstallStrategy) -> InstallStrategy:
11 """Register a strategy for its install_type.
13 Args:
14 strategy: The strategy instance to register.
16 Returns:
17 The same strategy instance (allows use as a decorator).
19 Raises:
20 ValueError: If a strategy is already registered for the same install_type.
21 """
22 key = strategy.install_type()
23 if key in _STRATEGIES:
24 msg = f"Duplicate install strategy registration for {key!r}"
25 raise ValueError(msg)
26 _STRATEGIES[key] = strategy
27 return strategy
30def get_strategy(install_type: str) -> InstallStrategy | None:
31 """Look up the strategy for an install_type.
33 Args:
34 install_type: The install_type string (e.g., ``"pip"``).
36 Returns:
37 The strategy instance, or None if unregistered.
38 """
39 return _STRATEGIES.get(install_type)
42def strategy_registry() -> dict[str, InstallStrategy]:
43 """Return a copy of the full registry.
45 Returns:
46 Dict mapping install_type to strategy.
47 """
48 return dict(_STRATEGIES)