Skip to content

Commit

Permalink
feat: add env objects and missing filters
Browse files Browse the repository at this point in the history
  • Loading branch information
nichmor committed Aug 1, 2024
1 parent af23b01 commit 0def0e1
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
29 changes: 29 additions & 0 deletions src/rattler_build_conda_compat/jinja/filters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from __future__ import annotations

from rattler_build_conda_compat.jinja.utils import _MissingUndefined


def _version_to_build_string(some_string: str | _MissingUndefined) -> str:
"""
Converts some version by removing the . character and returning only the first two elements of the version.
If piped value is undefined, it returns the undefined value as is.
"""
if isinstance(some_string, _MissingUndefined):
inner_value = f"{some_string._undefined_name} | version_to_build_string" # noqa: SLF001
return f"${{{{ {inner_value} }}}}"
# We first split the string by whitespace and take the first part
split = some_string.split()[0] if some_string.split() else some_string
# We then split the string by . and take the first two parts
parts = split.split(".")
major = parts[0] if len(parts) > 0 else ""
minor = parts[1] if len(parts) > 1 else ""
return f"{major}{minor}"


def _bool(value: str) -> bool:
return bool(value)


def _split(s: str, sep: str = " ") -> list[str]:
"""Filter that split a string by a separator"""
return s.split(sep)
2 changes: 1 addition & 1 deletion src/rattler_build_conda_compat/jinja/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def get(self, env_var: str, default: str | None = None) -> str:

return f"""${{{{ env.get("{env_var}")}}}}"""

def exists(self, env_var: str) -> bool:
def exists(self, env_var: str) -> str:
return f"${{{{ env.exists('{env_var}') }}}}"


Expand Down
11 changes: 11 additions & 0 deletions src/rattler_build_conda_compat/jinja/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from jinja2 import DebugUndefined


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__()}"

0 comments on commit 0def0e1

Please sign in to comment.