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# tomli used for every Python version now.

10 from .loaders_shared import dotenv, json, toml, yaml

11

12 __all__ = ["get", "toml", "json", "yaml", "dotenv"]

13

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

15

16 LOADERS: dict[str, T_loader] = {

17 "toml": toml,

18 "json": json,

19 "yml": yaml,

20 "yaml": yaml,

21 "env": dotenv,

22}

23

24

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

26 """

27 Get the right loader for a specific extension.

28 """

29 extension = extension.removeprefix(".")

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

31 return loader

32 else:

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