Skip to content

Commit efeb68a

Browse files
committed
feat: add jinja2 template renderer
Signed-off-by: James McCorrie <[email protected]>
1 parent e6ab190 commit efeb68a

File tree

4 files changed

+50
-1
lines changed

4 files changed

+50
-1
lines changed

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ dependencies = [
2020
"click>=8.1.7",
2121
"enlighten>=1.12.4",
2222
"hjson>=3.1.0",
23+
"jinja2>=3.1.6",
2324
"logzero>=1.7.0",
2425
"mistletoe>=1.4.0",
2526
"premailer>=3.10.0",

src/dvsim/templates/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Copyright lowRISC contributors (OpenTitan project).
2+
# Licensed under the Apache License, Version 2.0, see LICENSE for details.
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
"""Templates for DVSim."""

src/dvsim/templates/render.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Copyright lowRISC contributors (OpenTitan project).
2+
# Licensed under the Apache License, Version 2.0, see LICENSE for details.
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
"""Render templates for use with report generation.
6+
7+
This directory is also the parent directory containing templates for use with
8+
DVSim. Templates can be referenced relative to this directory.
9+
"""
10+
11+
from collections.abc import Mapping
12+
13+
from jinja2 import Environment, PackageLoader, select_autoescape
14+
15+
__all__ = ("render_template",)
16+
17+
_env: Environment | None = None
18+
19+
20+
def render_template(path: str, data: Mapping[str, object] | None = None) -> str:
21+
"""Render a template packaged with DVSim.
22+
23+
Args:
24+
path: relative path to the DVSim template directory
25+
data: mapping of key/value pairs to send to the template renderer
26+
27+
Returns:
28+
string containing the rendered template
29+
30+
"""
31+
global _env
32+
33+
if _env is None:
34+
_env = Environment(
35+
loader=PackageLoader("dvsim"),
36+
autoescape=select_autoescape(),
37+
)
38+
39+
template = _env.get_template(path)
40+
41+
return template.render(data)

uv.lock

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)