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 from .loaders_shared import json, yaml

10

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

12 from .loaders_311 import toml

13 else: # pragma: no cover

14 from .loaders_310 import toml

15

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

17

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

19

20 LOADERS: dict[str, T_loader] = {

21 "toml": toml,

22 "json": json,

23 "yml": yaml,

24 "yaml": yaml,

25}

26

27

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

29 """

30 Get the right loader for a specific extension.

31 """

32 extension = extension.removeprefix(".")

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

34 return loader

35 else:

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