8
8
from schema_salad .exceptions import ValidationException
9
9
from schema_salad .utils import yaml_no_ts
10
10
11
- from . import cwl_v1_0 as cwl_v1_0
12
- from . import cwl_v1_1 as cwl_v1_1
13
- from . import cwl_v1_2 as cwl_v1_2
11
+ from ..errors import GraphTargetMissingException
12
+ from . import cwl_v1_0 , cwl_v1_1 , cwl_v1_2
14
13
15
14
LoadingOptions = Union [
16
15
cwl_v1_0 .LoadingOptions , cwl_v1_1 .LoadingOptions , cwl_v1_2 .LoadingOptions
38
37
_Loader = Union [cwl_v1_0 ._Loader , cwl_v1_1 ._Loader , cwl_v1_2 ._Loader ]
39
38
40
39
40
+ def _get_id_from_graph (yaml : MutableMapping [str , Any ], id_ : Optional [str ]) -> Any :
41
+ if id_ is None :
42
+ id_ = "main"
43
+ for el in yaml ["$graph" ]:
44
+ if el ["id" ].lstrip ("#" ) == id_ :
45
+ return el
46
+ raise GraphTargetMissingException (
47
+ "Tool file contains graph of multiple objects, must specify "
48
+ "one of #%s" % ", #" .join (el ["id" ] for el in yaml ["$graph" ])
49
+ )
50
+
51
+
41
52
def cwl_version (yaml : Any ) -> Any :
42
53
"""Return the cwlVersion of a YAML object.
43
54
@@ -64,49 +75,61 @@ def load_document_by_uri(
64
75
"""Load a CWL object from a URI or a path."""
65
76
if isinstance (path , str ):
66
77
uri = urlparse (path )
78
+ id_ = uri .fragment or None
67
79
if not uri .scheme or uri .scheme == "file" :
68
80
real_path = Path (unquote_plus (uri .path )).resolve ().as_uri ()
69
81
else :
70
82
real_path = path
71
83
else :
72
84
real_path = path .resolve ().as_uri ()
85
+ id_ = path .resolve ().name .split ("#" )[1 ] if "#" in path .resolve ().name else None
73
86
74
87
baseuri = str (real_path )
75
88
76
89
if loadingOptions is None :
77
90
loadingOptions = cwl_v1_2 .LoadingOptions (fileuri = baseuri )
78
91
79
92
doc = loadingOptions .fetcher .fetch_text (real_path )
80
- return load_document_by_string (doc , baseuri , loadingOptions )
93
+ return load_document_by_string (doc , baseuri , loadingOptions , id_ )
81
94
82
95
83
96
def load_document (
84
97
doc : Any ,
85
98
baseuri : Optional [str ] = None ,
86
99
loadingOptions : Optional [LoadingOptions ] = None ,
100
+ id_ : Optional [str ] = None ,
87
101
) -> Any :
88
102
"""Load a CWL object from a serialized YAML string or a YAML object."""
89
103
if baseuri is None :
90
104
baseuri = cwl_v1_0 .file_uri (os .getcwd ()) + "/"
91
105
if isinstance (doc , str ):
92
- return load_document_by_string (doc , baseuri , loadingOptions )
93
- return load_document_by_yaml (doc , baseuri , loadingOptions )
106
+ return load_document_by_string (doc , baseuri , loadingOptions , id_ )
107
+ return load_document_by_yaml (doc , baseuri , loadingOptions , id_ )
94
108
95
109
96
110
def load_document_by_string (
97
- string : str , uri : str , loadingOptions : Optional [LoadingOptions ] = None
111
+ string : str ,
112
+ uri : str ,
113
+ loadingOptions : Optional [LoadingOptions ] = None ,
114
+ id_ : Optional [str ] = None ,
98
115
) -> Any :
99
116
"""Load a CWL object from a serialized YAML string."""
100
117
yaml = yaml_no_ts ()
101
118
result = yaml .load (string )
102
- return load_document_by_yaml (result , uri , loadingOptions )
119
+ return load_document_by_yaml (result , uri , loadingOptions , id_ )
103
120
104
121
105
122
def load_document_by_yaml (
106
- yaml : Any , uri : str , loadingOptions : Optional [LoadingOptions ] = None
123
+ yaml : Any ,
124
+ uri : str ,
125
+ loadingOptions : Optional [LoadingOptions ] = None ,
126
+ id_ : Optional [str ] = None ,
107
127
) -> Any :
108
128
"""Load a CWL object from a YAML object."""
109
129
version = cwl_version (yaml )
130
+ if "$graph" in yaml :
131
+ yaml = _get_id_from_graph (yaml , id_ )
132
+ yaml ["cwlVersion" ] = version
110
133
if version == "v1.0" :
111
134
result = cwl_v1_0 .load_document_by_yaml (
112
135
yaml , uri , cast (Optional [cwl_v1_0 .LoadingOptions ], loadingOptions )
0 commit comments