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

32 statements  

« prev     ^ index     » next       coverage.py v7.5.3, created at 2026-02-19 10:33 +0000

1# SPDX-FileCopyrightText: 2023 Birger Schacht 

2# SPDX-License-Identifier: MIT 

3 

4import logging 

5import tomllib 

6from urllib.parse import urlparse 

7from warnings import deprecated 

8 

9from django.conf import settings 

10from django.template.utils import get_app_template_dirs 

11 

12logger = logging.getLogger(__name__) 

13 

14 

15@deprecated("Not used anywhere, so it will be dropped.") 

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

17 """ 

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

19 if no entity is given 

20 """ 

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

22 if entity: 

23 # lookup entity settings by name and by capitalized name 

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

25 return apis_entities 

26 

27 

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

29 configs = {} 

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

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

32 try: 

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

34 except Exception as e: 

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

36 return configs 

37 

38 

39def internal_uris() -> list[str]: 

40 return list(set([apis_base_uri()] + getattr(settings, "APIS_FORMER_BASE_URIS", []))) 

41 

42 

43def apis_base_uri() -> str: 

44 return getattr(settings, "APIS_BASE_URI", "https://example.org") 

45 

46 

47def rdf_namespace_prefix() -> str: 

48 if hasattr(settings, "APIS_RDF_NAMESPACE_PREFIX"): 

49 return settings.APIS_RDF_NAMESPACE_PREFIX 

50 base_uri = urlparse(apis_base_uri()) 

51 hostname = base_uri.hostname or "example.org" 

52 return hostname.split(".", 1)[0]