Skip to content

Commit

Permalink
update osfmap_json deriver
Browse files Browse the repository at this point in the history
  • Loading branch information
aaxelb committed Oct 21, 2024
1 parent 7558ec1 commit 645aa3c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 16 deletions.
5 changes: 5 additions & 0 deletions tests/trove/derive/_inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
OSFMAP,
FOAF,
OWL,
PROV,
SHAREv2,
)

Expand Down Expand Up @@ -166,6 +167,10 @@ class DeriverTestDoc:
OSFMAP.contains: {'https://osf.example/2ph9b'},
OSFMAP.hostingInstitution: {'https://cos.example/'},
OSFMAP.keyword: {rdf.literal('Demo'), rdf.literal('IA'), rdf.literal('IMLS'), rdf.literal('OSF')},
PROV.qualifiedAttribution: {rdf.blanknode({
DCAT.hadRole: {OSFMAP['admin-contributor']},
PROV.agent: {'https://osf.example/bhcjn'},
})},
},
'https://osf.example/2ph9b': {
RDF.type: {OSFMAP.File},
Expand Down
4 changes: 0 additions & 4 deletions tests/trove/derive/test_osfmap_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,6 @@ def assert_derived_texts_equal(self, expected, actual):
]
}
],
'prov:qualifiedAttribution': [{
'prov:agent': [{'@id': 'https://osf.example/bhcjn'}],
'dcat:hadRole': [{'@id': 'osf:admin-contributor'}],
}],
"dateCopyrighted": [
{"@value": "2021"}
],
Expand Down
31 changes: 19 additions & 12 deletions trove/derive/osfmap_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def rdfobject_as_jsonld(self, rdfobject: rdf.RdfObject) -> dict:
rdf.twopledict_from_twopleset(rdfobject),
)
elif isinstance(rdfobject, rdf.Literal):
if not rdfobject.datatype_iris:
if not rdfobject.datatype_iris or rdfobject.datatype_iris == {RDF.string}:
return {'@value': rdfobject.unicode_value}
if RDF.JSON in rdfobject.datatype_iris:
# NOTE: does not reset jsonld context (is that a problem?)
Expand All @@ -62,13 +62,16 @@ def rdfobject_as_jsonld(self, rdfobject: rdf.RdfObject) -> dict:
'@language': _language_tag,
}
# datatype iri (or non-standard language iri)
_datatype_iris = sorted(
(
osfmap_shorthand().compact_iri(_datatype_iri)
for _datatype_iri in rdfobject.datatype_iris
),
key=len,
)
return {
'@value': rdfobject.unicode_value,
'@type': (
list(rdfobject.datatype_iris)
if len(rdfobject.datatype_iris) > 1
else next(iter(rdfobject.datatype_iris))
),
'@type': (_datatype_iris if (len(_datatype_iris) > 1) else _datatype_iris[0]),
}
elif isinstance(rdfobject, str):
return {'@id': osfmap_shorthand().compact_iri(rdfobject)}
Expand Down Expand Up @@ -111,7 +114,7 @@ def __nested_rdfobject_as_jsonld(
_nested_obj = (
{}
if rdfobject.startswith('_:') # HACK: non-blank blank nodes (stop that)
else {'@id': rdfobject}
else {'@id': osfmap_shorthand().compact_iri(rdfobject)}
)
for _pred, _objectset in tripledict[rdfobject].items():
_label = osfmap_shorthand().compact_iri(_pred)
Expand All @@ -126,22 +129,26 @@ def __nested_rdfobject_as_jsonld(
self.__nestvisiting_iris.discard(rdfobject)
return _nested_obj

def _list_or_single_value(self, predicate_iri, objectset):
def _list_or_single_value(self, predicate_iri, json_list: list):
_only_one_object = OWL.FunctionalProperty in (
OSFMAP_THESAURUS
.get(predicate_iri, {})
.get(RDF.type, ())
)
if _only_one_object:
if len(objectset) > 1:
if len(json_list) > 1:
raise trove_exceptions.OwlObjection((
f'expected at most one object for <{predicate_iri}>'
f' (got {objectset})'
f' (got {json_list})'
))
try:
(_only_obj,) = objectset
(_only_obj,) = json_list
except ValueError:
return None
else:
return _only_obj
return list(objectset)
return (
sorted(json_list, key=json.dumps)
if len(json_list) > 1
else json_list
)

0 comments on commit 645aa3c

Please sign in to comment.