88from schema_salad .exceptions import ValidationException
99from schema_salad .utils import yaml_no_ts
1010
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
1413
1514LoadingOptions = Union [
1615 cwl_v1_0 .LoadingOptions , cwl_v1_1 .LoadingOptions , cwl_v1_2 .LoadingOptions
3837_Loader = Union [cwl_v1_0 ._Loader , cwl_v1_1 ._Loader , cwl_v1_2 ._Loader ]
3938
4039
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+
4152def cwl_version (yaml : Any ) -> Any :
4253 """Return the cwlVersion of a YAML object.
4354
@@ -64,49 +75,61 @@ def load_document_by_uri(
6475 """Load a CWL object from a URI or a path."""
6576 if isinstance (path , str ):
6677 uri = urlparse (path )
78+ id_ = uri .fragment or None
6779 if not uri .scheme or uri .scheme == "file" :
6880 real_path = Path (unquote_plus (uri .path )).resolve ().as_uri ()
6981 else :
7082 real_path = path
7183 else :
7284 real_path = path .resolve ().as_uri ()
85+ id_ = path .resolve ().name .split ("#" )[1 ] if "#" in path .resolve ().name else None
7386
7487 baseuri = str (real_path )
7588
7689 if loadingOptions is None :
7790 loadingOptions = cwl_v1_2 .LoadingOptions (fileuri = baseuri )
7891
7992 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_ )
8194
8295
8396def load_document (
8497 doc : Any ,
8598 baseuri : Optional [str ] = None ,
8699 loadingOptions : Optional [LoadingOptions ] = None ,
100+ id_ : Optional [str ] = None ,
87101) -> Any :
88102 """Load a CWL object from a serialized YAML string or a YAML object."""
89103 if baseuri is None :
90104 baseuri = cwl_v1_0 .file_uri (os .getcwd ()) + "/"
91105 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_ )
94108
95109
96110def 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 ,
98115) -> Any :
99116 """Load a CWL object from a serialized YAML string."""
100117 yaml = yaml_no_ts ()
101118 result = yaml .load (string )
102- return load_document_by_yaml (result , uri , loadingOptions )
119+ return load_document_by_yaml (result , uri , loadingOptions , id_ )
103120
104121
105122def 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 ,
107127) -> Any :
108128 """Load a CWL object from a YAML object."""
109129 version = cwl_version (yaml )
130+ if "$graph" in yaml :
131+ yaml = _get_id_from_graph (yaml , id_ )
132+ yaml ["cwlVersion" ] = version
110133 if version == "v1.0" :
111134 result = cwl_v1_0 .load_document_by_yaml (
112135 yaml , uri , cast (Optional [cwl_v1_0 .LoadingOptions ], loadingOptions )
0 commit comments