Skip to content

Commit

Permalink
Merge pull request #64 from djarecka/fix_version
Browse files Browse the repository at this point in the history
updates to redcap2rs and jsonldutils
  • Loading branch information
djarecka authored Jul 9, 2024
2 parents dd4ea02 + a8a63b6 commit 4b3c3d2
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 13 deletions.
49 changes: 37 additions & 12 deletions reproschema/jsonldutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,7 @@ def load_file(
data = fixing_old_schema(data, copy_data=True)
if compact:
if compact_context:
if _is_file(compact_context):
with open(compact_context) as fp:
context = json.load(fp)
elif _is_url(compact_context):
context = _fetch_jsonld_context(compact_context)
else:
raise Exception(
f"compact_context has tobe a file or url, but {compact_context} provided"
)
context = read_contextfile(compact_context)
if _is_file(path_or_url):
data = jsonld.compact(
data, ctx=context, options={"base": base_url}
Expand Down Expand Up @@ -128,7 +120,7 @@ def validate_data(data):
# normalized = jsonld.normalize(data, kwargs)
obj_type = identify_model_class(data["@type"][0])
data_fixed = [fixing_old_schema(data, copy_data=True)]
context = _fetch_jsonld_context(CONTEXTFILE_URL)
context = read_contextfile(CONTEXTFILE_URL)
data_fixed_comp = jsonld.compact(data_fixed, context)
del data_fixed_comp["@context"]
conforms = False
Expand All @@ -141,6 +133,40 @@ def validate_data(data):
return conforms, v_text


def read_contextfile(contextfile):
"""Read a context file and return the context."""
if _is_file(contextfile):
with open(contextfile) as fp:
context = json.load(fp)
elif _is_url(contextfile):
context = _fetch_jsonld_context(contextfile)
else:
raise Exception(
f"compact_context has tobe a file or url, but {contextfile} provided"
)
return context


def get_context_version(contextfile):
"""Get the version from the context file path"""
from packaging.version import InvalidVersion, Version

if contextfile.split("/")[-3] != "releases":
raise ValueError(
f"Can't get the version from {contextfile}, expected to have releases in the path"
)
else:
try:
Version(contextfile.split("/")[-2])
return contextfile.split("/")[-2]
except InvalidVersion:
raise ValueError(
f"Can't get the version from {contextfile}, "
f"expected to have a valid version in the path, "
f"but got {contextfile.split('/')[-2]}"
)


def to_newformat(path, format, prefixfile=None, contextfile=None):
"""Convert a JSONLD document to n-triples format
Expand Down Expand Up @@ -171,8 +197,7 @@ def to_newformat(path, format, prefixfile=None, contextfile=None):
data = load_file(path)
if format == "jsonld":
if contextfile is not None:
with open(contextfile) as fp:
context = json.load(fp)
context = read_contextfile(contextfile)
data = jsonld.compact(data, context)
return json.dumps(data, indent=2)
kwargs = {"algorithm": "URDNA2015", "format": "application/n-quads"}
Expand Down
3 changes: 2 additions & 1 deletion reproschema/redcap2reproschema.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from bs4 import BeautifulSoup

from .context_url import CONTEXTFILE_URL
from .jsonldutils import get_context_version
from .models import Activity, Item, Protocol, write_obj_jsonld

matrix_group_count = {}
Expand Down Expand Up @@ -378,7 +379,7 @@ def create_form_schema(
"id": f"{form_name}_schema",
"prefLabel": {"en": activity_display_name},
# "description": {"en": activity_description},
"schemaVersion": "1.0.0-rc4",
"schemaVersion": get_context_version(schema_context_url),
"version": redcap_version,
"ui": {
"order": unique_order,
Expand Down

0 comments on commit 4b3c3d2

Please sign in to comment.