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

1"""

2 Loads loaders based on Python version.

3"""

4

5 import typing

6 from pathlib import Path

7

8 from ._types import T_config

9

10# tomli used for every Python version now.

11 from .loaders_shared import dotenv, ini, json, toml, yaml

12

13 __all__ = ["get", "toml", "json", "yaml", "dotenv", "ini"]

14

15 T_loader = typing.Callable[[typing.BinaryIO, Path], T_config]

16

17 LOADERS: dict[str, T_loader] = {

18 "toml": toml,

19 "json": json,

20 "yml": yaml,

21 "yaml": yaml,

22 "env": dotenv,

23 "ini": ini,

24}

25

26

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

28 """

29 Get the right loader for a specific extension.

30 """

31 extension = extension.removeprefix(".")

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

33 return loader

34 else:

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