-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
108 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import json | ||
|
||
from primitive_metadata import primitive_rdf as rdf | ||
|
||
from trove.vocab.jsonapi import ( | ||
JSONAPI_LINK_OBJECT, | ||
JSONAPI_MEMBERNAME, | ||
) | ||
from trove.vocab.namespaces import TROVE, RDF | ||
from ._base import BaseRenderer | ||
|
||
|
||
class TrovesearchSimpleJsonRenderer(BaseRenderer): | ||
'''for "simple json" search api -- very entangled with trove/trovesearch_gathering.py | ||
''' | ||
MEDIATYPE = 'application/json' | ||
|
||
def render_document(self, data: rdf.RdfTripleDictionary, focus_iri: str) -> str: | ||
_focustypes = data[focus_iri][RDF.type] | ||
_graph = rdf.RdfGraph(data) | ||
if TROVE.Cardsearch in _focustypes: | ||
_jsonable = self._render_cardsearch(_graph, focus_iri) | ||
elif TROVE.Valuesearch in _focustypes: | ||
_jsonable = self._render_valuesearch(_graph, focus_iri) | ||
elif TROVE.Indexcard in _focustypes: | ||
_jsonable = self._render_card(_graph, focus_iri) | ||
else: | ||
raise NotImplementedError(f'simplejson not implemented for any of {_focustypes}') | ||
# TODO: links, total in 'meta' | ||
return json.dumps({ | ||
'data': _jsonable, | ||
'links': self._render_links(_graph, focus_iri), | ||
'meta': self._render_meta(_graph, focus_iri), | ||
}, indent=2) | ||
|
||
def _render_cardsearch(self, graph: rdf.RdfGraph, cardsearch_iri: str): | ||
return self._render_searchresultpage(graph, cardsearch_iri) | ||
|
||
def _render_valuesearch(self, graph: rdf.RdfGraph, valuesearch_iri: str): | ||
return self._render_searchresultpage(graph, valuesearch_iri) | ||
|
||
def _render_searchresultpage(self, graph: rdf.RdfGraph, focus_iri: str): | ||
# just each card's contents | ||
_results_sequence = next( | ||
_page | ||
for _page in graph.q(focus_iri, TROVE.searchResultPage) | ||
if rdf.is_container(_page) # filter out page links | ||
) | ||
return [ | ||
self._render_result(graph, _search_result_blanknode) | ||
for _search_result_blanknode in rdf.sequence_objects_in_order(_results_sequence) | ||
] | ||
|
||
def _render_result(self, graph: rdf.RdfGraph, search_result_blanknode: rdf.RdfBlanknode): | ||
_card = next( | ||
_obj | ||
for _pred, _obj in search_result_blanknode | ||
if _pred == TROVE.indexCard | ||
) | ||
return self._render_card(graph, _card) | ||
|
||
def _render_card(self, graph: rdf.RdfGraph, card: str | rdf.RdfBlanknode): | ||
# just the card contents | ||
if isinstance(card, str): | ||
_card_contents = next(graph.q(card, TROVE.resourceMetadata)) | ||
elif isinstance(card, frozenset): | ||
_card_contents = next( | ||
_obj | ||
for _pred, _obj in card | ||
if _pred == TROVE.resourceMetadata | ||
) | ||
else: | ||
raise NotImplementedError | ||
assert isinstance(_card_contents, rdf.Literal) | ||
assert RDF.JSON in _card_contents.datatype_iris | ||
return json.loads(_card_contents.unicode_value) | ||
|
||
def _render_meta(self, graph: rdf.RdfGraph, focus_iri: str): | ||
_meta = {} | ||
try: | ||
_total = next(graph.q(focus_iri, TROVE.totalResultCount)) | ||
if isinstance(_total, int): | ||
_meta['total'] = _total | ||
elif isinstance(_total, rdf.Literal): | ||
_meta['total'] = int(_total.unicode_value) | ||
elif _total == TROVE['ten-thousands-and-more']: | ||
_meta['total'] = 'trove:ten-thousands-and-more' | ||
except StopIteration: | ||
pass | ||
return _meta | ||
|
||
def _render_links(self, graph: rdf.RdfGraph, focus_iri: str): | ||
_links = {} | ||
for _pagelink in graph.q(focus_iri, TROVE.searchResultPage): | ||
_twopledict = rdf.twopledict_from_twopleset(_pagelink) | ||
if JSONAPI_LINK_OBJECT in _twopledict.get(RDF.type, ()): | ||
(_membername,) = _twopledict[JSONAPI_MEMBERNAME] | ||
(_link_url,) = _twopledict[RDF.value] | ||
_links[_membername.unicode_value] = _link_url | ||
return _links |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters