Skip to content

Refactor codetool to use JSON input #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
7 changes: 4 additions & 3 deletions janis_core/code/pythontool.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ def prepare_file_or_directory_type(file_or_directory, value):
import argparse, json, sys
from typing import Optional, List, Dict, Any
cli = argparse.ArgumentParser("Argument parser for Janis PythonTool")
{nl.join(self.generate_cli_binding_for_input(inp) for inp in ins)}
cli.add_argument("--json", help="JSON file to parse")

{type_annotation_declarations}
{pt_decl}
Expand All @@ -189,8 +189,9 @@ def prepare_file_or_directory_type(file_or_directory, value):

try:
args = cli.parse_args()
result = code_block({argkwargs})
{extra_param_parsing}
with open(args.json) as fp:
d = json.load(fp)
result = code_block(**d)
print(json.dumps(result))
except Exception as e:
print(str(e), file=sys.stderr)
Expand Down
18 changes: 9 additions & 9 deletions janis_core/tests/test_python_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ def test_whole2(self):
test = CwlTranslator.translate_code_tool_internal(PythonEchoTool())
print(test)

def test_whole_cwl(self):
out = PythonEchoTool().translate("cwl", to_console=False)
print(out)

# def test_build_code_block(self):
# script = PythonEchoTool().prepared_script()
# print(script)
Expand Down Expand Up @@ -112,10 +116,7 @@ def test_whole2(self):
import argparse, json, sys
from typing import Optional, List, Dict, Any
cli = argparse.ArgumentParser("Argument parser for Janis PythonTool")
cli.add_argument("--name", type=str, required=True)
cli.add_argument("--infile", type=str, help='File to write to fout')
cli.add_argument("--flag", action='store_true', help='Random boolean')
cli.add_argument("--testvalue", type=str)
cli.add_argument("--json", help="JSON file to parse")

String = str
Filename = str
Expand Down Expand Up @@ -152,18 +153,17 @@ def code_block(name: str, infile: Filename, flag: bool = True, testvalue="test")

try:
args = cli.parse_args()
result = code_block(name=args.name, infile=args.infile, flag=args.flag, testvalue=args.testvalue)
with open(args.json) as fp:
d = json.load(fp)
result = code_block(**d)
print(json.dumps(result))
except Exception as e:
print(str(e), file=sys.stderr)
raise

EOT
python echo_tool-script.py \\
--name '~{name}' \\
--infile '~{select_first([infile, "generated"])}' \\
~{if defined(select_first([flag, true])) then "--flag" else ""} \\
~{if defined(select_first([testvalue, "test"])) then ("--testvalue '" + select_first([testvalue, "test"]) + "'") else ""}
--json ~{write_json({"name": name, "infile": infile, "flag": flag, "testvalue": testvalue})}
>>>
runtime {
disks: runtime_disks
Expand Down
28 changes: 25 additions & 3 deletions janis_core/translations/cwl.py
Original file line number Diff line number Diff line change
Expand Up @@ -682,21 +682,28 @@ def translate_code_tool_internal(
inputs=[],
outputs=[],
requirements=[],
arguments=[],
)

tool_cwl.arguments.append(
cwlgen.CommandLineBinding(valueFrom="inputs.json", prefix="--json")
)

tool_ins = tool.inputs()

tool_cwl.inputs.extend(
translate_tool_input(
ToolInput(
t.id(),
input_type=t.intype,
prefix=f"--{t.id()}",
# prefix=f"--{t.id()}",
default=t.default,
doc=t.doc.doc if t.doc else None,
),
inputsdict=inputsdict,
tool=tool,
)
for t in tool.inputs()
for t in tool_ins
)

for output in tool.tool_outputs():
Expand Down Expand Up @@ -724,13 +731,28 @@ def translate_code_tool_internal(
)
)

ins = "[" + ", ".join(f'"{k.id()}"' for k in tool_ins) + "]"

tool_cwl.requirements.append(
cwlgen.InitialWorkDirRequirement(
listing=[
cwlgen.Dirent(
entryname=scriptname,
entry=tool.prepared_script(SupportedTranslation.CWL),
)
),
cwlgen.Dirent(
entryname="inputs.json",
entry=f"""${{
var retval = {{}};
{ins}.forEach(function(k) {{
if (inputs[k] != null && inputs[k].path) {{
retval[k] = inputs[k].path;
}} else {{
retval[k] = inputs[k];
}}
}})
return JSON.stringify(retval);\n}}""",
),
]
)
)
Expand Down
20 changes: 15 additions & 5 deletions janis_core/translations/wdl.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ def translate_code_tool_internal(
f"numbers or an underscore)"
)

ins = cls.get_resource_override_inputs() + [
raw_ins = [
ToolInput(
t.id(),
input_type=t.intype,
Expand All @@ -447,6 +447,8 @@ def translate_code_tool_internal(
for t in tool.tool_inputs()
]

ins = cls.get_resource_override_inputs() + raw_ins

tr_ins = cls.translate_tool_inputs(ins)

outs = []
Expand Down Expand Up @@ -478,10 +480,18 @@ def translate_code_tool_internal(
)
)

command_ins = cls.build_command_from_inputs(ins)
bc = tool.base_command()
bcs = " ".join(bc) if isinstance(bc, list) else bc
commands.append(wdl.Task.Command(bcs, command_ins, []))
prepared_map = ", ".join(f'"{i.id()}": {i.id()}' for i in raw_ins)

tr_ins.append(
wdl.Input(
wdl.File,
"jsonFile__",
expression=f"write_json({{{prepared_map}}})",
requires_quotes=False,
)
)
command_ins = [wdl.Task.Command.CommandInput(f"--json '~{{jsonFile__}}'")]
commands.append(wdl.Task.Command(tool.base_command(), command_ins, []))

r = wdl.Task.Runtime()
if with_docker:
Expand Down