Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,581 changes: 0 additions & 1,581 deletions src/specify_cli/workflows/expressions.py

This file was deleted.

63 changes: 63 additions & 0 deletions src/specify_cli/workflows/expressions/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
"""Sandboxed expression evaluator for workflow templates.

Provides a safe Jinja2 subset for evaluating expressions in workflow YAML.
Templates cannot perform file I/O, import modules, or run arbitrary code —
the evaluator only walks the namespace and applies a fixed set of filters.

This package is split by responsibility:

- ``namespace``/``filters``/``operators``/``template`` — shared leaves;
- ``engine_current`` — the hand-written evaluator behind the contract;
- ``engine`` — the selection point (the one line that swaps the engine);
- ``api`` — the public evaluation entry points;
- ``diagnostics`` — condition validation and remediation.

This ``__init__`` is a logic-free facade that keeps the historical
``specify_cli.workflows.expressions`` surface — including the private names the
tests import — working unchanged.
"""

from __future__ import annotations

from .api import evaluate_condition, evaluate_expression
from .diagnostics import (
condition_has_malformed_expression_block,
condition_is_interpolated_to_text,
condition_is_never_evaluated,
format_condition_correction,
format_condition_remediation,
_has_unbalanced_bracket,
_has_unbalanced_quote,
_unresolvable_term,
)
from .engine import (
_COMPARISON_OPERATORS,
_WORD_OPERATORS,
_collect_leaves,
_evaluator_rejects,
_has_incomplete_operand,
_leaf_sink,
)
from .filters import _REGISTERED_FILTERS
from .template import _strip_stray_delimiters

__all__ = [
"condition_has_malformed_expression_block",
"condition_is_interpolated_to_text",
"condition_is_never_evaluated",
"evaluate_condition",
"evaluate_expression",
"format_condition_correction",
"format_condition_remediation",
"_COMPARISON_OPERATORS",
"_REGISTERED_FILTERS",
"_WORD_OPERATORS",
"_collect_leaves",
"_evaluator_rejects",
"_has_incomplete_operand",
"_has_unbalanced_bracket",
"_has_unbalanced_quote",
"_leaf_sink",
"_strip_stray_delimiters",
"_unresolvable_term",
]
70 changes: 70 additions & 0 deletions src/specify_cli/workflows/expressions/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
"""Public evaluation API for workflow expression templates.

The API layer owns namespace construction and nothing else: the whole
single-expression-vs-interpolation decision lives in the engine contract
(``_evaluate_template``), reached only through ``engine.py``.
"""

from __future__ import annotations

from typing import Any

from .engine import _evaluate_template
from .namespace import _build_namespace


def evaluate_expression(template: str, context: Any) -> Any:
"""Evaluate a template string with ``{{ ... }}`` expressions.

If the entire string is a single expression, returns the raw value
(preserving type). Otherwise, substitutes each expression inline
and returns a string.

Parameters
----------
template:
The template string (e.g., ``"{{ steps.plan.output.task_count }}"``
or ``"Processed {{ inputs.spec }}"``.
context:
A ``StepContext`` or compatible object.

Returns
-------
The resolved value (any type for single-expression templates,
string for multi-expression or mixed templates).
"""
if not isinstance(template, str):
return template

namespace = _build_namespace(context)
return _evaluate_template(template, namespace)


def evaluate_condition(condition: str, context: Any) -> bool:
"""Evaluate a condition expression and return a boolean.

Convenience wrapper around ``evaluate_expression`` that coerces
the result to bool.
"""
result = evaluate_expression(condition, context)
# Treat plain "false"/"true" strings as booleans so that
# condition: "false" (without {{ }}) behaves as expected.
#
# Strip before matching: the string a condition resolves to is most often
# captured command output, and a ``shell`` step stores ``proc.stdout``
# verbatim, so ``run: echo false`` resolves to ``"false\n"``. Without the
# strip that trailing newline matches neither branch and falls through to
# ``bool("false\n")`` -> True, silently taking an ``if`` step's ``then``
# branch (and keeping a ``while``/``do-while`` looping) on a step that
# printed "false". A workflow cannot strip it itself -- no registered
# filter trims whitespace (there is no ``trim``).
# ``InitStep._resolve_bool`` and the catalog readers already strip before
# matching boolean text. ``bool(result)`` below still sees the raw string,
# so no non-boolean text changes truthiness.
if isinstance(result, str):
lower = result.strip().lower()
if lower == "false":
return False
if lower == "true":
return True
return bool(result)
Loading
Loading