Skip to content

Commit 37e0ad8

Browse files
committed
python codegen shortname() plus test
1 parent 0e9e8f5 commit 37e0ad8

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

schema_salad/metaschema.py

Lines changed: 13 additions & 1 deletion
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,18 @@ 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+
677689
def parser_info() -> str:
678690
return "org.w3id.cwl.salad"
679691

schema_salad/python_codegen_support.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
Type,
2020
Union,
2121
)
22-
from urllib.parse import quote, urlsplit, urlunsplit
22+
from urllib.parse import quote, urlsplit, urlunsplit, urlparse
2323
from urllib.request import pathname2url
2424

2525
from ruamel.yaml.comments import CommentedMap
@@ -669,3 +669,15 @@ def save_relative_uri(
669669
return uri
670670
else:
671671
return save(uri, top=False, base_url=base_url)
672+
673+
674+
def shortname(inputid: str) -> str:
675+
"""
676+
Compute the shortname of a fully qualified identifer.
677+
678+
See https://w3id.org/cwl/v1.2/SchemaSalad.html#Short_names.
679+
"""
680+
parsed_id = urlparse(inputid)
681+
if parsed_id.fragment:
682+
return parsed_id.fragment.split("/")[-1]
683+
return parsed_id.path.split("/")[-1]

schema_salad/tests/test_cg.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,16 @@ def test_load_pt() -> None:
177177
] == doc.symbols
178178

179179

180+
def test_shortname() -> None:
181+
"""Test shortname() function."""
182+
assert cg_metaschema.shortname("http://example.com/foo") == "foo"
183+
assert cg_metaschema.shortname("http://example.com/#bar") == "bar"
184+
assert cg_metaschema.shortname("http://example.com/foo/bar") == "bar"
185+
assert cg_metaschema.shortname("http://example.com/foo#bar") == "bar"
186+
assert cg_metaschema.shortname("http://example.com/#foo/bar") == "bar"
187+
assert cg_metaschema.shortname("http://example.com/foo#bar/baz") == "baz"
188+
189+
180190
@pytest.fixture
181191
def metaschema_pre() -> Any:
182192
"""Prep-parsed schema for testing."""

0 commit comments

Comments
 (0)