Skip to content

Commit

Permalink
Add string example value filter
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisvanrun committed Jan 17, 2025
1 parent 4349c72 commit 62520e0
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 3 deletions.
6 changes: 6 additions & 0 deletions grand_challenge_forge/generation_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ def has_example_value(component_interface):
)


def escape_strings(arg):
if isinstance(arg, str):
return json.dumps(arg)
return arg


def create_civ_stub_file(*, target_path, component_interface):
"""Creates a stub based on a component interface"""
target_path.parent.mkdir(parents=True, exist_ok=True)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def run():
# For now, let us make bogus predictions
{%- for ci in algorithm.outputs %}
output_{{ ci.slug.replace("-", "_")}} =
{%- if ci | has_example_value %} {{ ci.example_value }}
{%- if ci | has_example_value %} {{ ci.example_value | escape_strings }}
{%- elif ci | is_image %} numpy.eye(4, 2)
{%- elif ci | is_json %} {"content": "should match the required format"}
{%- elif ci | is_file %} "content: should match the required format"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def run():
# For now, let us make bogus predictions
{%- for ci in phase.algorithm_outputs %}
output_{{ ci.slug.replace("-", "_")}} =
{%- if ci | has_example_value %} {{ ci.example_value }}
{%- if ci | has_example_value %} {{ ci.example_value | escape_strings }}
{%- elif ci | is_image %} numpy.eye(4, 2)
{%- elif ci | is_json %} {"content": "should match the required format"}
{%- elif ci | is_file %} "content: should match the required format"
Expand Down
5 changes: 5 additions & 0 deletions grand_challenge_forge/partials/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,8 @@ def has_file(arg):
@register_simple_filter
def has_example_value(arg):
return generation_utils.has_example_value(arg)


@register_simple_filter
def escape_strings(arg):
return generation_utils.escape_strings(arg)
2 changes: 1 addition & 1 deletion tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@
"kind": "Anything",
"super_kind": "File",
"relative_path": "yet-another-non-json-output-value",
"example_value": None
"example_value": "A string that needs to be escaped"
}
]
}
Expand Down

0 comments on commit 62520e0

Please sign in to comment.