2
2
3
3
import copy
4
4
import logging
5
- from typing import Any , Callable , Dict , List , Optional , Set , Union , Tuple
5
+ from typing import (Any , Callable , Dict , List , Optional , Set , Union ,
6
+ Tuple , MutableMapping , MutableSequence )
6
7
from typing_extensions import Text , Type , TYPE_CHECKING # pylint: disable=unused-import
7
8
# move to a regular typing import when Python 3.3-3.6 is no longer supported
8
9
9
10
from rdflib import Graph , URIRef # pylint: disable=unused-import
10
11
from rdflib .namespace import OWL , RDFS
12
+ from ruamel .yaml .comments import CommentedMap
11
13
import schema_salad .schema # pylint: disable=unused-import
12
14
from schema_salad import validate
13
15
from schema_salad .schema import AvroSchemaFromJSONData
@@ -194,22 +196,23 @@ def build_job_script(self, commands):
194
196
return None
195
197
196
198
def bind_input (self ,
197
- schema , # type: Dict [Text, Any]
199
+ schema , # type: MutableMapping [Text, Any]
198
200
datum , # type: Any
199
201
discover_secondaryFiles , # type: bool
200
202
lead_pos = None , # type: Optional[Union[int, List[int]]]
201
203
tail_pos = None , # type: Optional[List[int]]
202
- ): # type: (...) -> List[Dict [Text, Any]]
204
+ ): # type: (...) -> List[MutableMapping [Text, Any]]
203
205
204
206
if tail_pos is None :
205
207
tail_pos = []
206
208
if lead_pos is None :
207
209
lead_pos = []
208
- bindings = [] # type: List[Dict [Text,Text]]
209
- binding = None # type: Optional[Dict [Text,Any]]
210
+ bindings = [] # type: List[MutableMapping [Text, Text]]
211
+ binding = None # type: Optional[MutableMapping [Text,Any]]
210
212
value_from_expression = False
211
- if "inputBinding" in schema and isinstance (schema ["inputBinding" ], dict ):
212
- binding = copy .copy (schema ["inputBinding" ])
213
+ if "inputBinding" in schema and isinstance (schema ["inputBinding" ], MutableMapping ):
214
+ binding = CommentedMap (schema ["inputBinding" ].items ())
215
+ assert binding is not None
213
216
214
217
if "position" in binding :
215
218
binding ["position" ] = aslist (lead_pos ) + aslist (binding ["position" ]) + aslist (tail_pos )
@@ -221,12 +224,12 @@ def bind_input(self,
221
224
value_from_expression = True
222
225
223
226
# Handle union types
224
- if isinstance (schema ["type" ], list ):
227
+ if isinstance (schema ["type" ], MutableSequence ):
225
228
bound_input = False
226
229
for t in schema ["type" ]:
227
230
if isinstance (t , string_types ) and self .names .has_name (t , "" ):
228
231
avsc = self .names .get_name (t , "" )
229
- elif isinstance (t , dict ) and "name" in t and self .names .has_name (t ["name" ], "" ):
232
+ elif isinstance (t , MutableMapping ) and "name" in t and self .names .has_name (t ["name" ], "" ):
230
233
avsc = self .names .get_name (t ["name" ], "" )
231
234
else :
232
235
avsc = AvroSchemaFromJSONData (t , self .names )
@@ -240,7 +243,7 @@ def bind_input(self,
240
243
bound_input = True
241
244
if not bound_input :
242
245
raise validate .ValidationException (u"'%s' is not a valid union %s" % (datum , schema ["type" ]))
243
- elif isinstance (schema ["type" ], dict ):
246
+ elif isinstance (schema ["type" ], MutableMapping ):
244
247
st = copy .deepcopy (schema ["type" ])
245
248
if binding and "inputBinding" not in st and st ["type" ] == "array" and "itemSeparator" not in binding :
246
249
st ["inputBinding" ] = {}
@@ -289,7 +292,7 @@ def bind_input(self,
289
292
if "secondaryFiles" not in datum :
290
293
datum ["secondaryFiles" ] = []
291
294
for sf in aslist (schema ["secondaryFiles" ]):
292
- if isinstance (sf , dict ) or "$(" in sf or "${" in sf :
295
+ if isinstance (sf , MutableMapping ) or "$(" in sf or "${" in sf :
293
296
sfpath = self .do_eval (sf , context = datum )
294
297
else :
295
298
sfpath = substitute (datum ["basename" ], sf )
@@ -301,7 +304,7 @@ def bind_input(self,
301
304
if d ["basename" ] == sfname :
302
305
found = True
303
306
if not found :
304
- if isinstance (sfname , dict ):
307
+ if isinstance (sfname , MutableMapping ):
305
308
datum ["secondaryFiles" ].append (sfname )
306
309
elif discover_secondaryFiles :
307
310
datum ["secondaryFiles" ].append ({
@@ -344,7 +347,7 @@ def _capture_files(f):
344
347
return bindings
345
348
346
349
def tostr (self , value ): # type: (Any) -> Text
347
- if isinstance (value , dict ) and value .get ("class" ) in ("File" , "Directory" ):
350
+ if isinstance (value , MutableMapping ) and value .get ("class" ) in ("File" , "Directory" ):
348
351
if "path" not in value :
349
352
raise WorkflowException (u"%s object missing \" path\" : %s" % (value ["class" ], value ))
350
353
@@ -372,8 +375,8 @@ def generate_arg(self, binding): # type: (Dict[Text, Any]) -> List[Text]
372
375
with SourceLine (binding , "separate" , WorkflowException , _logger .isEnabledFor (logging .DEBUG )):
373
376
raise WorkflowException ("'separate' option can not be specified without prefix" )
374
377
375
- argl = [] # type: List[Dict [Text,Text]]
376
- if isinstance (value , list ):
378
+ argl = [] # type: MutableSequence[MutableMapping [Text, Text]]
379
+ if isinstance (value , MutableSequence ):
377
380
if binding .get ("itemSeparator" ) and value :
378
381
argl = [binding ["itemSeparator" ].join ([self .tostr (v ) for v in value ])]
379
382
elif binding .get ("valueFrom" ):
@@ -383,9 +386,9 @@ def generate_arg(self, binding): # type: (Dict[Text, Any]) -> List[Text]
383
386
return [prefix ]
384
387
else :
385
388
return []
386
- elif isinstance (value , dict ) and value .get ("class" ) in ("File" , "Directory" ):
389
+ elif isinstance (value , MutableMapping ) and value .get ("class" ) in ("File" , "Directory" ):
387
390
argl = [value ]
388
- elif isinstance (value , dict ):
391
+ elif isinstance (value , MutableMapping ):
389
392
return [prefix ] if prefix else []
390
393
elif value is True and prefix :
391
394
return [prefix ]
@@ -407,10 +410,10 @@ def generate_arg(self, binding): # type: (Dict[Text, Any]) -> List[Text]
407
410
def do_eval (self , ex , context = None , recursive = False , strip_whitespace = True ):
408
411
# type: (Union[Dict[Text, Text], Text], Any, bool, bool) -> Any
409
412
if recursive :
410
- if isinstance (ex , dict ):
413
+ if isinstance (ex , MutableMapping ):
411
414
return {k : self .do_eval (v , context , recursive )
412
415
for k , v in iteritems (ex )}
413
- if isinstance (ex , list ):
416
+ if isinstance (ex , MutableSequence ):
414
417
return [self .do_eval (v , context , recursive )
415
418
for v in ex ]
416
419
0 commit comments