Coverage for src / competitive_verifier_resources / resources.py: 90%

23 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-06-05 08:59 +0900

1from __future__ import annotations 

2 

3import importlib.resources 

4from typing import TYPE_CHECKING, Protocol, cast 

5 

6if TYPE_CHECKING: 

7 import pathlib 

8 from collections.abc import Iterable, Iterator 

9 

10_DOC_USAGE_PATH = "doc_usage.txt" 

11 

12 

13class TraversableResource(Protocol): 

14 @property 

15 def name(self) -> str: ... 

16 

17 def iterdir(self) -> Iterable[TraversableResource]: ... 

18 

19 def is_dir(self) -> bool: ... 

20 

21 def is_file(self) -> bool: ... 

22 

23 def read_bytes(self) -> bytes: ... 

24 

25 def read_text(self, encoding: str | None = None) -> str: ... 

26 

27 def __truediv__(self, child: str) -> TraversableResource: ... 

28 

29 

30_ROOT = cast("TraversableResource", importlib.resources.files(cast("str", __package__))) 

31 

32 

33def doc_usage( 

34 *, 

35 markdown_dir_path: pathlib.Path, 

36 repo_name: str, 

37 sample_repo_name: str, 

38) -> str: 

39 template = _ROOT / _DOC_USAGE_PATH 

40 return ( 

41 template.read_text(encoding="utf-8") 

42 .replace("{{{{{markdown_dir_path}}}}}", markdown_dir_path.as_posix()) 

43 .replace("{{{{{repository}}}}}", repo_name) 

44 .replace("{{{{{sample_repository}}}}}", sample_repo_name) 

45 ) 

46 

47 

48def _walk_files( 

49 root: TraversableResource, 

50 prefix: str = "", 

51) -> Iterator[tuple[str, TraversableResource]]: 

52 for child in root.iterdir(): 

53 rel = f"{prefix}{child.name}" 

54 if child.is_file(): 

55 yield rel, child 

56 elif child.is_dir(): 56 ↛ 52line 56 didn't jump to line 52 because the condition on line 56 was always true

57 yield from _walk_files(child, f"{rel}/") 

58 

59 

60def jekyll_files() -> dict[str, bytes]: 

61 root = _ROOT / "jekyll" 

62 return {path: resource.read_bytes() for path, resource in _walk_files(root)} 

63 

64 

65def jekyll_theme_override_files(theme_name: str) -> dict[str, bytes]: 

66 root = _ROOT / "jekyll_theme_overrides" / theme_name 

67 

68 if not root.is_dir(): 68 ↛ 69line 68 didn't jump to line 69 because the condition on line 68 was never true

69 return {} 

70 

71 return {path: resource.read_bytes() for path, resource in _walk_files(root)}