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

1"""

2 File loaders that work regardless of Python version.

3"""

4 import io

5 import json as json_lib

6 from typing import BinaryIO

7

8 import tomli

9 import yaml as yaml_lib

10 from dotenv import dotenv_values

11

12 from ._types import T_config, as_tconfig

13

14

15 def json(f: BinaryIO) -> T_config:

16 """

17 Load a JSON file.

18 """

19 data = json_lib.load(f)

20 return as_tconfig(data)

21

22

23 def yaml(f: BinaryIO) -> T_config:

24 """

25 Load a YAML file.

26 """

27 data = yaml_lib.load(f, yaml_lib.SafeLoader)

28 return as_tconfig(data)

29

30

31 def toml(f: BinaryIO) -> T_config:

32 """

33 Load a toml file.

34 """

35 data = tomli.load(f)

36 return as_tconfig(data)

37

38

39 def dotenv(f: BinaryIO) -> T_config:

40 """

41 Load a toml file.

42 """

43 _bytes = f.read()

44 text = _bytes.decode()

45 data = dotenv_values(stream=io.StringIO(text))

46 return as_tconfig(data)