Skip to content

Commit bf92bd9

Browse files
Regenerate parsers with unified runtime
1 parent 4a53edb commit bf92bd9

14 files changed

Lines changed: 225 additions & 3681 deletions

cwl_utils/cwl_v1_0_expression_refactor.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
from ruamel import yaml
1414
from schema_salad.metaschema import ArraySchema
15+
from schema_salad.runtime import LoadingOptions, save
1516
from schema_salad.sourceline import SourceLine
1617
from schema_salad.utils import json_dumps
1718

@@ -42,7 +43,7 @@ def expand_stream_shortcuts(process: cwl.CommandLineTool) -> cwl.CommandLineTool
4243
stdout_path = process.stdout
4344
if not stdout_path:
4445
stdout_path = hashlib.sha1( # nosec
45-
json_dumps(cwl.save(process)).encode("utf-8")
46+
json_dumps(save(process)).encode("utf-8")
4647
).hexdigest()
4748
result.stdout = stdout_path
4849
result.outputs[index].type_ = "File"
@@ -1219,7 +1220,7 @@ def process_level_reqs(
12191220

12201221

12211222
def add_input_to_process(
1222-
process: cwl.Process, name: str, inptype: Any, loadingOptions: cwl.LoadingOptions
1223+
process: cwl.Process, name: str, inptype: Any, loadingOptions: LoadingOptions
12231224
) -> None:
12241225
"""Add a new InputParameter to the given CommandLineTool."""
12251226
if isinstance(process, cwl.CommandLineTool):

cwl_utils/cwl_v1_1_expression_refactor.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
from ruamel import yaml
1414
from schema_salad.metaschema import ArraySchema
15+
from schema_salad.runtime import LoadingOptions, save
1516
from schema_salad.sourceline import SourceLine
1617
from schema_salad.utils import json_dumps
1718

@@ -42,7 +43,7 @@ def expand_stream_shortcuts(process: cwl.CommandLineTool) -> cwl.CommandLineTool
4243
stdout_path = process.stdout
4344
if not stdout_path:
4445
stdout_path = hashlib.sha1( # nosec
45-
json_dumps(cwl.save(process)).encode("utf-8")
46+
json_dumps(save(process)).encode("utf-8")
4647
).hexdigest()
4748
result.stdout = stdout_path
4849
result.outputs[index].type_ = "File"
@@ -1221,7 +1222,7 @@ def process_level_reqs(
12211222

12221223

12231224
def add_input_to_process(
1224-
process: cwl.Process, name: str, inptype: Any, loadingOptions: cwl.LoadingOptions
1225+
process: cwl.Process, name: str, inptype: Any, loadingOptions: LoadingOptions
12251226
) -> None:
12261227
"""Add a new InputParameter to the given CommandLineTool."""
12271228
if isinstance(process, cwl.CommandLineTool):

cwl_utils/cwl_v1_2_expression_refactor.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
from ruamel import yaml
1414
from schema_salad.metaschema import ArraySchema
15+
from schema_salad.runtime import LoadingOptions, save
1516
from schema_salad.sourceline import SourceLine
1617
from schema_salad.utils import json_dumps
1718

@@ -41,7 +42,7 @@ def expand_stream_shortcuts(process: cwl.CommandLineTool) -> cwl.CommandLineTool
4142
stdout_path = process.stdout
4243
if not stdout_path:
4344
stdout_path = hashlib.sha1( # nosec
44-
json_dumps(cwl.save(process)).encode("utf-8")
45+
json_dumps(save(process)).encode("utf-8")
4546
).hexdigest()
4647
result.stdout = stdout_path
4748
result.outputs[index].type_ = "File"
@@ -1324,7 +1325,7 @@ def process_level_reqs(
13241325

13251326

13261327
def add_input_to_process(
1327-
process: cwl.Process, name: str, inptype: Any, loadingOptions: cwl.LoadingOptions
1328+
process: cwl.Process, name: str, inptype: Any, loadingOptions: LoadingOptions
13281329
) -> None:
13291330
"""Add a new InputParameter to the given CommandLineTool."""
13301331
if isinstance(process, cwl.CommandLineTool):

cwl_utils/expression_refactor.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
from ruamel.yaml.main import YAML
1515
from ruamel.yaml.scalarstring import walk_tree
16+
from schema_salad.runtime import save
1617

1718
from cwl_utils import (
1819
cwl_v1_0_expression_refactor,
@@ -111,15 +112,12 @@ def refactor(args: argparse.Namespace) -> int:
111112
traverse: Callable[[Any, bool, bool, bool, bool], tuple[Any, bool]] = (
112113
cwl_v1_0_expression_refactor.traverse
113114
)
114-
save: saveCWL = cwl_v1_0.save
115115
case "v1.1":
116116
top = cwl_v1_1.load_document_by_yaml(result, uri)
117117
traverse = cwl_v1_1_expression_refactor.traverse
118-
save = cwl_v1_1.save
119118
case "v1.2":
120119
top = cwl_v1_2.load_document_by_yaml(result, uri)
121120
traverse = cwl_v1_2_expression_refactor.traverse
122-
save = cwl_v1_2.save
123121
case _:
124122
_logger.error(
125123
"Sorry, %s is not a supported CWL version by this tool.",

cwl_utils/normalizer.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@
1212

1313
from cwlupgrader import main as cwlupgrader
1414
from ruamel import yaml
15+
from schema_salad.runtime import save
1516
from schema_salad.sourceline import add_lc_filename
1617

1718
from cwl_utils import cwl_v1_2_expression_refactor
1819
from cwl_utils.loghandler import _logger as _cwlutilslogger
1920
from cwl_utils.pack import pack
20-
from cwl_utils.parser.cwl_v1_2 import load_document_by_yaml, save
21+
from cwl_utils.parser.cwl_v1_2 import load_document_by_yaml
2122

2223
_logger = logging.getLogger("cwl-normalizer") # pylint: disable=invalid-name
2324
defaultStreamHandler = logging.StreamHandler() # pylint: disable=invalid-name

cwl_utils/parser/__init__.py

Lines changed: 20 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
from abc import ABC
55
from collections.abc import MutableMapping, MutableSequence
66
from pathlib import Path
7-
from typing import Any, Optional, TypeAlias, cast
7+
from typing import Any, TypeAlias, cast
88
from urllib.parse import unquote_plus, urlparse
99

1010
import schema_salad.metaschema
11+
import schema_salad.runtime
1112
from schema_salad.exceptions import ValidationException
1213
from schema_salad.utils import yaml_no_ts
1314

@@ -19,11 +20,9 @@ class NoType(ABC):
1920
pass
2021

2122

22-
LoadingOptions: TypeAlias = (
23-
cwl_v1_0.LoadingOptions | cwl_v1_1.LoadingOptions | cwl_v1_2.LoadingOptions
24-
)
23+
LoadingOptions: TypeAlias = schema_salad.runtime.LoadingOptions
2524
"""Type union for a CWL v1.x LoadingOptions object."""
26-
Saveable: TypeAlias = cwl_v1_0.Saveable | cwl_v1_1.Saveable | cwl_v1_2.Saveable
25+
Saveable: TypeAlias = schema_salad.runtime.Saveable
2726
"""Type union for a CWL v1.x Saveable object."""
2827
InputParameter: TypeAlias = (
2928
cwl_v1_0.InputParameter | cwl_v1_1.InputParameter | cwl_v1_2.InputParameter
@@ -235,7 +234,7 @@ class NoType(ABC):
235234
| cwl_v1_2.WorkflowStepInput
236235
)
237236
"""Type Union for a CWL v1.x LoadContents object."""
238-
_Loader: TypeAlias = cwl_v1_0._Loader | cwl_v1_1._Loader | cwl_v1_2._Loader
237+
_Loader: TypeAlias = schema_salad.runtime._Loader
239238
"""Type union for a CWL v1.x _Loader."""
240239

241240

@@ -286,53 +285,16 @@ def load_document_by_uri(
286285
base_uri = path.resolve().parent.as_uri()
287286
id_ = path.resolve().name.split("#")[1] if "#" in path.resolve().name else None
288287

289-
match loadingOptions:
290-
case cwl_v1_0.LoadingOptions():
291-
loadingOptions = cwl_v1_0.LoadingOptions(
292-
fileuri=real_uri, baseuri=base_uri, copyfrom=loadingOptions
293-
)
294-
return load_document_by_string(
295-
loadingOptions.fetcher.fetch_text(real_uri),
296-
real_uri,
297-
loadingOptions,
298-
id_,
299-
load_all,
300-
)
301-
case cwl_v1_1.LoadingOptions():
302-
loadingOptions = cwl_v1_1.LoadingOptions(
303-
fileuri=real_uri, baseuri=base_uri, copyfrom=loadingOptions
304-
)
305-
return load_document_by_string(
306-
loadingOptions.fetcher.fetch_text(real_uri),
307-
real_uri,
308-
loadingOptions,
309-
id_,
310-
load_all,
311-
)
312-
case cwl_v1_2.LoadingOptions():
313-
loadingOptions = cwl_v1_2.LoadingOptions(
314-
fileuri=real_uri, baseuri=base_uri, copyfrom=loadingOptions
315-
)
316-
return load_document_by_string(
317-
loadingOptions.fetcher.fetch_text(real_uri),
318-
real_uri,
319-
loadingOptions,
320-
id_,
321-
load_all,
322-
)
323-
case None:
324-
loadingOptions = cwl_v1_2.LoadingOptions(fileuri=real_uri, baseuri=base_uri)
325-
return load_document_by_string(
326-
loadingOptions.fetcher.fetch_text(real_uri),
327-
real_uri,
328-
None,
329-
id_,
330-
load_all,
331-
)
332-
case _:
333-
raise ValidationException(
334-
f"Unsupported loadingOptions type: {type(loadingOptions)}"
335-
)
288+
if loadingOptions is None:
289+
loadingOptions = LoadingOptions(fileuri=real_uri, baseuri=base_uri)
290+
291+
return load_document_by_string(
292+
loadingOptions.fetcher.fetch_text(real_uri),
293+
real_uri,
294+
loadingOptions,
295+
id_,
296+
load_all,
297+
)
336298

337299

338300
def load_document(
@@ -344,7 +306,7 @@ def load_document(
344306
) -> Any:
345307
"""Load a CWL object from a serialized YAML string or a YAML object."""
346308
if baseuri is None:
347-
baseuri = cwl_v1_0.file_uri(str(Path.cwd())) + "/"
309+
baseuri = schema_salad.runtime.file_uri(str(Path.cwd())) + "/"
348310
if isinstance(doc, str):
349311
return load_document_by_string(doc, baseuri, loadingOptions, id_)
350312
return load_document_by_yaml(doc, baseuri, loadingOptions, id_, load_all)
@@ -376,17 +338,11 @@ def load_document_by_yaml(
376338
yaml["cwlVersion"] = version
377339
match version:
378340
case "v1.0":
379-
result = cwl_v1_0.load_document_by_yaml(
380-
yaml, uri, cast(Optional[cwl_v1_0.LoadingOptions], loadingOptions)
381-
)
341+
result = cwl_v1_0.load_document_by_yaml(yaml, uri, loadingOptions)
382342
case "v1.1":
383-
result = cwl_v1_1.load_document_by_yaml(
384-
yaml, uri, cast(Optional[cwl_v1_1.LoadingOptions], loadingOptions)
385-
)
343+
result = cwl_v1_1.load_document_by_yaml(yaml, uri, loadingOptions)
386344
case "v1.2":
387-
result = cwl_v1_2.load_document_by_yaml(
388-
yaml, uri, cast(Optional[cwl_v1_2.LoadingOptions], loadingOptions)
389-
)
345+
result = cwl_v1_2.load_document_by_yaml(yaml, uri, loadingOptions)
390346
case None:
391347
raise ValidationException("could not get the cwlVersion")
392348
case _:
@@ -412,7 +368,7 @@ def save(
412368
) -> Any:
413369
"""Convert a CWL Python object into a JSON/YAML serializable object."""
414370
match val:
415-
case cwl_v1_0.Saveable() | cwl_v1_1.Saveable() | cwl_v1_2.Saveable():
371+
case Saveable():
416372
return val.save(top=top, base_url=base_url, relative_uris=relative_uris)
417373
case MutableSequence():
418374
lst = [

0 commit comments

Comments
 (0)