Commit 0f89107b authored by Sylvain Thénault's avatar Sylvain Thénault
Browse files

add SKOS RDF/XML views for schemes

parent 35d3883a9242
......@@ -415,6 +415,17 @@ class RDFLibRDFGraph(AbstractRDFGraph):
yield unicode(subj)
def default_graph():
"""Return a default graph instance depending on installed software."""
try:
return RDFLibRDFGraph()
except ImportError:
try:
return LibRDFRDFGraph()
except ImportError:
raise ValueError('neither rdflib nor RDF library installed')
def rdf_graph_to_entities(reg, graph, etypes, output_cls=ExtEntity):
"""Turns RDF data into an transitional ExtEntity representation that may
be then imported into some database easily.
......
......@@ -15,11 +15,15 @@
# You should have received a copy of the GNU Lesser General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
import os
from io import BytesIO
from tempfile import NamedTemporaryFile
from cubicweb.web import Redirect
from cubicweb.devtools.testlib import CubicWebTC
from cubes.skos.rdfio import default_graph
class ViewsTC(CubicWebTC):
......@@ -81,6 +85,26 @@ class ViewsTC(CubicWebTC):
definition="Création volontaire ou en application de la loi").one()
self.assertEqual(concept2.broader_concept[0], concept1)
def _test_skos_view(self, vid, nb_expected_triples):
with self.admin_access.client_cnx() as cnx:
scheme = cnx.find('ConceptScheme', title=u'musique').one()
scheme.add_concept(u'pop')
with NamedTemporaryFile(delete=False) as fobj:
try:
fobj.write(scheme.view(vid))
fobj.close()
graph = default_graph()
graph.load('file://'+fobj.name, rdf_format='xml')
finally:
os.unlink(fobj.name)
triples = set(graph.triples())
self.assertEqual(len(triples), nb_expected_triples, triples)
def test_skos_primary_view(self):
self._test_skos_view('primary.rdf', 5)
def test_skos_list_view(self):
self._test_skos_view('list.rdf', 2)
if __name__ == '__main__':
......
......@@ -15,6 +15,7 @@
# with this program. If not, see <http://www.gnu.org/licenses/>.
"""cubicweb-skos views"""
from io import BytesIO
from copy import copy
from rql.utils import uquote
......@@ -34,6 +35,8 @@ from cubicweb.web.views import (autoform, editcontroller, uicfg, ibreadcrumbs,
from cubes.skos import to_unicode, lcsv
from cubes.skos.entities import CSVIndentationError, CSVDecodeError
from cubes.skos.rdfio import default_graph
_ = unicode
......@@ -535,3 +538,41 @@ class SourceSynchronizationService(Service):
source = repo.sources_by_uri[source_entity.name]
assert repo.config.source_enabled(source)
source.pull_data(cnx)
# SKOS RDF export ##################################################################################
class SKOSPrimaryView(EntityView):
"""RDF primary view for ConceptScheme: output the full concept tree."""
__regid__ = 'primary.rdf'
__select__ = is_instance('ConceptScheme')
templatable = False
content_type = 'text/xml'
adapter = 'RDFPrimary'
def call(self):
graph = default_graph()
for entity in self.cw_rset.entities():
self.entity_call(entity, graph)
self._dump(graph)
def entity_call(self, entity, graph=None):
dump = graph is None
if graph is None:
graph = default_graph()
rdfgenerator = entity.cw_adapt_to(self.adapter)
rdfgenerator.fill(graph)
if dump:
self._dump(graph)
def _dump(self, graph):
fobj = BytesIO()
graph.dump(fobj, rdf_format='xml')
self.w(unicode(fobj.getvalue(), 'utf-8'))
class SKOSListView(SKOSPrimaryView):
"""RDF list view for ConceptScheme: only output the scheme's type and title."""
__regid__ = 'list.rdf'
__select__ = is_instance('ConceptScheme')
adapter = 'RDFList'
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment