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
« prev ^ index » next coverage.py v7.13.1, created at 2026-06-05 08:59 +0900
1from __future__ import annotations
3import importlib.resources
4from typing import TYPE_CHECKING, Protocol, cast
6if TYPE_CHECKING:
7 import pathlib
8 from collections.abc import Iterable, Iterator
10_DOC_USAGE_PATH = "doc_usage.txt"
13class TraversableResource(Protocol):
14 @property
15 def name(self) -> str: ...
17 def iterdir(self) -> Iterable[TraversableResource]: ...
19 def is_dir(self) -> bool: ...
21 def is_file(self) -> bool: ...
23 def read_bytes(self) -> bytes: ...
25 def read_text(self, encoding: str | None = None) -> str: ...
27 def __truediv__(self, child: str) -> TraversableResource: ...
30_ROOT = cast("TraversableResource", importlib.resources.files(cast("str", __package__)))
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 )
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}/")
60def jekyll_files() -> dict[str, bytes]:
61 root = _ROOT / "jekyll"
62 return {path: resource.read_bytes() for path, resource in _walk_files(root)}
65def jekyll_theme_override_files(theme_name: str) -> dict[str, bytes]:
66 root = _ROOT / "jekyll_theme_overrides" / theme_name
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 {}
71 return {path: resource.read_bytes() for path, resource in _walk_files(root)}