Skip to content

Commit 6f53789

Browse files
authored
Make load_document_by_path applicable to a URI with http and https schemes (#117)
* Rename `load_document_by_path` to `load_document_by_uri`
1 parent d5e0338 commit 6f53789

File tree

4 files changed

+38
-15
lines changed

4 files changed

+38
-15
lines changed

README.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,13 @@ Using the CWL Parsers
6969
from ruamel import yaml
7070
import sys
7171
72-
from cwl_utils.parser import load_document_by_path, save
72+
from cwl_utils.parser import load_document_by_uri, save
7373
7474
# File Input - This is the only thing you will need to adjust or take in as an input to your function:
7575
cwl_file = Path("testdata/md5sum.cwl") # or a plain string works as well
7676
7777
# Import CWL Object
78-
cwl_obj = load_document_by_path(cwl_file)
78+
cwl_obj = load_document_by_uri(cwl_file)
7979
8080
# View CWL Object
8181
print("List of object attributes:\n{}".format("\n".join(map(str, dir(cwl_obj)))))

cwl_utils/parser/__init__.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,21 +65,27 @@ def cwl_version(yaml: Any) -> Any:
6565
return yaml["cwlVersion"]
6666

6767

68-
def load_document_by_path(
68+
def load_document_by_uri(
6969
path: Union[str, Path],
7070
loadingOptions: Optional[LoadingOptions] = None,
7171
) -> Any:
72-
"""Load a CWL object from a path."""
72+
"""Load a CWL object from a URI or a path."""
7373
if isinstance(path, str):
74-
if path.startswith("file:/"):
75-
path = unquote_plus(urlparse(path).path)
76-
real_path = Path(path)
74+
uri = urlparse(path)
75+
if not uri.scheme or uri.scheme == "file":
76+
real_path = Path(unquote_plus(uri.path)).resolve().as_uri()
77+
else:
78+
real_path = path
7779
else:
78-
real_path = path
79-
baseuri = real_path.resolve().as_uri()
80-
with open(path) as handle:
81-
doc = yaml_no_ts().load(handle)
82-
return load_document_by_yaml(doc, baseuri, loadingOptions)
80+
real_path = path.resolve().as_uri()
81+
82+
baseuri = str(real_path)
83+
84+
if loadingOptions is None:
85+
loadingOptions = cwl_v1_2.LoadingOptions(fileuri=baseuri)
86+
87+
doc = loadingOptions.fetcher.fetch_text(real_path)
88+
return load_document_by_string(doc, baseuri, loadingOptions)
8389

8490

8591
def load_document(

tests/load_cwl_by_path.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
from ruamel import yaml
55
import sys
66

7-
from cwl_utils.parser import load_document_by_path, save
7+
from cwl_utils.parser import load_document_by_uri, save
88

99
# File Input - This is the only thing you will need to adjust or take in as an input to your function:
1010
cwl_file = Path("testdata/md5sum.cwl") # or a plain string works as well
1111

1212
# Import CWL Object
13-
cwl_obj = load_document_by_path(cwl_file)
13+
cwl_obj = load_document_by_uri(cwl_file)
1414

1515
# View CWL Object
1616
print("List of object attributes:\n{}".format("\n".join(map(str, dir(cwl_obj)))))

tests/test_parser.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
"""Test the load and save functions for CWL."""
2-
from cwl_utils.parser import cwl_version, load_document, save
2+
from cwl_utils.parser import cwl_version, load_document, load_document_by_uri, save
33
from pathlib import Path
44
from ruamel import yaml
55

66
HERE = Path(__file__).resolve().parent
77
TEST_v1_0_CWL = HERE / "../testdata/md5sum.cwl"
8+
TEST_v1_0_CWL_REMOTE = "https://raw.githubusercontent.com/common-workflow-language/cwl-utils/main/testdata/md5sum.cwl"
89
TEST_v1_2_CWL = HERE / "../testdata/workflow_input_format_expr_v1_2.cwl"
910

1011

@@ -25,6 +26,22 @@ def test_load_document() -> None:
2526
assert cwl_obj.inputs[0].id.endswith("input_file")
2627

2728

29+
def test_load_document_with_local_uri() -> None:
30+
"""Test load_document for a CommandLineTool in a local URI."""
31+
uri = Path(TEST_v1_0_CWL).resolve().as_uri()
32+
assert uri.startswith("file://")
33+
cwl_obj = load_document_by_uri(uri)
34+
assert cwl_obj.cwlVersion == "v1.0"
35+
assert cwl_obj.inputs[0].id.endswith("input_file")
36+
37+
38+
def test_load_document_with_remote_uri() -> None:
39+
"""Test load_document for a CommandLineTool in a remote URI."""
40+
cwl_obj = load_document_by_uri(TEST_v1_0_CWL_REMOTE)
41+
assert cwl_obj.cwlVersion == "v1.0"
42+
assert cwl_obj.inputs[0].id.endswith("input_file")
43+
44+
2845
def test_save() -> None:
2946
"""Test save for a list of Process objects with different cwlVersions."""
3047
with open(TEST_v1_0_CWL, "r") as cwl_h:

0 commit comments

Comments
 (0)