-
Notifications
You must be signed in to change notification settings - Fork 32
/
template.py
68 lines (47 loc) · 1.81 KB
/
template.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
from importlib import resources
import jinja2
from jinja2.sandbox import SandboxedEnvironment
from coverage_comment import coverage as coverage_module
MARKER = """<!-- This comment was produced by python-coverage-comment-action -->"""
class CommentLoader(jinja2.BaseLoader):
def __init__(self, base_template: str, custom_template: str | None):
self.base_template = base_template
self.custom_template = custom_template
def get_source(
self, environment: jinja2.Environment, template: str
) -> tuple[str, str | None, bool]:
if template == "base":
return self.base_template, None, True
if self.custom_template and template == "custom":
return self.custom_template, None, True
raise jinja2.TemplateNotFound(template)
class MissingMarker(Exception):
pass
class TemplateError(Exception):
pass
def get_markdown_comment(
coverage: coverage_module.Coverage,
diff_coverage: coverage_module.DiffCoverage,
previous_coverage_rate: float | None,
base_template: str,
custom_template: str | None = None,
):
loader = CommentLoader(base_template=base_template, custom_template=custom_template)
env = SandboxedEnvironment(loader=loader)
env.filters["pct"] = pct
try:
comment = env.get_template("custom" if custom_template else "base").render(
previous_coverage_rate=previous_coverage_rate,
coverage=coverage,
diff_coverage=diff_coverage,
marker=MARKER,
)
except jinja2.exceptions.TemplateError as exc:
raise TemplateError from exc
if MARKER not in comment:
raise MissingMarker()
return comment
def read_template_file() -> str:
return resources.read_text("coverage_comment", "default.md.j2")
def pct(val):
return f"{val:.0%}"