-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
initial jinja rendering in Python (#34)
Co-authored-by: nichmor <[email protected]>
- Loading branch information
Showing
9 changed files
with
1,152 additions
and
1,944 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,7 @@ jobs: | |
- uses: prefix-dev/[email protected] | ||
with: | ||
pixi-version: "latest" | ||
environments: lint | ||
environments: type-checking | ||
|
||
- name: type check | ||
run: | | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
from __future__ import annotations | ||
|
||
from typing import Any, TypedDict | ||
|
||
import jinja2 | ||
import yaml | ||
from jinja2 import DebugUndefined | ||
|
||
from rattler_build_conda_compat.loader import load_yaml | ||
|
||
|
||
class RecipeWithContext(TypedDict, total=False): | ||
context: dict[str, str] | ||
|
||
|
||
class _MissingUndefined(DebugUndefined): | ||
def __str__(self) -> str: | ||
""" | ||
By default, `DebugUndefined` return values in the form `{{ value }}`. | ||
`rattler-build` has a different syntax, so we need to override this method, | ||
and return the value in the form `${{ value }}`. | ||
""" | ||
return f"${super().__str__()}" | ||
|
||
|
||
def jinja_env() -> jinja2.Environment: | ||
""" | ||
Create a `rattler-build` specific Jinja2 environment with modified syntax. | ||
""" | ||
return jinja2.Environment( | ||
variable_start_string="${{", | ||
variable_end_string="}}", | ||
trim_blocks=True, | ||
lstrip_blocks=True, | ||
autoescape=True, | ||
undefined=_MissingUndefined, | ||
) | ||
|
||
|
||
def load_recipe_context(context: dict[str, str], jinja_env: jinja2.Environment) -> dict[str, str]: | ||
""" | ||
Load all string values from the context dictionary as Jinja2 templates. | ||
""" | ||
# Process each key-value pair in the dictionary | ||
for key, value in context.items(): | ||
# If the value is a string, render it as a template | ||
if isinstance(value, str): | ||
template = jinja_env.from_string(value) | ||
rendered_value = template.render(context) | ||
context[key] = rendered_value | ||
|
||
return context | ||
|
||
|
||
def render_recipe_with_context(recipe_content: RecipeWithContext) -> dict[str, Any]: | ||
""" | ||
Render the recipe using known values from context section. | ||
Unknown values are not evaluated and are kept as it is. | ||
Examples: | ||
--- | ||
```python | ||
>>> from pathlib import Path | ||
>>> from rattler_build_conda_compat.loader import load_yaml | ||
>>> recipe_content = load_yaml((Path().resolve() / "tests" / "data" / "eval_recipe_using_context.yaml").read_text()) | ||
>>> evaluated_context = render_recipe_with_context(recipe_content) | ||
>>> assert "my_value-${{ not_present_value }}" == evaluated_context["build"]["string"] | ||
>>> | ||
``` | ||
""" | ||
env = jinja_env() | ||
context = recipe_content.get("context", {}) | ||
# load all context templates | ||
context_templates = load_recipe_context(context, env) | ||
|
||
# render the rest of the document with the values from the context | ||
# and keep undefined expressions _as is_. | ||
template = env.from_string(yaml.dump(recipe_content)) | ||
rendered_content = template.render(context_templates) | ||
return load_yaml(rendered_content) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# serializer version: 1 | ||
# name: test_render_context | ||
''' | ||
build: | ||
string: ${{ blas_variant }}${{ hash }}_foo-bla | ||
context: | ||
name: foo | ||
name_version: foo-bla | ||
version: bla | ||
package: | ||
name: foo | ||
version: bla | ||
|
||
''' | ||
# --- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
context: | ||
name: "foo" | ||
version: "bla" | ||
name_version: ${{ name }}-${{ version }} | ||
|
||
package: | ||
name: ${{ name }} | ||
version: ${{ version }} | ||
|
||
build: | ||
string: ${{ blas_variant }}${{ hash }}_${{ name_version }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
context: | ||
name: "my_value" | ||
build: | ||
string: ${{ name }}-${{ not_present_value }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from pathlib import Path | ||
|
||
import yaml | ||
from rattler_build_conda_compat.jinja import load_yaml, render_recipe_with_context | ||
|
||
|
||
def test_render_recipe_with_context(snapshot) -> None: | ||
recipe = Path("tests/data/context.yaml") | ||
with recipe.open() as f: | ||
recipe_yaml = load_yaml(f) | ||
|
||
rendered = render_recipe_with_context(recipe_yaml) | ||
into_yaml = yaml.dump(rendered) | ||
|
||
assert into_yaml == snapshot |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters