diff --git a/.requirements.txt b/.requirements.txt deleted file mode 100644 index 9eb2b58..0000000 --- a/.requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -pyyaml -pytest diff --git a/pyproject.toml b/pyproject.toml index 8224519..6cf24a9 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -28,7 +28,16 @@ keywords = [ "variation" ] requires-python = ">=3.12" -dynamic = ["version", "dependencies"] +dependencies = [ + "pyyaml" +] +dynamic = ["version"] + +[project.optional-dependencies] +dev = [ + "pytest", + "ruff==0.7.2" +] [project.urls] Homepage = "https://github.com/ga4gh/gks-metaschema" @@ -37,9 +46,6 @@ Changelog = "https://github.com/ga4gh/gks-metaschema/releases" Source = "https://github.com/ga4gh/gks-metaschema" "Bug Tracker" = "https://github.com/ga4gh/gks-metaschema/issues" -[tool.setuptools.dynamic] -dependencies = {file = [".requirements.txt"]} - [tool.setuptools_scm] [project.scripts] diff --git a/src/ga4gh/gks/metaschema/tools/schema_import.py b/src/ga4gh/gks/metaschema/tools/schema_import.py new file mode 100644 index 0000000..49fb5ee --- /dev/null +++ b/src/ga4gh/gks/metaschema/tools/schema_import.py @@ -0,0 +1,24 @@ +import urllib.request +import tempfile + +def construct_url(spec): + return f"https://raw.githubusercontent.com/{spec["org"]}/{spec["repo"]}/{spec["sha"]}/{spec["path"]}" + +def retrieve_import(spec): + url = construct_url(spec) + response = urllib.request.urlopen(url) + with tempfile.NamedTemporaryFile(delete=False) as t: + t.write(response.read()) + return t.name + +## Leaving 'Rich' comments: + +# test_spec = {"org":"clingen-data-model", +# "repo":"gks-metaschema", +# "path":"tests/data/vrs/vrs.yaml", +# "sha":"fe2995181d06896f567f81ca4550b3b02830b1e5"} + +# path = retrieve_import(test_spec) + +# print(path) + diff --git a/src/ga4gh/gks/metaschema/tools/source_proc.py b/src/ga4gh/gks/metaschema/tools/source_proc.py index 2bdde49..cdb4f0e 100755 --- a/src/ga4gh/gks/metaschema/tools/source_proc.py +++ b/src/ga4gh/gks/metaschema/tools/source_proc.py @@ -7,6 +7,7 @@ from pathlib import Path from collections import defaultdict from urllib.parse import urlparse +from ga4gh.gks.metaschema.tools.schema_import import retrieve_import SCHEMA_DEF_KEYWORD_BY_VERSION = { "https://json-schema.org/draft-07/schema": "definitions", @@ -183,17 +184,31 @@ def load_schema(schema_fp): schema = yaml.load(f, Loader=yaml.SafeLoader) return schema + def import_dependency_from_path(self, dependency, fp): + if self.imported: + root_fp = self.root_schema_fp + else: + root_fp = self.schema_fp + self.imports[dependency] = YamlSchemaProcessor(fp, root_fp=root_fp) + + def import_embedded_dependency(self, dependency): + fp = Path(self.raw_schema['imports'][dependency]) + if not fp.is_absolute(): + base_path = self.schema_fp.parent + fp = base_path.joinpath(fp) + self.import_dependency_from_path(dependency, fp) + + def import_github_reference_dependency(self, dependency): + spec = self.raw_schema['imports'][dependency] + ref_path = retrieve_import(spec) + self.import_dependency_from_path(dependency, ref_path) + def import_dependencies(self): for dependency in self.raw_schema.get('imports', list()): - fp = Path(self.raw_schema['imports'][dependency]) - if not fp.is_absolute(): - base_path = self.schema_fp.parent - fp = base_path.joinpath(fp) - if self.imported: - root_fp = self.root_schema_fp + if isinstance(self.raw_schema['imports'][dependency], str): + self.import_embedded_dependency(dependency) else: - root_fp = self.schema_fp - self.imports[dependency] = YamlSchemaProcessor(fp, root_fp=root_fp) + self.import_github_reference_dependency(dependency) def process_schema(self): if self.defs is None: diff --git a/tests/data/vrs/vrs-source.yaml b/tests/data/vrs/vrs-source.yaml index f024454..884ef51 100644 --- a/tests/data/vrs/vrs-source.yaml +++ b/tests/data/vrs/vrs-source.yaml @@ -15,8 +15,13 @@ type: object strict: true imports: - gks.core: ../gks-common/core-source.yaml - + # gks.core: ../gks-common/core-source.yaml + gks.core: + org: clingen-data-model + repo: gks-metaschema + path: tests/data/gks-common/core-source.yaml + sha: fe2995181d06896f567f81ca4550b3b02830b1e5 + namespaces: gks.common: ../gks-common/$defs/ diff --git a/tests/test_basic.py b/tests/test_basic.py index ddbe756..3ceb1e8 100644 --- a/tests/test_basic.py +++ b/tests/test_basic.py @@ -4,11 +4,15 @@ import os from pathlib import Path +import ga4gh.gks.metaschema.tools.schema_import + from ga4gh.gks.metaschema.tools.source_proc import YamlSchemaProcessor +from ga4gh.gks.metaschema.tools.schema_import import construct_url from ga4gh.gks.metaschema.scripts.y2t import main as y2t from ga4gh.gks.metaschema.scripts.source2splitjs import split_defs_to_js from ga4gh.gks.metaschema.scripts.source2classes import main as s2c + root = Path(__file__).parent processor = YamlSchemaProcessor(root / 'data/vrs/vrs-source.yaml') @@ -63,3 +67,13 @@ def test_docs_create(): os.makedirs(defs) y2t(processor) assert True + +def test_imports(): + spec = {"org":"clingen-data-model", + "repo":"gks-metaschema", + "path":"tests/data/vrs/vrs.yaml", + "sha":"fe2995181d06896f567f81ca4550b3b02830b1e5"} + target_string = "https://raw.githubusercontent.com/clingen-data-model/gks-metaschema/fe2995181d06896f567f81ca4550b3b02830b1e5/tests/data/vrs/vrs.yaml" + assert target_string == construct_url(spec) + +