Skip to content
This repository was archived by the owner on Jun 9, 2020. It is now read-only.

Commit cdd436a

Browse files
authored
Merge pull request #13 from illusional/optional-types
Optional types
2 parents 52df2b3 + d1a07cd commit cdd436a

File tree

3 files changed

+52
-6
lines changed

3 files changed

+52
-6
lines changed

cwlgen/elements.py

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,41 @@
1010
'draft-3.dev4', 'draft-3.dev5', 'draft-3', 'draft-4.dev1',
1111
'draft-4.dev2', 'draft-4.dev3', 'v1.0.dev4', 'v1.0']
1212
DEF_VERSION = 'v1.0'
13+
NON_NULL_CWL_TYPE = ['boolean', 'int', 'long', 'float', 'double', 'string', 'File',
14+
'Directory', 'stdout']
1315
CWL_TYPE = ['null', 'boolean', 'int', 'long', 'float', 'double', 'string', 'File',
1416
'Directory', 'stdout', None]
1517
DEF_TYPE = 'null'
1618

1719

20+
def parse_param_type(param_type):
21+
"""
22+
Parses the parameter type as one of the required types:
23+
:: https://www.commonwl.org/v1.0/CommandLineTool.html#CommandInputParameter
24+
25+
26+
:param param_type:
27+
:return: CWLType | CommandInputRecordSchema | CommandInputEnumSchema | CommandInputArraySchema | string |
28+
array<CWLType | CommandInputRecordSchema | CommandInputEnumSchema | CommandInputArraySchema | string>
29+
"""
30+
31+
if isinstance(param_type, str) and len(param_type) > 0:
32+
# Must be CWLType
33+
optional = param_type[-1] == "?"
34+
if optional:
35+
_LOGGER.debug("Detected {param_type} to be optional".format(param_type=param_type))
36+
cwltype = param_type[:-1] if optional else param_type
37+
if cwltype not in CWL_TYPE:
38+
_LOGGER.warning("The type '{param_type}' is not a valid CWLType, expected one of: {types}"
39+
.format(param_type=param_type, types=", ".join(str(x) for x in CWL_TYPE)))
40+
_LOGGER.warning("type is set to {}.".format(DEF_TYPE))
41+
return DEF_TYPE
42+
return param_type
43+
else:
44+
_LOGGER.warning("Unable to detect type of param '{param_type}".format(param_type=param_type))
45+
return DEF_TYPE
46+
47+
1848
class Parameter(object):
1949
'''
2050
Based class for parameters (common field of Input and Output) for CommandLineTool and Workflow
@@ -40,17 +70,13 @@ def __init__(self, param_id, label=None, secondary_files=None, param_format=None
4070
:param param_type: type of data assigned to the parameter
4171
:type param_type: STRING corresponding to CWLType
4272
'''
43-
if param_type not in CWL_TYPE:
44-
_LOGGER.warning("The type {} is incorrect for the parameter.".format(param_type))
45-
_LOGGER.warning("type is set to {}.".format(DEF_TYPE))
46-
param_type = DEF_TYPE
4773
self.id = param_id
4874
self.label = label
4975
self.secondaryFiles = secondary_files
5076
self.format = param_format
5177
self.streamable = streamable
5278
self.doc = doc
53-
self.type = param_type
79+
self.type = parse_param_type(param_type)
5480

5581
def get_dict(self):
5682
'''

test/test_unit_typing.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import unittest
2+
from cwlgen.elements import parse_param_type, NON_NULL_CWL_TYPE, CWL_TYPE, DEF_TYPE
3+
import logging
4+
5+
6+
class TestParamTyping(unittest.TestCase):
7+
8+
def test_types(self):
9+
for cwl_type in NON_NULL_CWL_TYPE:
10+
self.assertEqual(parse_param_type(cwl_type), cwl_type)
11+
12+
def test_incorrect_type(self):
13+
for cwl_type in NON_NULL_CWL_TYPE:
14+
should_be_def_type = parse_param_type(cwl_type.upper())
15+
self.assertNotEqual(should_be_def_type, cwl_type)
16+
self.assertEqual(should_be_def_type, DEF_TYPE)
17+
18+
def test_optional_string(self):
19+
for cwl_type in NON_NULL_CWL_TYPE:
20+
optional_type = cwl_type + "?"
21+
self.assertEqual(parse_param_type(optional_type), optional_type)

test/test_unit_workflow.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ def test_generates_workflow_int_inputs(self):
8585
generated = self.capture_tempfile(w.export)
8686
self.assertEqual(expected, generated)
8787

88-
8988
def test_add_requirements(self):
9089
w = cwlgen.Workflow()
9190
req = cwlgen.InlineJavascriptReq()

0 commit comments

Comments
 (0)