Coverage for src/configuraptor/loaders/__init__.py: 100%

1"""

2 Loads loaders based on Python version.

3"""

4

5 import sys

6 import typing

7

8 from ._types import T_config

9

10# tomlkit used for every Python version now.

11 from .loaders_shared import json, yaml, toml

12

13# if sys.version_info > (3, 11):

14# from .loaders_311 import toml

15# else: # pragma: no cover

16# from .loaders_310 import toml

17

18

19 __all__ = ["get", "toml", "json", "yaml"]

20

21 T_loader = typing.Callable[[typing.BinaryIO], T_config]

22

23 LOADERS: dict[str, T_loader] = {

24 "toml": toml,

25 "json": json,

26 "yml": yaml,

27 "yaml": yaml,

28}

29

30

31 def get(extension: str) -> T_loader:

32 """

33 Get the right loader for a specific extension.

34 """

35 extension = extension.removeprefix(".")

36 if loader := LOADERS.get(extension):

37 return loader

38 else:

39 raise ValueError(f"Invalid extension {extension}")