Skip to content

Commit

Permalink
Minor additions and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
nx10 committed May 20, 2024
1 parent 879c794 commit 9af78f3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
9 changes: 4 additions & 5 deletions src/styx/compiler/compile/inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ def _bt_template_str_parse(
"""Parse a Boutiques command line template string into segments."""
bt_template_str = boutiques_split_command(input_command_line_template)

bt_id_inputs = {input_.data.template_key: input_ for input_ in inputs}
template_key_inputs = {input_.data.template_key: input_ for input_ in inputs}

segments: list[list[str | WithSymbol[InputArgument]]] = []

Expand All @@ -250,13 +250,12 @@ def _bt_template_str_parse(
token = stack.pop()
if isinstance(token, str):
any_match = False
for _, bt_input in bt_id_inputs.items():
value_key = bt_input.data.internal_id
if value_key == token:
for template_key, bt_input in template_key_inputs.items():
if template_key == token:
stack.append(bt_input)
any_match = True
break
o = token.split(value_key, 1)
o = token.split(template_key, 1)
if len(o) == 2:
stack.append(o[0])
stack.append(bt_input)
Expand Down
30 changes: 25 additions & 5 deletions src/styx/compiler/compile/outputs.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from styx.compiler.compile.common import SharedScopes, SharedSymbols
from styx.model.core import InputArgument, OutputArgument, WithSymbol
from styx.model.core import InputArgument, InputTypePrimitive, OutputArgument, WithSymbol
from styx.pycodegen.core import PyFunc, PyModule, indent
from styx.pycodegen.utils import as_py_literal, enbrace, enquote

Expand All @@ -18,6 +18,8 @@ def generate_outputs_definition(
'"""',
f"Output object returned when calling `{symbols.function}(...)`.",
'"""',
"root: OutputPathType",
'"""Output root folder. This is the root folder for all outputs."""',
]),
])
for out in outputs:
Expand Down Expand Up @@ -54,16 +56,34 @@ def generate_output_building(

func.body.append(f"{symbols.ret} = {symbols.output_class}(")

# Set root output path
func.body.extend(indent([f'root={symbols.execution}.output_file("."),']))

for out in outputs:
strip_extensions = out.data.stripped_file_extensions is not None
if out.data.path_template is not None:
s = out.data.path_template
for a in inputs:
for input_ in inputs:
if input_.data.template_key not in s:
continue

substitute = input_.symbol

if input_.data.type.primitive == InputTypePrimitive.File:
# Just use the stem of the file
# This is commonly used when output files 'inherit' the name of an input file
substitute = f"pathlib.Path({substitute}).stem"
elif input_.data.type.primitive != InputTypePrimitive.String:
raise Exception(
f"Unsupported input type {input_.data.type.primitive} "
f"for output path template of '{out.data.name}'."
)

if strip_extensions:
exts = as_py_literal(out.data.stripped_file_extensions, "'")
s = s.replace(f"{a.data.internal_id}", enbrace(f"{py_rstrip_fun}({a.symbol}, {exts})"))
else:
s = s.replace(f"{a.data.internal_id}", enbrace(a.symbol))
substitute = f"{py_rstrip_fun}({substitute}, {exts})"

s = s.replace(input_.data.template_key, enbrace(substitute))

s_optional = ", optional=True" if out.data.optional else ""

Expand Down

0 comments on commit 9af78f3

Please sign in to comment.