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

1"""

2 Loads loaders based on Python version.

3"""

4

5 import typing

6

7 from ._types import T_config

8

9# tomlkit used for every Python version now.

10 from .loaders_shared import json, toml, yaml

11

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

13# from .loaders_311 import toml

14# else: # pragma: no cover

15# from .loaders_310 import toml

16

17

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

19

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

21

22 LOADERS: dict[str, T_loader] = {

23 "toml": toml,

24 "json": json,

25 "yml": yaml,

26 "yaml": yaml,

27}

28

29

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

31 """

32 Get the right loader for a specific extension.

33 """

34 extension = extension.removeprefix(".")

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

36 return loader

37 else:

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