Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Git ref imports #30

Open
wants to merge 5 commits into
base: git-ref-imports
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .requirements.txt

This file was deleted.

14 changes: 10 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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]
Expand Down
24 changes: 24 additions & 0 deletions src/ga4gh/gks/metaschema/tools/schema_import.py
Original file line number Diff line number Diff line change
@@ -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)

31 changes: 23 additions & 8 deletions src/ga4gh/gks/metaschema/tools/source_proc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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:
Expand Down
9 changes: 7 additions & 2 deletions tests/data/vrs/vrs-source.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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/

Expand Down
14 changes: 14 additions & 0 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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)