-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
__init__.py
303 lines (275 loc) · 10.5 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# SPDX-License-Identifier: Apache-2.0
import os
from pathlib import Path
from typing import Any, MutableMapping, MutableSequence, Optional, Union, cast
from urllib.parse import unquote_plus, urlparse
from schema_salad.exceptions import ValidationException
from schema_salad.utils import yaml_no_ts
from ..errors import GraphTargetMissingException
from . import cwl_v1_0, cwl_v1_1, cwl_v1_2
LoadingOptions = Union[
cwl_v1_0.LoadingOptions, cwl_v1_1.LoadingOptions, cwl_v1_2.LoadingOptions
]
"""Type union for a CWL v1.x LoadingOptions object."""
Saveable = Union[cwl_v1_0.Saveable, cwl_v1_1.Saveable, cwl_v1_2.Saveable]
"""Type union for a CWL v1.x Saveable object."""
InputParameter = Union[
cwl_v1_0.InputParameter, cwl_v1_1.InputParameter, cwl_v1_2.InputParameter
]
"""Type union for a CWL v1.x InputParameter object."""
OutputParameter = Union[
cwl_v1_0.OutputParameter, cwl_v1_1.OutputParameter, cwl_v1_2.OutputParameter
]
"""Type union for a CWL v1.x OutputParameter object."""
Workflow = Union[cwl_v1_0.Workflow, cwl_v1_1.Workflow, cwl_v1_2.Workflow]
WorkflowTypes = (cwl_v1_0.Workflow, cwl_v1_1.Workflow, cwl_v1_2.Workflow)
"""Type union for a CWL v1.x Workflow object."""
WorkflowInputParameter = Union[
cwl_v1_0.InputParameter,
cwl_v1_1.WorkflowInputParameter,
cwl_v1_2.WorkflowInputParameter,
]
"""Type union for a CWL v1.x WorkflowInputParameter object."""
WorkflowOutputParameter = Union[
cwl_v1_0.WorkflowOutputParameter,
cwl_v1_1.WorkflowOutputParameter,
cwl_v1_2.WorkflowOutputParameter,
]
"""Type union for a CWL v1.x WorkflowOutputParameter object."""
WorkflowStep = Union[
cwl_v1_0.WorkflowStep, cwl_v1_1.WorkflowStep, cwl_v1_2.WorkflowStep
]
"""Type union for a CWL v1.x WorkflowStep object."""
WorkflowStepInput = Union[
cwl_v1_0.WorkflowStepInput, cwl_v1_1.WorkflowStepInput, cwl_v1_2.WorkflowStepInput
]
"""Type union for a CWL v1.x WorkflowStepInput object."""
WorkflowStepOutput = Union[
cwl_v1_0.WorkflowStepOutput,
cwl_v1_1.WorkflowStepOutput,
cwl_v1_2.WorkflowStepOutput,
]
"""Type union for a CWL v1.x WorkflowStepOutput object."""
CommandLineTool = Union[
cwl_v1_0.CommandLineTool, cwl_v1_1.CommandLineTool, cwl_v1_2.CommandLineTool
]
"""Type union for a CWL v1.x CommandLineTool object."""
CommandLineBinding = Union[
cwl_v1_0.CommandLineBinding,
cwl_v1_1.CommandLineBinding,
cwl_v1_2.CommandLineBinding,
]
"""Type union for a CWL v1.x CommandLineBinding object."""
CommandOutputParameter = Union[
cwl_v1_0.CommandOutputParameter,
cwl_v1_1.CommandOutputParameter,
cwl_v1_2.CommandOutputParameter,
]
"""Type union for a CWL v1.x CommandOutputParameter object."""
ExpressionTool = Union[
cwl_v1_0.ExpressionTool, cwl_v1_1.ExpressionTool, cwl_v1_2.ExpressionTool
]
"""Type union for a CWL v1.x ExpressionTool object."""
DockerRequirement = Union[
cwl_v1_0.DockerRequirement, cwl_v1_1.DockerRequirement, cwl_v1_2.DockerRequirement
]
DockerRequirementTypes = (
cwl_v1_0.DockerRequirement,
cwl_v1_1.DockerRequirement,
cwl_v1_2.DockerRequirement,
)
"""Type union for a CWL v1.x DockerRequirement object."""
Process = Union[Workflow, CommandLineTool, ExpressionTool, cwl_v1_2.Operation]
"""Type Union for a CWL v1.x Process object."""
ProcessRequirement = Union[
cwl_v1_0.ProcessRequirement,
cwl_v1_1.ProcessRequirement,
cwl_v1_2.ProcessRequirement,
]
"""Type Union for a CWL v1.x ProcessRequirement object."""
ProcessRequirementTypes = (
cwl_v1_0.ProcessRequirement,
cwl_v1_1.ProcessRequirement,
cwl_v1_2.ProcessRequirement,
)
SoftwareRequirement = Union[
cwl_v1_0.SoftwareRequirement,
cwl_v1_1.SoftwareRequirement,
cwl_v1_2.SoftwareRequirement,
]
SoftwareRequirementTypes = (
cwl_v1_0.SoftwareRequirement,
cwl_v1_1.SoftwareRequirement,
cwl_v1_2.SoftwareRequirement,
)
"""Type union for a CWL v1.x SoftwareRequirement object."""
ArraySchema = Union[cwl_v1_0.ArraySchema, cwl_v1_1.ArraySchema, cwl_v1_2.ArraySchema]
"""Type Union for a CWL v1.x ArraySchema object."""
EnumSchema = Union[cwl_v1_0.EnumSchema, cwl_v1_1.EnumSchema, cwl_v1_2.EnumSchema]
"""Type Union for a CWL v1.x EnumSchema object."""
RecordSchema = Union[
cwl_v1_0.RecordSchema, cwl_v1_1.RecordSchema, cwl_v1_2.RecordSchema
]
"""Type Union for a CWL v1.x RecordSchema object."""
File = Union[cwl_v1_0.File, cwl_v1_1.File, cwl_v1_2.File]
"""Type Union for a CWL v1.x File object."""
SecondaryFileSchema = Union[cwl_v1_1.SecondaryFileSchema, cwl_v1_2.SecondaryFileSchema]
"""Type Union for a CWL v1.x SecondaryFileSchema object."""
Directory = Union[cwl_v1_0.Directory, cwl_v1_1.Directory, cwl_v1_2.Directory]
"""Type Union for a CWL v1.x Directory object."""
Dirent = Union[cwl_v1_0.Dirent, cwl_v1_1.Dirent, cwl_v1_2.Dirent]
"""Type Union for a CWL v1.x Dirent object."""
_Loader = Union[cwl_v1_0._Loader, cwl_v1_1._Loader, cwl_v1_2._Loader]
"""Type union for a CWL v1.x _Loader."""
def _get_id_from_graph(yaml: MutableMapping[str, Any], id_: Optional[str]) -> Any:
if id_ is None:
id_ = "main"
for el in yaml["$graph"]:
if el["id"].lstrip("#") == id_:
return el
raise GraphTargetMissingException(
"Tool file contains graph of multiple objects, must specify "
"one of #%s" % ", #".join(el["id"] for el in yaml["$graph"])
)
def cwl_version(yaml: Any) -> Optional[str]:
"""Return the cwlVersion of a YAML object.
:param yaml: ruamel.yaml object for a CWL document
:raises ValidationException: If `yaml` is not a MutableMapping.
"""
if not isinstance(yaml, MutableMapping):
raise ValidationException("MutableMapping is required")
if "cwlVersion" not in list(yaml.keys()):
return None
return cast(str, yaml["cwlVersion"])
def load_document_by_uri(
path: Union[str, Path],
loadingOptions: Optional[LoadingOptions] = None,
load_all: bool = False,
) -> Any:
"""Load a CWL object from a URI or a path."""
base_uri = ""
real_uri = ""
if isinstance(path, str):
uri = urlparse(path)
id_ = uri.fragment or None
if not uri.scheme or uri.scheme == "file":
real_uri = Path(unquote_plus(uri.path)).resolve().as_uri()
base_uri = Path(unquote_plus(uri.path)).resolve().parent.as_uri()
else:
real_uri = path
base_uri = os.path.dirname(path)
else:
real_uri = path.resolve().as_uri()
base_uri = path.resolve().parent.as_uri()
id_ = path.resolve().name.split("#")[1] if "#" in path.resolve().name else None
if loadingOptions is None:
loadingOptions = cwl_v1_2.LoadingOptions(fileuri=real_uri, baseuri=base_uri)
doc = loadingOptions.fetcher.fetch_text(real_uri)
return load_document_by_string(doc, real_uri, loadingOptions, id_, load_all)
def load_document(
doc: Any,
baseuri: Optional[str] = None,
loadingOptions: Optional[LoadingOptions] = None,
id_: Optional[str] = None,
load_all: bool = False,
) -> Any:
"""Load a CWL object from a serialized YAML string or a YAML object."""
if baseuri is None:
baseuri = cwl_v1_0.file_uri(os.getcwd()) + "/"
if isinstance(doc, str):
return load_document_by_string(doc, baseuri, loadingOptions, id_)
return load_document_by_yaml(doc, baseuri, loadingOptions, id_, load_all)
def load_document_by_string(
string: str,
uri: str,
loadingOptions: Optional[LoadingOptions] = None,
id_: Optional[str] = None,
load_all: bool = False,
) -> Any:
"""Load a CWL object from a serialized YAML string."""
yaml = yaml_no_ts()
result = yaml.load(string)
return load_document_by_yaml(result, uri, loadingOptions, id_, load_all)
def load_document_by_yaml(
yaml: Any,
uri: str,
loadingOptions: Optional[LoadingOptions] = None,
id_: Optional[str] = None,
load_all: bool = False,
) -> Any:
"""Load a CWL object from a YAML object."""
version = cwl_version(yaml)
if "$graph" in yaml and not load_all:
yaml = _get_id_from_graph(yaml, id_)
yaml["cwlVersion"] = version
if version == "v1.0":
result = cwl_v1_0.load_document_by_yaml(
yaml, uri, cast(Optional[cwl_v1_0.LoadingOptions], loadingOptions)
)
elif version == "v1.1":
result = cwl_v1_1.load_document_by_yaml(
yaml, uri, cast(Optional[cwl_v1_1.LoadingOptions], loadingOptions)
)
elif version == "v1.2":
result = cwl_v1_2.load_document_by_yaml(
yaml, uri, cast(Optional[cwl_v1_2.LoadingOptions], loadingOptions)
)
elif version is None:
raise ValidationException("could not get the cwlVersion")
else:
raise ValidationException(
f"Version error. Did not recognise {version} as a CWL version"
)
if isinstance(result, MutableSequence):
lst = []
for r in result:
if "cwlVersion" in r.attrs:
r.cwlVersion = version
lst.append(r)
return lst
return result
def save(
val: Optional[Union[Saveable, MutableSequence[Saveable]]],
top: bool = True,
base_url: str = "",
relative_uris: bool = True,
) -> Any:
"""Convert a CWL Python object into a JSON/YAML serializable object."""
if (
isinstance(val, cwl_v1_0.Saveable)
or isinstance(val, cwl_v1_1.Saveable)
or isinstance(val, cwl_v1_2.Saveable)
):
return val.save(top=top, base_url=base_url, relative_uris=relative_uris)
if isinstance(val, MutableSequence):
lst = [
save(v, top=top, base_url=base_url, relative_uris=relative_uris)
for v in val
]
if top and all(is_process(v) for v in val):
vers = [
e.get("cwlVersion") for i, e in enumerate(lst) if is_process(val[i])
]
latest = max(
(v for v in vers if v is not None), key=cast(Any, version_split)
)
return {"cwlVersion": latest, "$graph": lst}
return lst
if isinstance(val, MutableMapping):
newdict = {}
for key in val:
newdict[key] = save(
val[key], top=False, base_url=base_url, relative_uris=relative_uris
)
return newdict
return val
def is_process(v: Any) -> bool:
"""Test to see if the object is a CWL v1.x Python Process object."""
return (
isinstance(v, cwl_v1_0.Process)
or isinstance(v, cwl_v1_1.Process)
or isinstance(v, cwl_v1_2.Process)
)
def version_split(version: str) -> MutableSequence[int]:
"""Split a cwlVersion value into its numerical components."""
return [int(v) for v in version[1:].split(".")]