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

25 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-09-16 07:42 +0000

1# SPDX-FileCopyrightText: 2023 Birger Schacht 

2# SPDX-License-Identifier: MIT 

3 

4import logging 

5from pathlib import Path 

6 

7import tomllib 

8from django.conf import settings 

9from django.template.utils import get_app_template_dirs 

10 

11logger = logging.getLogger(__name__) 

12 

13 

14def default_settings() -> Path: 

15 curpath = Path(__file__).parent 

16 return curpath.parent / "default_settings" 

17 

18 

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

20 """ 

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

22 if no entity is given 

23 """ 

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

25 if entity: 

26 # lookup entity settings by name and by capitalized name 

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

28 return apis_entities 

29 

30 

31def list_links_to_edit() -> bool: 

32 return getattr(settings, "APIS_LIST_LINKS_TO_EDIT", False) 

33 

34 

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

36 configs = {} 

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

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

39 try: 

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

41 except Exception as e: 

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

43 return configs