Skip to content

Commit 1fa4e16

Browse files
committed
refresh parsers to pick up new shortname helper function
Add test for shortname
1 parent 937e978 commit 1fa4e16

File tree

4 files changed

+76
-11
lines changed

4 files changed

+76
-11
lines changed

cwl_utils/parser/cwl_v1_0.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
# This file was autogenerated using schema-salad-tool --codegen=python
33
# The code itself is released under the Apache 2.0 license and the help text is
44
# subject to the license of the original schema.
5-
from abc import ABC, abstractmethod
65
import copy
76
import os
87
import pathlib
98
import re
109
import tempfile
1110
import uuid as _uuid__ # pylint: disable=unused-import # noqa: F401
11+
from abc import ABC, abstractmethod
1212
from io import StringIO
1313
from typing import (
1414
Any,
@@ -22,7 +22,7 @@
2222
Type,
2323
Union,
2424
)
25-
from urllib.parse import quote, urlsplit, urlunsplit
25+
from urllib.parse import quote, urlsplit, urlunsplit, urlparse
2626
from urllib.request import pathname2url
2727

2828
from ruamel.yaml.comments import CommentedMap
@@ -337,6 +337,7 @@ def load(self, doc, baseuri, loadingOptions, docRoot=None):
337337
d
338338
)
339339
)
340+
r.append(new_dict)
340341

341342
else:
342343
raise ValidationException(
@@ -360,6 +361,7 @@ def load(self, doc, baseuri, loadingOptions, docRoot=None):
360361
doc
361362
)
362363
)
364+
r.append(new_dict)
363365

