8000 get_type_dict + more InputRecordSchema requirement definitions · illusional/python-cwlgen@863e04d · GitHub
[go: up one dir, main page]

Skip to content

Commit 863e04d

Browse files
committed
get_type_dict + more InputRecordSchema requirement definitions
1 parent ea22ec6 commit 863e04d

File tree

2 files changed

+151
-28
lines changed

2 files changed

+151
-28
lines changed

cwlgen/elements.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
DEF_TYPE = 'null'
1818

1919

20-
def parse_param_type(param_type, requires_type=False):
20+
def parse_type(param_type, requires_type=False):
2121
"""
2222
Parses the parameter type as one of the required types:
2323
:: https://www.commonwl.org/v1.0/CommandLineTool.html#CommandInputParameter
@@ -53,7 +53,7 @@ def parse_param_type(param_type, requires_type=False):
5353
return param_type
5454

5555
elif isinstance(param_type, list):
56-
return [parse_param_type(p) for p in param_type]
56+
return [parse_type(p) for p in param_type]
5757

5858
elif isinstance(param_type, CommandInputArraySchema):
5959
return param_type # validate if required
@@ -62,6 +62,27 @@ def parse_param_type(param_type, requires_type=False):
6262
return DEF_TYPE
6363

6464

65+
def get_type_dict(param_type):
66+
"""
67+
Convets
68+
:param param_type:
69+
:type param_type: CWLType | InputRecordSchema | InputEnumSchema | InputArraySchema | string |
70+
array<CWLType | InputRecordSchema | InputEnumSchema | InputArraySchema | string>
71+
:return: str | dict
72+
"""
73+
if isinstance(param_type, str):
74+
return param_type
75+
elif isinstance(param_type, list):
76+
return [get_type_dict(p) for p in param_type]
77+
elif isinstance(param_type, dict):
78+
return param_type
79+
elif getattr(param_type, 'get_dict', None) and callable(getattr(param_type, 'get_dict', None)):
80+
return param_type.get_dict()
81+
else:
82+
raise Exception("Could not convert '{param_type}' to dictionary as it was unrecognised"
83+
.format(param_type=type(param_type)))
84+
85+
6586
class Parameter(object):
6687
'''
6788
Based class for parameters (common field of Input and Output) for CommandLineTool and Workflow
@@ -93,7 +114,7 @@ def __init__(self, param_id, label=None, secondary_files=None, param_format=None
93114
self.format = param_format
94115
self.streamable = streamable
95116
self.doc = doc
96-
self.type = parse_param_type(param_type, requires_type)
117+
self.type = parse_type(param_type, requires_type)
97118

98119
def get_dict(self):
99120
'''
@@ -140,7 +161,7 @@ def __init__(self, items=None, label=None, input_binding=None):
140161
:type input_binding: CommandLineBinding
141162
'''
142163
self.type = "array"
143-
self.items = parse_param_type(items, requires_type=True)
164+
self.items = parse_type(items, requires_type=True)
144165
self.label = label
145166
self.inputBinding = input_binding
146167

cwlgen/requirements.py

Lines changed: 126 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
from abc import ABC, abstractmethod
22

3+
import six
4+
5+
from .elements import parse_type, get_type_dict
6+
37

48
class Requirement(ABC):
59
'''
@@ -65,30 +69,128 @@ def get_dict(self):
6569
base = Requirement.get_dict(self)
6670
base['types'] = [t.get_dict() for t in self.types]
6771

68-
# class InputRecordSchema(object):
69-
# """
70-
# Documentation: https://www.commonwl.org/v1.0/Workflow.html#InputRecordSchema
71-
# """
72-
# def __init__(self, label=None, name=None):
73-
# """
74-
# :param fields: Defines the fields of the record.
75-
# :type fields: array<InputRecordField>
76-
# :param label: A short, human-readable label of this object.
77-
# :param name: NF (Name of the InputRecord)
78-
# """
79-
# self.fields = []
80-
# self.label = label
81-
# self.name = name
82-
#
83-
# def get_dict(self):
84-
# schema = {"type": "record"}
85-
# if self.fields:
86-
# schema["fields"] = {f.name: f.get_dict() for f in self.fields}
87-
# if self.label:
88-
# schema["label"] = self.label
89-
# if self.name:
90-
# schema["name"] = self.name
91-
# return schema
72+
class InputRecordSchema(object):
73+
"""
74+
Documentation: https://www.commonwl.org/v1.0/Workflow.html#InputRecordSchema
75+
"""
76+
def __init__(self, label=None, name=None):
77+
"""
78+
:param fields: Defines the fields of the record.
79+
:type fields: array<InputRecordField>
80+
:param label: A short, human-readable label of this object.
81+
:param name: NF (Name of the InputRecord)
82+
"""
83+
self.fields = []
84+
self.label = label
85+
self.name = name
86+
87+
def get_dict(self):
88+
schema = {"type": "record"}
89+
if self.fields:
90+
schema["fields"] = {f.name: f.get_dict() for f in self.fields}
91+
if self.label:
92+
schema["label"] = self.label
93+
if self.name:
94+
schema["name"] = self.name
95+
return schema
96+
97+
class InputRecordField(object):
98+
"""
99+
Documentation: https://www.commonwl.org/v1.0/Workflow.html#InputRecordField
100+
"""
101+
def __init__(self, name, input_type, doc=None, input_binding=None, label=None):
102+
"""
103+
:param name:
104+
:param input_type:
105+
:type input_type: CWLType | InputRecordSchema | InputEnumSchema | InputArraySchema | string |
106+
array<CWLType | InputRecordSchema | InputEnumSchema | InputArraySchema | string>
107+
:param doc: A documentation string for this field
108+
:param input_binding:
109+
:type input_binding: CommandLineBinding
110+
:param label:
111+
"""
112+
self.name = name
113+
self.type = parse_type(input_type, requires_type=True)
114+
self.doc = doc
115+
self.inputBinding = input_binding
116+
self.label = label
117+
118+
def get_dict(self):
119+
base = {
120+
'name': self.name,
121+
"type": get_type_dict(self.type)
122+
}
123+
124+
if self.doc:
125+
base["doc"] = self.doc
126+
if self.inputBinding:
127+
base["inputBinding"] = self.inputBinding.get_dict()
128+
if self.label:
129+
base["label"] = self.label
130+
return base
131+
132+
class InputEnumSchema(object):
133+
"""
134+
Documentation: https://www.commonwl.org/v1.0/Workflow.html#InputEnumSchema
135+
"""
136+
def __init__(self, symbols, label=None, name=None, input_binding=None):
137+
"""
138+
:param symbols: Defines the set of valid symbols.
139+
:type symbols: list[STRING]
140+
:param label: A short, human-readable label of this object.
141+
:type label: STRING
142+
:param name:
143+
:type name: STRING
144+
:param input_binding:
145+
:type input_binding: CommandLineBinding
146+
"""
147+
self.symbols = symbols
148+
self.label = label
149+
self.name = name
150+
self.inputBinding = input_binding
151+
152+
def get_dict(self):
153+
base = {
154+
'symbols': self.symbols,
155+
'type': 'enum'
156+
}
157+
if self.label:
158+
base["label"] = self.label
159+
if self.name:
160+
base["name"] = self.name
161+
if self.inputBinding:
162+
base["inputBinding"] = self.inputBinding
163+
return base
164+
165+
class InputArraySchema(object):
166+
"""
167+
Documentation: https://www.commonwl.org/v1.0/Workflow.html#InputArraySchema
168+
"""
169+
170+
def __init__(self, items, label, input_binding):
171+
"""
172+
:param items: Defines the type of the array elements.
173+
:type items: CWLType | InputRecordSchema | InputEnumSchema | InputArraySchema | string |
174+
array<CWLType | InputRecordSchema | InputEnumSchema | InputArraySchema | string>
175+
:param label: A short, human-readable label of this object.
176+
:type label: STRING
177+
:param input_binding:
178+
:type input_binding: CommandLineBinding
179+
"""
180+
self.items = items
181+
self.label = label
182+
self.inputBinding = input_binding
183+
184+
def get_dict(self):
185+
base = {
186+
'items': get_type_dict(self.items),
187+
'type': 'enum'
188+
}
189+
if self.label:
190+
base["label"] = self.label
191+
if self.inputBinding:
192+
base["inputBinding"] = self.inputBinding
193+
return base
92194

93195

94196
class SoftwareRequirement(Requirement):

0 commit comments

Comments
 (0)
0