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

1"""Strategy registry mapping install_type strings to strategy instances.""" 

2 

3from __future__ import annotations 

4 

5from lintro.tools.core.install_strategies.base import InstallStrategy 

6 

7_STRATEGIES: dict[str, InstallStrategy] = {} 

8 

9 

10def register_strategy(strategy: InstallStrategy) -> InstallStrategy: 

11 """Register a strategy for its install_type. 

12 

13 Args: 

14 strategy: The strategy instance to register. 

15 

16 Returns: 

17 The same strategy instance (allows use as a decorator). 

18 

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 

28 

29 

30def get_strategy(install_type: str) -> InstallStrategy | None: 

31 """Look up the strategy for an install_type. 

32 

33 Args: 

34 install_type: The install_type string (e.g., ``"pip"``). 

35 

36 Returns: 

37 The strategy instance, or None if unregistered. 

38 """ 

39 return _STRATEGIES.get(install_type) 

40 

41 

42def strategy_registry() -> dict[str, InstallStrategy]: 

43 """Return a copy of the full registry. 

44 

45 Returns: 

46 Dict mapping install_type to strategy. 

47 """ 

48 return dict(_STRATEGIES)