Skip to content

Commit 8f9e407

Browse files
tetronmr-c
andauthored
Add basic tracking of $import and $include dependencies. (#650)
* Add basic tracking of $import and $include dependencies. Currently does not round trip them but is suitable for dependency management. * Update metaschema.py * GA: pin tox to 3.x Co-authored-by: Michael R. Crusoe <[email protected]>
1 parent 9a7c109 commit 8f9e407

File tree

4 files changed

+46
-10
lines changed

4 files changed

+46
-10
lines changed

.github/workflows/ci-tests.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ jobs:
4646
- name: Upgrade setuptools and install tox
4747
run: |
4848
pip install -U pip setuptools wheel
49-
pip install tox tox-gh-actions
49+
pip install 'tox<4' tox-gh-actions
5050
5151
- name: MyPy cache
5252
if: ${{ matrix.step == 'mypy' }}
@@ -75,7 +75,7 @@ jobs:
7575

7676
env:
7777
py-semver: "3.11"
78-
TOXENV: ${{ format('py310-{0}', matrix.step) }}
78+
TOXENV: ${{ format('py311-{0}', matrix.step) }}
7979

8080
steps:
8181
- uses: actions/checkout@v3
@@ -96,7 +96,7 @@ jobs:
9696
- name: Upgrade setuptools and install tox
9797
run: |
9898
pip install -U pip setuptools wheel
99-
pip install tox tox-gh-actions
99+
pip install 'tox<4' tox-gh-actions
100100
101101
- if: ${{ matrix.step == 'pydocstyle' && github.event_name == 'pull_request'}}
102102
name: Create local branch for diff-quality for PRs

README.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ using the `CWL v1.2 schema <https://github.com/common-workflow-language/cwl-v1.2
107107
as an example.
108108

109109
+-------------+---------------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
110-
| Language | Repository | Serializaton Example | Deserialization Example |
110+
| Language | Repository | Serialization Example | Deserialization Example |
111111
+=============+=========================================================+======================================================================================================================================================+============================================================================================================================================================================+
112112
| Python | https://github.com/common-workflow-language/cwl-utils/ | `create_cwl_from_objects.py <https://github.com/common-workflow-language/cwl-utils/blob/main/create_cwl_from_objects.py>`_ | `load_document() <https://github.com/common-workflow-language/cwl-utils/blob/main/cwl_utils/parser/__init__.py#L93>`_ |
113113
+-------------+---------------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

schema_salad/metaschema.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ class LoadingOptions:
5858
vocab: Dict[str, str]
5959
rvocab: Dict[str, str]
6060
cache: CacheType
61+
imports: List[str]
62+
includes: List[str]
6163

6264
def __init__(
6365
self,
@@ -70,6 +72,8 @@ def __init__(
7072
addl_metadata: Optional[Dict[str, str]] = None,
7173
baseuri: Optional[str] = None,
7274
idx: Optional[IdxType] = None,
75+
imports: Optional[List[str]] = None,
76+
includes: Optional[List[str]] = None,
7377
) -> None:
7478
"""Create a LoadingOptions object."""
7579
self.original_doc = original_doc
@@ -104,6 +108,16 @@ def __init__(
104108
else:
105109
self.addl_metadata = copyfrom.addl_metadata if copyfrom is not None else {}
106110

111+
if imports is not None:
112+
self.imports = imports
113+
else:
114+
self.imports = copyfrom.imports if copyfrom is not None else []
115+
116+
if includes is not None:
117+
self.includes = includes
118+
else:
119+
self.includes = copyfrom.includes if copyfrom is not None else []
120+
107121
if fetcher is not None:
108122
self.fetcher = fetcher
109123
elif copyfrom is not None:
@@ -203,18 +217,22 @@ def load_field(val, fieldtype, baseuri, loadingOptions):
203217
if "$import" in val:
204218
if loadingOptions.fileuri is None:
205219
raise SchemaSaladException("Cannot load $import without fileuri")
220+
url = loadingOptions.fetcher.urljoin(loadingOptions.fileuri, val["$import"])
206221
result, metadata = _document_load_by_url(
207222
fieldtype,
208-
loadingOptions.fetcher.urljoin(loadingOptions.fileuri, val["$import"]),
223+
url,
209224
loadingOptions,
210225
)
226+
loadingOptions.imports.append(url)
211227
return result
212228
elif "$include" in val:
213229
if loadingOptions.fileuri is None:
214230
raise SchemaSaladException("Cannot load $import without fileuri")
215-
val = loadingOptions.fetcher.fetch_text(
216-
loadingOptions.fetcher.urljoin(loadingOptions.fileuri, val["$include"])
231+
url = loadingOptions.fetcher.urljoin(
232+
loadingOptions.fileuri, val["$include"]
217233
)
234+
val = loadingOptions.fetcher.fetch_text(url)
235+
loadingOptions.includes.append(url)
218236
return fieldtype.load(val, baseuri, loadingOptions)
219237

220238

schema_salad/python_codegen_support.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ class LoadingOptions:
5555
vocab: Dict[str, str]
5656
rvocab: Dict[str, str]
5757
cache: CacheType
58+
imports: List[str]
59+
includes: List[str]
5860

5961
def __init__(
6062
self,
@@ -67,6 +69,8 @@ def __init__(
6769
addl_metadata: Optional[Dict[str, str]] = None,
6870
baseuri: Optional[str] = None,
6971
idx: Optional[IdxType] = None,
72+
imports: Optional[List[str]] = None,
73+
includes: Optional[List[str]] = None,
7074
) -> None:
7175
"""Create a LoadingOptions object."""
7276
self.original_doc = original_doc
@@ -101,6 +105,16 @@ def __init__(
101105
else:
102106
self.addl_metadata = copyfrom.addl_metadata if copyfrom is not None else {}
103107

108+
if imports is not None:
109+
self.imports = imports
110+
else:
111+
self.imports = copyfrom.imports if copyfrom is not None else []
112+
113+
if includes is not None:
114+
self.includes = includes
115+
else:
116+
self.includes = copyfrom.includes if copyfrom is not None else []
117+
104118
if fetcher is not None:
105119
self.fetcher = fetcher
106120
elif copyfrom is not None:
@@ -200,18 +214,22 @@ def load_field(val, fieldtype, baseuri, loadingOptions):
200214
if "$import" in val:
201215
if loadingOptions.fileuri is None:
202216
raise SchemaSaladException("Cannot load $import without fileuri")
217+
url = loadingOptions.fetcher.urljoin(loadingOptions.fileuri, val["$import"])
203218
result, metadata = _document_load_by_url(
204219
fieldtype,
205-
loadingOptions.fetcher.urljoin(loadingOptions.fileuri, val["$import"]),
220+
url,
206221
loadingOptions,
207222
)
223+
loadingOptions.imports.append(url)
208224
return result
209225
elif "$include" in val:
210226
if loadingOptions.fileuri is None:
211227
raise SchemaSaladException("Cannot load $import without fileuri")
212-
val = loadingOptions.fetcher.fetch_text(
213-
loadingOptions.fetcher.urljoin(loadingOptions.fileuri, val["$include"])
228+
url = loadingOptions.fetcher.urljoin(
229+
loadingOptions.fileuri, val["$include"]
214230
)
231+
val = loadingOptions.fetcher.fetch_text(url)
232+
loadingOptions.includes.append(url)
215233
return fieldtype.load(val, baseuri, loadingOptions)
216234

217235

0 commit comments

Comments
 (0)