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

Commit a968c5d

Browse files
authored
Merge pull request #1 from illusional/cwltypes-class-conversion
Adds the ability to refer to individual CwlTypes through CwlTypes class
2 parents 0d28a6e + d90feb9 commit a968c5d

File tree

2 files changed

+30
-17
lines changed

2 files changed

+30
-17
lines changed

cwlgen/elements.py

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,24 @@
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']
15-
CWL_TYPE = ['null', 'boolean', 'int', 'long', 'float', 'double', 'string', 'File',
16-
'Directory', 'stdout', None]
17-
DEF_TYPE = 'null'
13+
14+
15+
class CwlTypes:
16+
DEF_TYPE = "null"
17+
18+
NULL = "null"
19+
BOOLEAN = "boolean"
20+
INT = "integer"
21+
LONG = "long"
22+
FLOAT = "float"
23+
DOUBLE = "double"
24+
STRING = "string"
25+
FILE = "File"
26+
DIRECTORY = "Directory"
27+
STDOUT = "stdout"
28+
29+
NON_NULL_TYPES = [BOOLEAN, INT, LONG, FLOAT, DOUBLE, STRING, FILE, DIRECTORY, STDOUT]
30+
TYPES = [NULL, None, BOOLEAN, INT, LONG, FLOAT, DOUBLE, STRING, FILE, DIRECTORY, STDOUT]
1831

1932

2033
def parse_type(param_type, requires_type=False):
@@ -46,13 +59,13 @@ def parse_type(param_type, requires_type=False):
4659
if len(cwltype) > 2 and cwltype[-2:] == "[]":
4760
array_type = CommandInputArraySchema(items=cwltype[:-2])
4861
# How to make arrays optional input: https://www.biostars.org/p/233562/#234089
49-
return [DEF_TYPE, array_type] if optional else array_type
62+
return [CwlTypes.DEF_TYPE, array_type] if optional else array_type
5063

51-
if cwltype not in CWL_TYPE:
64+
if cwltype not in CwlTypes.TYPES:
5265
_LOGGER.warning("The type '{param_type}' is not a valid CWLType, expected one of: {types}"
53-
.format(param_type=param_type, types=", ".join(str(x) for x in CWL_TYPE)))
54-
_LOGGER.warning("type is set to {}.".format(DEF_TYPE))
55-
return DEF_TYPE
66+
.format(param_type=param_type, types=", ".join(str(x) for x in CwlTypes.TYPES)))
67+
_LOGGER.warning("type is set to {}.".format(CwlTypes.DEF_TYPE))
68+
return CwlTypes.DEF_TYPE
5669
return param_type
5770

5871
elif isinstance(param_type, list):
@@ -62,7 +75,7 @@ def parse_type(param_type, requires_type=False):
6275
return param_type # validate if required
6376
else:
6477
_LOGGER.warning("Unable to detect type of param '{param_type}'".format(param_type=param_type))
65-
return DEF_TYPE
78+
return CwlTypes.DEF_TYPE
6679

6780

6881
def get_type_dict(param_type):

test/test_unit_typing.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
import unittest
2-
from cwlgen.elements import parse_type, NON_NULL_CWL_TYPE, CWL_TYPE, DEF_TYPE, CommandInputArraySchema
2+
from cwlgen.elements import parse_type, CwlTypes, CommandInputArraySchema
33
import logging
44

55

66
class TestParamTyping(unittest.TestCase):
77

88
def test_types(self):
9-
for cwl_type in NON_NULL_CWL_TYPE:
9+
for cwl_type in CwlTypes.NON_NULL_TYPES:
1010
self.assertEqual(parse_type(cwl_type), cwl_type)
1111

1212
def test_incorrect_type(self):
1313
invalid_type = "invalid"
1414
should_be_def_type = parse_type(invalid_type)
1515
self.assertNotEqual(should_be_def_type, invalid_type)
16-
self.assertEqual(should_be_def_type, DEF_TYPE)
16+
self.assertEqual(should_be_def_type, CwlTypes.DEF_TYPE)
1717

1818
def test_optional_string(self):
19-
for cwl_type in NON_NULL_CWL_TYPE:
19+
for cwl_type in CwlTypes.NON_NULL_TYPES:
2020
optional_type = cwl_type + "?"
2121
self.assertEqual(parse_type(optional_type), optional_type)
2222

@@ -31,7 +31,7 @@ def test_incorrectly_typed_array(self):
3131
q = parse_type(array_string_type)
3232
self.assertIsInstance(q, CommandInputArraySchema)
3333
self.assertNotEqual(q.items, "invalid")
34-
self.assertEqual(q.items, DEF_TYPE)
34+
self.assertEqual(q.items, CwlTypes.DEF_TYPE)
3535

3636
def test_optionally_typed_array(self):
3737
array_string_type = "string?[]"
@@ -44,7 +44,7 @@ def test_optional_typed_array(self):
4444
q = parse_type(optional_array_string_type)
4545
self.assertIsInstance(q, list)
4646
self.assertEqual(len(q), 2)
47-
null_idx = q.index(DEF_TYPE)
47+
null_idx = q.index(CwlTypes.DEF_TYPE)
4848
array_type = q[1 - null_idx]
4949
self.assertIsInstance(array_type, CommandInputArraySchema)
5050
self.assertEqual(array_type.items, "string")

0 commit comments

Comments
 (0)