Skip to content

Commit 631a835

Browse files
authored
Allow $schemas in codegen (#346)
1 parent 8bb0956 commit 631a835

File tree

4 files changed

+92
-6
lines changed

4 files changed

+92
-6
lines changed

schema_salad/python_codegen.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -246,9 +246,12 @@ def end_class(self, classname, field_names):
246246

247247
self.serializer.write(
248248
"""
249-
if top and self.loadingOptions.namespaces:
250-
r["$namespaces"] = self.loadingOptions.namespaces
251-
249+
# top refers to the directory level
250+
if top:
251+
if self.loadingOptions.namespaces:
252+
r["$namespaces"] = self.loadingOptions.namespaces
253+
if self.loadingOptions.schemas:
254+
r["$schemas"] = self.loadingOptions.schemas
252255
"""
253256
)
254257

schema_salad/python_codegen_support.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,15 @@ def __init__(
4444
self,
4545
fetcher=None, # type: Optional[Fetcher]
4646
namespaces=None, # type: Optional[Dict[str, str]]
47+
schemas=None, # type: Optional[Dict[str, str]]
4748
fileuri=None, # type: Optional[str]
4849
copyfrom=None, # type: Optional[LoadingOptions]
4950
original_doc=None, # type: Optional[Any]
5051
): # type: (...) -> None
5152
self.idx = {} # type: Dict[str, Dict[str, Any]]
5253
self.fileuri = fileuri # type: Optional[str]
5354
self.namespaces = namespaces
55+
self.schemas = schemas
5456
self.original_doc = original_doc
5557
if copyfrom is not None:
5658
self.idx = copyfrom.idx
@@ -60,6 +62,8 @@ def __init__(
6062
self.fileuri = copyfrom.fileuri
6163
if namespaces is None:
6264
self.namespaces = copyfrom.namespaces
65+
if schemas is None:
66+
self.schemas = copyfrom.schemas
6367

6468
if fetcher is None:
6569
import requests
@@ -466,11 +470,13 @@ def _document_load(loader, doc, baseuri, loadingOptions):
466470
)
467471

468472
if isinstance(doc, MutableMapping):
469-
if "$namespaces" in doc:
473+
if "$namespaces" in doc or "$schemas" in doc:
470474
loadingOptions = LoadingOptions(
471-
copyfrom=loadingOptions, namespaces=doc["$namespaces"]
475+
copyfrom=loadingOptions,
476+
namespaces=doc.get("$namespaces", None),
477+
schemas=doc.get("$schemas", None),
472478
)
473-
doc = {k: v for k, v in doc.items() if k != "$namespaces"}
479+
doc = {k: v for k, v in doc.items() if k not in ["$namespaces", "$schemas"]}
474480

475481
if "$base" in doc:
476482
baseuri = doc["$base"]

schema_salad/tests/formattest2.cwl

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
$namespaces:
2+
edam: http://edamontology.org/
3+
$schemas:
4+
- EDAM.owl
5+
class: CommandLineTool
6+
cwlVersion: v1.0
7+
doc: "Reverse each line using the `rev` command"
8+
hints:
9+
ResourceRequirement:
10+
ramMin: 8
11+
DockerRequirement:
12+
dockerPull: "debian:stretch-slim"
13+
14+
inputs:
15+
input:
16+
type: File
17+
inputBinding: {}
18+
format: edam:format_2330
19+
20+
outputs:
21+
output:
22+
type: File
23+
outputBinding:
24+
glob: output.txt
25+
format: $(inputs.input.format)
26+
27+
baseCommand: rev
28+
stdout: output.txt
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
"""
2+
Checks for accepting $schemas directive
3+
4+
run individually as py.test -k tests/test_schemas_directive.py
5+
"""
6+
7+
from typing import Any, Dict, Union, Tuple
8+
from schema_salad.avro.schema import Names, SchemaParseException
9+
from schema_salad.schema import load_and_validate, load_schema
10+
from schema_salad.ref_resolver import Loader
11+
import os
12+
13+
from .util import get_data
14+
15+
test_dir_name = "tests/"
16+
17+
18+
class TestSchemasDirective:
19+
"""Ensure codegen-produced parsers accept $schemas directives"""
20+
21+
document_loader = None # type: Loader
22+
avsc_names = None # type: Union[Names, SchemaParseException]
23+
schema_metadata = None # type: Dict[str, Any]
24+
metaschema_loader = None # type: Loader
25+
26+
@classmethod
27+
def setup_class(cls) -> None:
28+
path = get_data("tests/test_schema/CommonWorkflowLanguage.yml")
29+
assert path
30+
(
31+
cls.document_loader,
32+
cls.avsc_names,
33+
schema_metadata,
34+
metaschema_loader,
35+
) = load_schema(path)
36+
37+
def load_cwl(self, src: str) -> Tuple[Any, Dict[str, Any]]:
38+
path = get_data(test_dir_name + src)
39+
assert path
40+
assert isinstance(self.avsc_names, Names)
41+
res = load_and_validate(self.document_loader, self.avsc_names, path, True)
42+
return res
43+
44+
def test_dollarsign_schema(self) -> None:
45+
"""EDAM.owl as a schema"""
46+
res = self.load_cwl(src="formattest2.cwl")
47+
48+
# EDAM.owl resides in this directory
49+
assert os.path.split(str(res[0]["$schemas"][0]))[1] == "EDAM.owl"

0 commit comments

Comments
 (0)