web/views/rdf.py
author Julien Cristau <julien.cristau@logilab.fr>
Mon, 09 Nov 2015 15:29:07 +0100
changeset 10871 1d4a94d04ec6
parent 10666 7f6b5f023884
permissions -rw-r--r--
[dataimport] remove replace_sep parameter from massive store It is unused since changeset efbbf1e93a04 "[dataimport] Properly escape strings sent to COPY FROM"
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7016
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
     1
# copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
     2
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
     3
#
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
     4
# This file is part of CubicWeb.
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
     5
#
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
     6
# CubicWeb is free software: you can redistribute it and/or modify it under the
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
     7
# terms of the GNU Lesser General Public License as published by the Free
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
     8
# Software Foundation, either version 2.1 of the License, or (at your option)
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
     9
# any later version.
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    10
#
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    11
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    13
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    14
# details.
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    15
#
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    16
# You should have received a copy of the GNU Lesser General Public License along
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    17
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    18
"""base xml and rss views"""
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    19
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    20
__docformat__ = "restructuredtext en"
10666
7f6b5f023884 [py3k] replace '_ = unicode' in global scope (closes #7589459)
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10609
diff changeset
    21
from cubicweb import _
7016
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    22
10609
e2d8e81bfe68 [py3k] import range using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10317
diff changeset
    23
from six.moves import range
e2d8e81bfe68 [py3k] import range using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10317
diff changeset
    24
7016
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    25
from yams import xy
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    26
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    27
from cubicweb.schema import VIRTUAL_RTYPES
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    28
from cubicweb.view import EntityView
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    29
from cubicweb.web.views.xmlrss import SERIALIZERS
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    30
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    31
try:
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    32
    import rdflib
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    33
except ImportError:
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    34
    rdflib = None
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    35
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    36
if rdflib is not None:
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    37
    RDF = rdflib.Namespace('http://www.w3.org/1999/02/22-rdf-syntax-ns#')
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    38
    CW = rdflib.Namespace('http://ns.cubicweb.org/cubicweb/0.0/')
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    39
    from rdflib import Literal, URIRef, Namespace
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    40
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    41
    def urijoin(item):
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    42
        base, ext = item
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    43
        return URIRef(Namespace(base)[ext])
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    44
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    45
    SKIP_RTYPES = VIRTUAL_RTYPES | set(['cwuri', 'is', 'is_instance_of'])
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    46
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    47
    class RDFView(EntityView):
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    48
        """rdf view for entities"""
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    49
        __regid__ = 'rdf'
8035
f98012ec7c53 [export views] uniformize titles, much nicer when listing possible views. Closes #2060944
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7987
diff changeset
    50
        title = _('rdf export')
7016
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    51
        templatable = False
10317
b81adb5e32f9 [web/views] add text/n3 rdf view
Julien Cristau <julien.cristau@logilab.fr>
parents: 10234
diff changeset
    52
        binary = True
b81adb5e32f9 [web/views] add text/n3 rdf view
Julien Cristau <julien.cristau@logilab.fr>
parents: 10234
diff changeset
    53
        format = 'xml'
7016
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    54
        content_type = 'text/xml' # +rdf
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    55
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    56
        def call(self):
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    57
            graph = rdflib.Graph()
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    58
            graph.bind('cw', CW)
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    59
            for prefix, xmlns in xy.XY.prefixes.items():
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    60
                graph.bind(prefix, rdflib.Namespace(xmlns))
10609
e2d8e81bfe68 [py3k] import range using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10317
diff changeset
    61
            for i in range(self.cw_rset.rowcount):
7016
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    62
                entity = self.cw_rset.complete_entity(i, 0)
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    63
                self.entity2graph(graph, entity)
10317
b81adb5e32f9 [web/views] add text/n3 rdf view
Julien Cristau <julien.cristau@logilab.fr>
parents: 10234
diff changeset
    64
            self.w(graph.serialize(format=self.format))
7016
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    65
7519
4e531c8697f8 [rdf] add method so one can call entity.view('rdf')
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7016
diff changeset
    66
        def entity_call(self, entity):
4e531c8697f8 [rdf] add method so one can call entity.view('rdf')
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7016
diff changeset
    67
            self.call()
4e531c8697f8 [rdf] add method so one can call entity.view('rdf')
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7016
diff changeset
    68
7016
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    69
        def entity2graph(self, graph, entity):
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    70
            cwuri = URIRef(entity.cwuri)
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    71
            add = graph.add
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    72
            add( (cwuri, RDF.type, CW[entity.e_schema.type]) )
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    73
            try:
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    74
                for item in xy.xeq(entity.e_schema.type):
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    75
                    add( (cwuri, RDF.type, urijoin(item)) )
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    76
            except xy.UnsupportedVocabulary:
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    77
                pass
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    78
            for rschema, eschemas, role in entity.e_schema.relation_definitions('relation'):
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    79
                rtype = rschema.type
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    80
                if rtype in SKIP_RTYPES or rtype.endswith('_permission'):
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    81
                    continue
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    82
                for eschema in eschemas:
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    83
                    if eschema.final:
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    84
                        try:
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    85
                            value = entity.cw_attr_cache[rtype]
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    86
                        except KeyError:
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    87
                            continue # assuming rtype is Bytes
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    88
                        if value is not None:
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    89
                            add( (cwuri, CW[rtype], Literal(value)) )
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    90
                            try:
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    91
                                for item in xy.xeq('%s %s' % (entity.e_schema.type, rtype)):
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    92
                                    add( (cwuri, urijoin(item[1]), Literal(value)) )
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    93
                            except xy.UnsupportedVocabulary:
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    94
                                pass
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    95
                    else:
7987
0977d9594d3c [rdf] drop buggy (name error) try/except (done using related(safe=True))
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7911
diff changeset
    96
                        for related in entity.related(rtype, role, entities=True, safe=True):
0977d9594d3c [rdf] drop buggy (name error) try/except (done using related(safe=True))
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7911
diff changeset
    97
                            if role == 'subject':
0977d9594d3c [rdf] drop buggy (name error) try/except (done using related(safe=True))
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7911
diff changeset
    98
                                add( (cwuri, CW[rtype], URIRef(related.cwuri)) )
0977d9594d3c [rdf] drop buggy (name error) try/except (done using related(safe=True))
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7911
diff changeset
    99
                                try:
0977d9594d3c [rdf] drop buggy (name error) try/except (done using related(safe=True))
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7911
diff changeset
   100
                                    for item in xy.xeq('%s %s' % (entity.e_schema.type, rtype)):
10234
a12e4459eee5 [web/views/rdf] Take the second element of XY equivalent for non final relation
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 8035
diff changeset
   101
                                        add( (cwuri, urijoin(item[1]), URIRef(related.cwuri)) )
7987
0977d9594d3c [rdf] drop buggy (name error) try/except (done using related(safe=True))
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7911
diff changeset
   102
                                except xy.UnsupportedVocabulary:
0977d9594d3c [rdf] drop buggy (name error) try/except (done using related(safe=True))
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7911
diff changeset
   103
                                    pass
0977d9594d3c [rdf] drop buggy (name error) try/except (done using related(safe=True))
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7911
diff changeset
   104
                            else:
0977d9594d3c [rdf] drop buggy (name error) try/except (done using related(safe=True))
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7911
diff changeset
   105
                                add( (URIRef(related.cwuri), CW[rtype], cwuri) )
7016
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
   106
10317
b81adb5e32f9 [web/views] add text/n3 rdf view
Julien Cristau <julien.cristau@logilab.fr>
parents: 10234
diff changeset
   107
b81adb5e32f9 [web/views] add text/n3 rdf view
Julien Cristau <julien.cristau@logilab.fr>
parents: 10234
diff changeset
   108
    class RDFN3View(RDFView):
b81adb5e32f9 [web/views] add text/n3 rdf view
Julien Cristau <julien.cristau@logilab.fr>
parents: 10234
diff changeset
   109
        __regid__ = 'n3rdf'
b81adb5e32f9 [web/views] add text/n3 rdf view
Julien Cristau <julien.cristau@logilab.fr>
parents: 10234
diff changeset
   110
        format = 'n3'
b81adb5e32f9 [web/views] add text/n3 rdf view
Julien Cristau <julien.cristau@logilab.fr>
parents: 10234
diff changeset
   111
        content_type = 'text/n3'