Coverage for src/backoffice/utils.py: 0%

26 statements  

« prev     ^ index     » next       coverage.py v7.15.4, created at 2026-08-13 03:07 +0000

1"""utility functions""" 

2 

3import json 

4from typing import TYPE_CHECKING, Any 

5 

6try: 

7 from pydantic import ValidationError 

8except ImportError as e: 

9 raise ImportError( 

10 "pydantic is required for backoffice.utils. " 

11 "Please install `backoffice[dev]` or use backoffice.utils_plain." 

12 ) from e 

13 

14from backoffice.compatibility import CompatibilitySummary, InitialSummary 

15from backoffice.utils_pure import cached_download, get_summary_file_path 

16 

17if TYPE_CHECKING: 

18 from ruyaml import YAML 

19else: 

20 try: 

21 from ruyaml import YAML 

22 except ImportError: 

23 from ruamel.yaml import YAML 

24 

25yaml = YAML(typ="safe") 

26 

27 

28def get_rdf_content_from_url(url: str, sha256: str) -> dict[str, Any]: 

29 local_path = cached_download(url, sha256) 

30 return yaml.load(local_path) 

31 

32 

33def get_summary(item_id: str, version: str) -> "InitialSummary | CompatibilitySummary": 

34 """Retrieve the summary for a specific item and version.""" 

35 summary_path = get_summary_file_path(item_id, version) 

36 if not summary_path.exists(): 

37 return InitialSummary( 

38 rdf_content={}, 

39 rdf_yaml_sha256="", 

40 status="untested", 

41 ) 

42 

43 with summary_path.open(encoding="utf-8") as f: 

44 data = json.load(f) 

45 

46 try: 

47 return CompatibilitySummary.model_validate(data) 

48 except ValidationError: 

49 return InitialSummary.model_validate(data)