Coverage for app/logic/config.py: 91%

23 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2024-06-17 19:06 +0000

1import yaml 

2 

3import collections.abc as collections 

4 

5def deep_update(d, u): 

6 """ 

7 Update old_dict in place with the values from new_dict, respecting nested dictionaries. 

8 Adapted from this stackoverflow answer: https://stackoverflow.com/a/32357112 

9 """ 

10 if d is None: d = {} 

11 if not u: return d 

12 

13 for key, val in u.items(): 

14 if isinstance(d, collections.Mapping): 

15 if isinstance(val, collections.Mapping): 

16 r = deep_update(d.get(key, {}), val) 

17 d[key] = r 

18 else: 

19 d[key] = u[key] 

20 else: 

21 d = {key: u[key]} 

22 return d 

23 

24def load_config_files(app): 

25 # we want to switch between three config files 

26 update_config_from_yaml(app, "default.yml") 

27 update_config_from_yaml(app, f"{app.env}.yml") 

28 update_config_from_yaml(app, "local-override.yml") 

29 

30def update_config_from_yaml(app, configFile): 

31 """ 

32 Update the application config with a yml file based on the Flask environment. 

33 """ 

34 with open(f"app/config/{configFile}", 'r') as ymlfile: 

35 try: 

36 app.config.update(deep_update(app.config, yaml.load(ymlfile, Loader=yaml.FullLoader))) 

37 except TypeError: 

38 print(F"There was an error loading the override config file {configFile}.")