Skip to content

Commit 512b667

Browse files
committed
simple input object generation
1 parent 3a3c6b9 commit 512b667

File tree

1 file changed

+61
-1
lines changed

1 file changed

+61
-1
lines changed

cwltool/main.py

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from __future__ import absolute_import
44

55
import argparse
6+
import collections
67
import functools
78
import json
89
import logging
@@ -220,6 +221,9 @@ def arg_parser(): # type: () -> argparse.ArgumentParser
220221
parser.add_argument("--relax-path-checks", action="store_true",
221222
default=False, help="Relax requirements on path names to permit "
222223
"spaces and hash characters.", dest="relax_path_checks")
224+
exgroup.add_argument("--make-template", action="store_true",
225+
help="Generate a template input object")
226+
223227

224228
parser.add_argument("workflow", type=Text, nargs="?", default=None)
225229
parser.add_argument("job_order", nargs=argparse.REMAINDER)
@@ -439,6 +443,56 @@ def generate_parser(toolparser, tool, namemap, records):
439443

440444
return toolparser
441445

446+
def generate_example_input(inptype):
447+
defaults = { 'null': 'null',
448+
'Any': 'null',
449+
'boolean': False,
450+
'int': 0,
451+
'long': 0,
452+
'float': 0.1,
453+
'double': 0.1,
454+
'string': 'default_string',
455+
'File': { 'class': 'File',
456+
'path': 'default/file/path' },
457+
'Directory': { 'class': 'Directory',
458+
'path': 'default/directory/path' } }
459+
if (not isinstance(inptype, str) and
460+
not isinstance(inptype, collections.Mapping)
461+
and isinstance(inptype, collections.Iterable)):
462+
if len(inptype) == 2 and 'null' in inptype:
463+
inptype.remove('null')
464+
return generate_example_input(inptype[0])
465+
# TODO: indicate that this input is optional
466+
else:
467+
raise Exception("multi-types other than optional not yet supported"
468+
" for generating example input objects: %s"
469+
% inptype)
470+
if 'type' in inptype:
471+
if inptype['type'] == 'array':
472+
return [ generate_example_input(inptype['items']) ]
473+
elif inptype['type'] == 'enum':
474+
return 'valid_enum_value'
475+
# TODO: list valid values in a comment
476+
elif inptype['type'] == 'record':
477+
record = {}
478+
for field in inptype['fields']:
479+
record[shortname(field)] = generate_example_input(field)
480+
return record
481+
else:
482+
return defaults.get(inptype, 'custom_type')
483+
# TODO: support custom types, complex arrays
484+
485+
486+
def generate_input_template(tool):
487+
# type: (Process) -> Dict[Text, Any]
488+
template = {}
489+
for inp in tool.tool["inputs"]:
490+
name = shortname(inp["id"])
491+
inptype = inp["type"]
492+
template[name] = generate_example_input(inptype)
493+
return template
494+
495+
442496

443497
def load_job_order(args, t, stdin, print_input_deps=False, relative_deps=False,
444498
stdout=sys.stdout, make_fs_access=None, fetcher_constructor=None):
@@ -669,7 +723,8 @@ def main(argsl=None, # type: List[str]
669723
'validate': False,
670724
'enable_ga4gh_tool_registry': False,
671725
'ga4gh_tool_registries': [],
672-
'find_default_container': None
726+
'find_default_container': None,
727+
'make_template': False
673728
}):
674729
if not hasattr(args, k):
675730
setattr(args, k, v)
@@ -751,6 +806,11 @@ def main(argsl=None, # type: List[str]
751806

752807
tool = make_tool(document_loader, avsc_names, metadata, uri,
753808
makeTool, make_tool_kwds)
809+
if args.make_template:
810+
yaml.safe_dump(generate_input_template(tool), sys.stdout,
811+
default_flow_style=False, indent=4,
812+
block_seq_indent=2)
813+
return 0
754814

755815
if args.validate:
756816
return 0

0 commit comments

Comments
 (0)