Skip to content

Commit 0c90c93

Browse files
authored
Merge pull request #397 from chasejohnson3/output_directory
Output directory from output_file_template
2 parents 5e5e6b7 + eb67b4f commit 0c90c93

File tree

2 files changed

+107
-2
lines changed

2 files changed

+107
-2
lines changed

pydra/engine/helpers_file.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,7 @@ def template_update_single(field, inputs_dict, output_dir=None, spec_type="input
556556
based on the value from inputs_dict
557557
(checking the types of the fields, that have "output_file_template)"
558558
"""
559-
from .specs import File, MultiOutputFile
559+
from .specs import File, MultiOutputFile, Directory
560560

561561
if spec_type == "input":
562562
if field.type not in [str, ty.Union[str, bool]]:
@@ -574,7 +574,7 @@ def template_update_single(field, inputs_dict, output_dir=None, spec_type="input
574574
f"type of {field.name} is str, consider using Union[str, bool]"
575575
)
576576
elif spec_type == "output":
577-
if field.type not in [File, MultiOutputFile]:
577+
if field.type not in [File, MultiOutputFile, Directory]:
578578
raise Exception(
579579
f"output {field.name} should be a File, but {field.type} set as the type"
580580
)

pydra/engine/tests/test_shelltask.py

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
ShellSpec,
1414
SpecInfo,
1515
File,
16+
Directory,
1617
MultiOutputFile,
1718
MultiInputObj,
1819
)
@@ -2927,6 +2928,110 @@ def test_shell_cmd_outputspec_7b_error():
29272928
assert "has to have a callable" in str(e.value)
29282929

29292930

2931+
@pytest.mark.parametrize("results_function", [result_no_submitter, result_submitter])
2932+
def test_shell_cmd_outputspec_7c(tmpdir, plugin, results_function):
2933+
"""
2934+
customised output_spec, adding Directory to the output named by args
2935+
"""
2936+
2937+
def get_lowest_directory(directory_path):
2938+
return str(directory_path).replace(str(Path(directory_path).parents[0]), "")
2939+
2940+
cmd = "mkdir"
2941+
args = [f"{tmpdir}/dir1", f"{tmpdir}/dir2"]
2942+
2943+
my_output_spec = SpecInfo(
2944+
name="Output",
2945+
fields=[
2946+
(
2947+
"resultsDir",
2948+
attr.ib(
2949+
type=Directory,
2950+
metadata={
2951+
"output_file_template": "{args}",
2952+
"help_string": "output file",
2953+
},
2954+
),
2955+
)
2956+
],
2957+
bases=(ShellOutSpec,),
2958+
)
2959+
2960+
shelly = ShellCommandTask(
2961+
name="shelly",
2962+
executable=cmd,
2963+
args=args,
2964+
output_spec=my_output_spec,
2965+
resultsDir="outdir",
2966+
).split("args")
2967+
2968+
res = results_function(shelly, plugin)
2969+
for index, arg_dir in enumerate(args):
2970+
assert Path(Path(tmpdir) / Path(arg_dir)).exists() == True
2971+
assert get_lowest_directory(arg_dir) == f"/dir{index+1}"
2972+
2973+
2974+
@pytest.mark.parametrize("results_function", [result_no_submitter, result_submitter])
2975+
def test_shell_cmd_outputspec_7d(tmpdir, plugin, results_function):
2976+
"""
2977+
customised output_spec, adding Directory to the output named by input spec
2978+
"""
2979+
2980+
def get_lowest_directory(directory_path):
2981+
return str(directory_path).replace(str(Path(directory_path).parents[0]), "")
2982+
2983+
cmd = "mkdir"
2984+
2985+
my_input_spec = SpecInfo(
2986+
name="Input",
2987+
fields=[
2988+
(
2989+
"resultsDir",
2990+
attr.ib(
2991+
type=str,
2992+
metadata={
2993+
"position": 1,
2994+
"help_string": "new directory",
2995+
"argstr": "",
2996+
},
2997+
),
2998+
)
2999+
],
3000+
bases=(ShellSpec,),
3001+
)
3002+
3003+
my_output_spec = SpecInfo(
3004+
name="Output",
3005+
fields=[
3006+
(
3007+
"resultsDir",
3008+
attr.ib(
3009+
type=Directory,
3010+
metadata={
3011+
"output_file_template": "{resultsDir}",
3012+
"help_string": "output file",
3013+
},
3014+
),
3015+
)
3016+
],
3017+
bases=(ShellOutSpec,),
3018+
)
3019+
3020+
shelly = ShellCommandTask(
3021+
name="shelly",
3022+
executable=cmd,
3023+
input_spec=my_input_spec,
3024+
output_spec=my_output_spec,
3025+
resultsDir=Path(tmpdir) / Path("test"),
3026+
)
3027+
3028+
res = results_function(shelly, plugin)
3029+
assert (Path(tmpdir) / Path("test")).exists() == True
3030+
assert get_lowest_directory(res.output.resultsDir) == get_lowest_directory(
3031+
Path(tmpdir) / Path("test")
3032+
)
3033+
3034+
29303035
@pytest.mark.parametrize("results_function", [result_no_submitter, result_submitter])
29313036
def test_shell_cmd_state_outputspec_1(plugin, results_function, tmpdir):
29323037
"""

0 commit comments

Comments
 (0)