Coverage for apis_core/utils/settings.py: 68%

19 statements  

« prev     ^ index     » next       coverage.py v7.6.4, created at 2024-11-22 07:51 +0000

1# SPDX-FileCopyrightText: 2023 Birger Schacht 

2# SPDX-License-Identifier: MIT 

3 

4import logging 

5 

6import tomllib 

7from django.conf import settings 

8from django.template.utils import get_app_template_dirs 

9 

10logger = logging.getLogger(__name__) 

11 

12 

13def get_entity_settings_by_modelname(entity: str = None) -> dict: 

14 """ 

15 return the settings for a specific entity or the dict for all entities 

16 if no entity is given 

17 """ 

18 apis_entities = getattr(settings, "APIS_ENTITIES", {}) 

19 if entity: 

20 # lookup entity settings by name and by capitalized name 

21 return apis_entities.get(entity, apis_entities.get(entity.capitalize(), {})) 

22 return apis_entities 

23 

24 

25def dict_from_toml_directory(directory: str) -> dict: 

26 configs = {} 

27 pathlists = [path.glob("**/*.toml") for path in get_app_template_dirs(directory)] 

28 for file in [path for pathlist in pathlists for path in pathlist]: 

29 try: 

30 configs[file.resolve()] = tomllib.loads(file.read_text()) 

31 except Exception as e: 

32 logger.error(f"TOML parser could not read {file}: {e}") 

33 return configs