Skip to content

Commit 1075312

Browse files
authored
Merge pull request #462 from common-workflow-language/generate_input_object
simple input object generation
2 parents 3a3c6b9 + 4bb5e47 commit 1075312

File tree

1 file changed

+63
-1
lines changed

1 file changed

+63
-1
lines changed

cwltool/main.py

Lines changed: 63 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,58 @@ def generate_parser(toolparser, tool, namemap, records):
439443

440444
return toolparser
441445

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

443499
def load_job_order(args, t, stdin, print_input_deps=False, relative_deps=False,
444500
stdout=sys.stdout, make_fs_access=None, fetcher_constructor=None):
@@ -669,7 +725,8 @@ def main(argsl=None, # type: List[str]
669725
'validate': False,
670726
'enable_ga4gh_tool_registry': False,
671727
'ga4gh_tool_registries': [],
672-
'find_default_container': None
728+
'find_default_container': None,
729+
'make_template': False
673730
}):
674731
if not hasattr(args, k):
675732
setattr(args, k, v)
@@ -751,6 +808,11 @@ def main(argsl=None, # type: List[str]
751808

752809
tool = make_tool(document_loader, avsc_names, metadata, uri,
753810
makeTool, make_tool_kwds)
811+
if args.make_template:
812+
yaml.safe_dump(generate_input_template(tool), sys.stdout,
813+
default_flow_style=False, indent=4,
814+
block_seq_indent=2)
815+
return 0
754816

755817
if args.validate:
756818
return 0

0 commit comments

Comments
 (0)