1
1
import copy
2
2
from functools import partial
3
3
from typing import (
4
- Any ,
5
4
Callable ,
6
5
Dict ,
7
6
List ,
10
9
Optional ,
11
10
Tuple ,
12
11
Union ,
12
+ cast ,
13
13
)
14
14
15
15
from ruamel .yaml .comments import CommentedMap , CommentedSeq
18
18
from schema_salad .sourceline import SourceLine
19
19
20
20
from .loghandler import _logger
21
- from .utils import aslist , visit_class , visit_field
21
+ from .utils import CWLObjectType , CWLOutputType , aslist , visit_class , visit_field
22
22
23
23
24
- def v1_1to1_2 (doc , loader , baseuri ): # pylint: disable=unused-argument
25
- # type: (Any, Loader, str) -> Tuple[Any, str]
24
+ def v1_1to1_2 (
25
+ doc : CommentedMap , loader : Loader , baseuri : str
26
+ ) -> Tuple [CommentedMap , str ]: # pylint: disable=unused-argument
26
27
"""Public updater for v1.1 to v1.2"""
27
28
28
29
doc = copy .deepcopy (doc )
29
30
30
31
upd = doc
31
32
if isinstance (upd , MutableMapping ) and "$graph" in upd :
32
- upd = upd ["$graph" ]
33
+ upd = cast ( CommentedMap , upd ["$graph" ])
33
34
for proc in aslist (upd ):
34
35
if "cwlVersion" in proc :
35
36
del proc ["cwlVersion" ]
@@ -38,8 +39,8 @@ def v1_1to1_2(doc, loader, baseuri): # pylint: disable=unused-argument
38
39
39
40
40
41
def v1_0to1_1 (
41
- doc : Any , loader : Loader , baseuri : str
42
- ) -> Tuple [Any , str ]: # pylint: disable=unused-argument
42
+ doc : CommentedMap , loader : Loader , baseuri : str
43
+ ) -> Tuple [CommentedMap , str ]: # pylint: disable=unused-argument
43
44
"""Public updater for v1.0 to v1.1."""
44
45
doc = copy .deepcopy (doc )
45
46
@@ -52,29 +53,31 @@ def v1_0to1_1(
52
53
"http://commonwl.org/cwltool#LoadListingRequirement" : "LoadListingRequirement" ,
53
54
}
54
55
55
- def rewrite_requirements (t : MutableMapping [ str , Any ] ) -> None :
56
+ def rewrite_requirements (t : CWLObjectType ) -> None :
56
57
if "requirements" in t :
57
- for r in t ["requirements" ]:
58
+ for r in cast ( MutableSequence [ CWLObjectType ], t ["requirements" ]) :
58
59
if isinstance (r , MutableMapping ):
59
- if r ["class" ] in rewrite :
60
- r ["class" ] = rewrite [r ["class" ]]
60
+ cls = cast (str , r ["class" ])
61
+ if cls in rewrite :
62
+ r ["class" ] = rewrite [cls ]
61
63
else :
62
64
raise ValidationException (
63
65
"requirements entries must be dictionaries: {} {}." .format (
64
66
type (r ), r
65
67
)
66
68
)
67
69
if "hints" in t :
68
- for r in t ["hints" ]:
70
+ for r in cast ( MutableSequence [ CWLObjectType ], t ["hints" ]) :
69
71
if isinstance (r , MutableMapping ):
70
- if r ["class" ] in rewrite :
71
- r ["class" ] = rewrite [r ["class" ]]
72
+ cls = cast (str , r ["class" ])
73
+ if cls in rewrite :
74
+ r ["class" ] = rewrite [cls ]
72
75
else :
73
76
raise ValidationException (
74
77
"hints entries must be dictionaries: {} {}." .format (type (r ), r )
75
78
)
76
79
if "steps" in t :
77
- for s in t ["steps" ]:
80
+ for s in cast ( MutableSequence [ CWLObjectType ], t ["steps" ]) :
78
81
if isinstance (s , MutableMapping ):
79
82
rewrite_requirements (s )
80
83
else :
@@ -83,25 +86,27 @@ def rewrite_requirements(t: MutableMapping[str, Any]) -> None:
83
86
)
84
87
85
88
def update_secondaryFiles (t , top = False ):
86
- # type: (Any , bool) -> Union[MutableSequence[MutableMapping[str, str]], MutableMapping[str, str]]
89
+ # type: (CWLOutputType , bool) -> Union[MutableSequence[MutableMapping[str, str]], MutableMapping[str, str]]
87
90
if isinstance (t , CommentedSeq ):
88
91
new_seq = copy .deepcopy (t )
89
92
for index , entry in enumerate (t ):
90
93
new_seq [index ] = update_secondaryFiles (entry )
91
94
return new_seq
92
95
elif isinstance (t , MutableSequence ):
93
- return CommentedSeq ([update_secondaryFiles (p ) for p in t ])
96
+ return CommentedSeq (
97
+ [update_secondaryFiles (cast (CWLOutputType , p )) for p in t ]
98
+ )
94
99
elif isinstance (t , MutableMapping ):
95
- return t
100
+ return cast ( MutableMapping [ str , str ], t )
96
101
elif top :
97
102
return CommentedSeq ([CommentedMap ([("pattern" , t )])])
98
103
else :
99
104
return CommentedMap ([("pattern" , t )])
100
105
101
- def fix_inputBinding (t ): # type: (Dict[str, Any] ) -> None
102
- for i in t ["inputs" ]:
106
+ def fix_inputBinding (t : CWLObjectType ) -> None :
107
+ for i in cast ( MutableSequence [ CWLObjectType ], t ["inputs" ]) :
103
108
if "inputBinding" in i :
104
- ib = i ["inputBinding" ]
109
+ ib = cast ( CWLObjectType , i ["inputBinding" ])
105
110
for k in list (ib .keys ()):
106
111
if k != "loadContents" :
107
112
_logger .warning (
@@ -118,7 +123,7 @@ def fix_inputBinding(t): # type: (Dict[str, Any]) -> None
118
123
119
124
upd = doc
120
125
if isinstance (upd , MutableMapping ) and "$graph" in upd :
121
- upd = upd ["$graph" ]
126
+ upd = cast ( CommentedMap , upd ["$graph" ])
122
127
for proc in aslist (upd ):
123
128
proc .setdefault ("hints" , CommentedSeq ())
124
129
proc ["hints" ].insert (
@@ -136,33 +141,35 @@ def fix_inputBinding(t): # type: (Dict[str, Any]) -> None
136
141
return (doc , "v1.1" )
137
142
138
143
139
- def v1_1_0dev1to1_1 (doc , loader , baseuri ): # pylint: disable=unused-argument
140
- # type: (Any, Loader, str) -> Tuple[Any, str]
144
+ def v1_1_0dev1to1_1 (
145
+ doc : CommentedMap , loader : Loader , baseuri : str
146
+ ) -> Tuple [CommentedMap , str ]: # pylint: disable=unused-argument
141
147
return (doc , "v1.1" )
142
148
143
149
144
- def v1_2_0dev1todev2 (doc , loader , baseuri ): # pylint: disable=unused-argument
145
- # type: (Any, Loader, str) -> Tuple[Any, str]
150
+ def v1_2_0dev1todev2 (
151
+ doc : CommentedMap , loader : Loader , baseuri : str
152
+ ) -> Tuple [CommentedMap , str ]: # pylint: disable=unused-argument
146
153
return (doc , "v1.2.0-dev2" )
147
154
148
155
149
156
def v1_2_0dev2todev3 (
150
- doc : Any , loader : Loader , baseuri : str
151
- ) -> Tuple [Any , str ]: # pylint: disable=unused-argument
157
+ doc : CommentedMap , loader : Loader , baseuri : str
158
+ ) -> Tuple [CommentedMap , str ]: # pylint: disable=unused-argument
152
159
"""Public updater for v1.2.0-dev2 to v1.2.0-dev3"""
153
160
doc = copy .deepcopy (doc )
154
161
155
- def update_pickvalue (t : Any ) -> None :
156
- for step in t ["steps" ]:
157
- for inp in step ["in" ]:
162
+ def update_pickvalue (t : CWLObjectType ) -> None :
163
+ for step in cast ( MutableSequence [ CWLObjectType ], t ["steps" ]) :
164
+ for inp in cast ( MutableSequence [ CWLObjectType ], step ["in" ]) :
158
165
if "pickValue" in inp :
159
166
if inp ["pickValue" ] == "only_non_null" :
160
167
inp ["pickValue" ] = "the_only_non_null"
161
168
162
169
visit_class (doc , "Workflow" , update_pickvalue )
163
170
upd = doc
164
171
if isinstance (upd , MutableMapping ) and "$graph" in upd :
165
- upd = upd ["$graph" ]
172
+ upd = cast ( CommentedMap , upd ["$graph" ])
166
173
for proc in aslist (upd ):
167
174
if "cwlVersion" in proc :
168
175
del proc ["cwlVersion" ]
@@ -172,14 +179,14 @@ def update_pickvalue(t: Any) -> None:
172
179
UPDATES = {
173
180
u"v1.0" : v1_0to1_1 ,
174
181
u"v1.1" : v1_1to1_2 ,
175
- } # type: Dict[str, Optional[Callable[[Any , Loader, str], Tuple[Any , str]]]]
182
+ } # type: Dict[str, Optional[Callable[[CommentedMap , Loader, str], Tuple[CommentedMap , str]]]]
176
183
177
184
DEVUPDATES = {
178
185
u"v1.1.0-dev1" : v1_1_0dev1to1_1 ,
179
186
u"v1.2.0-dev1" : v1_2_0dev1todev2 ,
180
187
u"v1.2.0-dev2" : v1_2_0dev2todev3 ,
181
188
u"v1.2.0-dev3" : None ,
182
- } # type: Dict[str, Optional[Callable[[Any , Loader, str], Tuple[Any , str]]]]
189
+ } # type: Dict[str, Optional[Callable[[CommentedMap , Loader, str], Tuple[CommentedMap , str]]]]
183
190
184
191
185
192
ALLUPDATES = UPDATES .copy ()
@@ -190,18 +197,16 @@ def update_pickvalue(t: Any) -> None:
190
197
ORIGINAL_CWLVERSION = "http://commonwl.org/cwltool#original_cwlVersion"
191
198
192
199
193
- def identity (doc , loader , baseuri ): # pylint: disable=unused-argument
194
- # type: (Any, Loader, str) -> Tuple[Any, Union[str, str]]
200
+ def identity (
201
+ doc : CommentedMap , loader : Loader , baseuri : str
202
+ ) -> Tuple [CommentedMap , str ]: # pylint: disable=unused-argument
195
203
"""Default, do-nothing, CWL document upgrade function."""
196
- return (doc , doc ["cwlVersion" ])
204
+ return (doc , cast ( str , doc ["cwlVersion" ]) )
197
205
198
206
199
207
def checkversion (
200
- doc , # type: Union[CommentedSeq, CommentedMap]
201
- metadata , # type: CommentedMap
202
- enable_dev , # type: bool
203
- ):
204
- # type: (...) -> Tuple[CommentedMap, str]
208
+ doc : Union [CommentedSeq , CommentedMap ], metadata : CommentedMap , enable_dev : bool ,
209
+ ) -> Tuple [CommentedMap , str ]:
205
210
"""Check the validity of the version of the give CWL document.
206
211
207
212
Returns the document and the validated version string.
@@ -251,15 +256,20 @@ def checkversion(
251
256
return (cdoc , version )
252
257
253
258
254
- def update (doc , loader , baseuri , enable_dev , metadata ):
255
- # type: (Union[CommentedSeq, CommentedMap], Loader, str, bool, Any) -> CommentedMap
259
+ def update (
260
+ doc : Union [CommentedSeq , CommentedMap ],
261
+ loader : Loader ,
262
+ baseuri : str ,
263
+ enable_dev : bool ,
264
+ metadata : CommentedMap ,
265
+ ) -> CommentedMap :
256
266
257
267
(cdoc , version ) = checkversion (doc , metadata , enable_dev )
258
268
originalversion = copy .copy (version )
259
269
260
270
nextupdate = (
261
271
identity
262
- ) # type: Optional[Callable[[Any , Loader, str], Tuple[Any , str]]]
272
+ ) # type: Optional[Callable[[CommentedMap , Loader, str], Tuple[CommentedMap , str]]]
263
273
264
274
while nextupdate :
265
275
(cdoc , version ) = nextupdate (cdoc , loader , baseuri )
0 commit comments