Skip to content

Commit

Permalink
Merge pull request #162 from simleo/extra_context
Browse files Browse the repository at this point in the history
Add method to add contexts
  • Loading branch information
simleo authored Sep 22, 2023
2 parents 1e75c3f + 41b05ae commit b3e24ba
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
8 changes: 6 additions & 2 deletions rocrate/model/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def __init__(self, crate, source=None, dest_path=None, properties=None):
properties=properties
)
# https://www.researchobject.org/ro-crate/1.1/appendix/jsonld.html#extending-ro-crate
self.extra_contexts = []
self.extra_terms = {}

def _empty(self):
Expand All @@ -63,9 +64,12 @@ def generate(self):
graph = []
for entity in self.crate.get_entities():
graph.append(entity.properties())
context = f'{self.PROFILE}/context'
context = [f'{self.PROFILE}/context']
context.extend(self.extra_contexts)
if self.extra_terms:
context = [context, self.extra_terms]
context.append(self.extra_terms)
if len(context) == 1:
context = context[0]
return {'@context': context, '@graph': graph}

def write(self, base_path):
Expand Down
15 changes: 15 additions & 0 deletions test/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,3 +460,18 @@ def test_get_by_type(test_data_dir):
assert crate.get_by_type(["File", "ComputationalWorkflow"]) == [wf]
assert crate.get_by_type("Person") == []
assert crate.get_by_type("Dataset") == [crate.root_dataset]


def test_context(helpers):
crate = ROCrate()
jsonld = crate.metadata.generate()
base_context = f"{helpers.PROFILE}/context"
assert jsonld["@context"] == base_context
wfrun_ctx = "https://w3id.org/ro/terms/workflow-run"
crate.metadata.extra_contexts.append(wfrun_ctx)
jsonld = crate.metadata.generate()
assert jsonld["@context"] == [base_context, wfrun_ctx]
k, v = "runsOn", "https://w3id.org/ro/terms/test#runsOn"
crate.metadata.extra_terms[k] = v
jsonld = crate.metadata.generate()
assert jsonld["@context"] == [base_context, wfrun_ctx, {k: v}]

0 comments on commit b3e24ba

Please sign in to comment.