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

21 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-28 14:59 +0200

1""" 

2File loaders that work regardless of Python version. 

3""" 

4import io 

5import json as json_lib 

6from typing import BinaryIO 

7 

8import tomli 

9import yaml as yaml_lib 

10from dotenv import dotenv_values 

11 

12from ._types import T_config, as_tconfig 

13 

14 

15def 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 

23def 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 

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

32 """ 

33 Load a toml file. 

34 """ 

35 data = tomli.load(f) 

36 return as_tconfig(data) 

37 

38 

39def 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)