364366
elif isinstance(doc, str):
365367
if doc.endswith("?"):
@@ -672,6 +674,22 @@ def save_relative_uri(
672674
return save(uri, top=False, base_url=base_url)
673675

674676

677+
def shortname(inputid: str) -> str:
678+
"""
679+
Compute the shortname of a fully qualified identifer.
680+
681+
See https://w3id.org/cwl/v1.2/SchemaSalad.html#Short_names.
682+
"""
683+
parsed_id = urlparse(inputid)
684+
if parsed_id.fragment:
685+
return parsed_id.fragment.split("/")[-1]
686+
return parsed_id.path.split("/")[-1]
687+
688+
689+
def parser_info() -> str:
690+
return "org.w3id.cwl.v1_0"
691+
692+
675693
class RecordField(Savable):
676694
"""
677695
A field of a record.
@@ -11431,7 +11449,7 @@ def save(
1143111449
"https://w3id.org/cwl/cwl#v1.0.dev4": "v1.0.dev4",
1143211450
}
1143311451

11434-
strtype = _PrimitiveLoader((str, str))
11452+
strtype = _PrimitiveLoader(str)
1143511453
inttype = _PrimitiveLoader(int)
1143611454
floattype = _PrimitiveLoader(float)
1143711455
booltype = _PrimitiveLoader(bool)

cwl_utils/parser/cwl_v1_1.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
Type,
2323
Union,
2424
)
25-
from urllib.parse import quote, urlsplit, urlunsplit
25+
from urllib.parse import quote, urlsplit, urlunsplit, urlparse
2626
from urllib.request import pathname2url
2727

2828
from ruamel.yaml.comments import CommentedMap
@@ -674,6 +674,22 @@ def save_relative_uri(
674674
return save(uri, top=False, base_url=base_url)
675675

676676

677+
def shortname(inputid: str) -> str:
678+
"""
679+
Compute the shortname of a fully qualified identifer.
680+
681+
See https://w3id.org/cwl/v1.2/SchemaSalad.html#Short_names.
682+
"""
683+
parsed_id = urlparse(inputid)
684+
if parsed_id.fragment:
685+
return parsed_id.fragment.split("/")[-1]
686+
return parsed_id.path.split("/")[-1]
687+
688+
689+
def parser_info() -> str:
690+
return "org.w3id.cwl.v1_1"
691+
692+
677693
class Documented(Savable):
678694
pass
679695

@@ -13230,7 +13246,7 @@ def save(
1323013246
"https://w3id.org/cwl/cwl#v1.1.0-dev1": "v1.1.0-dev1",
1323113247
}
1323213248

13233-
strtype = _PrimitiveLoader((str, str))
13249+
strtype = _PrimitiveLoader(str)
1323413250
inttype = _PrimitiveLoader(int)
1323513251
floattype = _PrimitiveLoader(float)
1323613252
booltype = _PrimitiveLoader(bool)

cwl_utils/parser/cwl_v1_2.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
Type,
2323
Union,
2424
)
25-
from urllib.parse import quote, urlsplit, urlunsplit
25+
from urllib.parse import quote, urlsplit, urlunsplit, urlparse
2626
from urllib.request import pathname2url
2727

2828
from ruamel.yaml.comments import CommentedMap
@@ -674,6 +674,22 @@ def save_relative_uri(
674674
return save(uri, top=False, base_url=base_url)
675675

676676

677+
def shortname(inputid: str) -> str:
678+
"""
679+
Compute the shortname of a fully qualified identifer.
680+
681+
See https://w3id.org/cwl/v1.2/SchemaSalad.html#Short_names.
682+
"""
683+
parsed_id = urlparse(inputid)
684+
if parsed_id.fragment:
685+
return parsed_id.fragment.split("/")[-1]
686+
return parsed_id.path.split("/")[-1]
687+
688+
689+
def parser_info() -> str:
690+
return "org.w3id.cwl.v1_2"
691+
692+
677693
class Documented(Savable):
678694
pass
679695

@@ -1205,8 +1221,8 @@ class File(Savable):
12051221
a number of properties that provide metadata about the file.
12061222

12071223
The `location` property of a File is a URI that uniquely identifies the
1208-
file. Implementations must support the file:// URI scheme and may support
1209-
other schemes such as http://. The value of `location` may also be a
1224+
file. Implementations must support the `file://` URI scheme and may support
1225+
other schemes such as `http://` and `https://`. The value of `location` may also be a
12101226
relative reference, in which case it must be resolved relative to the URI
12111227
of the document it appears in. Alternately to `location`, implementations
12121228
must also accept the `path` property on File, which must be a filesystem
@@ -3972,7 +3988,7 @@ class SchemaDefRequirement(ProcessRequirement):
39723988
"""
39733989
This field consists of an array of type definitions which must be used when
39743990
interpreting the `inputs` and `outputs` fields. When a `type` field
3975-
contain a IRI, the implementation must check if the type is defined in
3991+
contains a IRI, the implementation must check if the type is defined in
39763992
`schemaDefs` and use that definition. If the type is not found in
39773993
`schemaDefs`, it is an error. The entries in `schemaDefs` must be
39783994
processed in the order listed such that later schema definitions may refer
@@ -14507,7 +14523,7 @@ def save(
1450714523
"https://w3id.org/cwl/cwl#v1.2.0-dev5": "v1.2.0-dev5",
1450814524
}
1450914525

14510-
strtype = _PrimitiveLoader((str, str))
14526+
strtype = _PrimitiveLoader(str)
1451114527
inttype = _PrimitiveLoader(int)
1451214528
floattype = _PrimitiveLoader(float)
1451314529
booltype = _PrimitiveLoader(bool)

tests/test_parser.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@
33

44
from ruamel import yaml
55

6-
from cwl_utils.parser import cwl_version, load_document, load_document_by_uri, save
6+
from cwl_utils.parser import (
7+
cwl_version,
8+
load_document,
9+
load_document_by_uri,
10+
save,
11+
cwl_v1_2,
12+
)
713
import cwl_utils.parser.latest as latest
814

915
HERE = Path(__file__).resolve().parent
@@ -69,3 +75,12 @@ def test_latest_parser() -> None:
6975
yaml_obj12 = yaml.main.round_trip_load(cwl_h, preserve_quotes=True)
7076
latest_cwl_obj = latest.load_document_by_yaml(yaml_obj12, uri) # type: ignore
7177
assert latest_cwl_obj.cwlVersion == "v1.2"
78+
79+
80+
def test_shortname() -> None:
81+
assert cwl_v1_2.shortname("http://example.com/foo") == "foo"
82+
assert cwl_v1_2.shortname("http://example.com/#bar") == "bar"
83+
assert cwl_v1_2.shortname("http://example.com/foo/bar") == "bar"
84+
assert cwl_v1_2.shortname("http://example.com/foo#bar") == "bar"
85+
assert cwl_v1_2.shortname("http://example.com/#foo/bar") == "bar"
86+
assert cwl_v1_2.shortname("http://example.com/foo#bar/baz") == "baz"

0 commit comments

Comments
 (0)