web/views/rdf.py
author Rémi Cardona <remi.cardona@logilab.fr>
Tue, 15 Dec 2015 11:41:25 +0100
changeset 10978 6f88cb7b7a84
parent 10317 b81adb5e32f9
child 10609 e2d8e81bfe68
permissions -rw-r--r--
merge with 3.20.12
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"
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    21
_ = unicode
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    22
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    23
from yams import xy
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    24
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    25
from cubicweb.schema import VIRTUAL_RTYPES
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    26
from cubicweb.view import EntityView
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    27
from cubicweb.web.views.xmlrss import SERIALIZERS
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    28
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    29
try:
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    30
    import rdflib
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    31
except ImportError:
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    32
    rdflib = None
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    33
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    34
if rdflib is not None:
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    35
    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
    36
    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
    37
    from rdflib import Literal, URIRef, Namespace
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    38
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    39
    def urijoin(item):
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    40
        base, ext = item
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    41
        return URIRef(Namespace(base)[ext])
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    42
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    43
    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
    44
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    45
    class RDFView(EntityView):
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    46
        """rdf view for entities"""
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    47
        __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
    48
        title = _('rdf export')
7016
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    49
        templatable = False
10317
b81adb5e32f9 [web/views] add text/n3 rdf view
Julien Cristau <julien.cristau@logilab.fr>
parents: 10234
diff changeset
    50
        binary = True
b81adb5e32f9 [web/views] add text/n3 rdf view
Julien Cristau <julien.cristau@logilab.fr>
parents: 10234
diff changeset
    51
        format = 'xml'
7016
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    52
        content_type = 'text/xml' # +rdf
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    53
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    54
        def call(self):
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    55
            graph = rdflib.Graph()
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    56
            graph.bind('cw', CW)
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    57
            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
    58
                graph.bind(prefix, rdflib.Namespace(xmlns))
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    59
            for i in xrange(self.cw_rset.rowcount):
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    60
                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
    61
                self.entity2graph(graph, entity)
10317
b81adb5e32f9 [web/views] add text/n3 rdf view
Julien Cristau <julien.cristau@logilab.fr>
parents: 10234
diff changeset
    62
            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
    63
7519
4e531c8697f8 [rdf] add method so one can call entity.view('rdf')
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7016
diff changeset
    64
        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
    65
            self.call()
4e531c8697f8 [rdf] add method so one can call entity.view('rdf')
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7016
diff changeset
    66
7016
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    67
        def entity2graph(self, graph, entity):
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    68
            cwuri = URIRef(entity.cwuri)
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    69
            add = graph.add
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    70
            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
    71
            try:
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    72
                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
    73
                    add( (cwuri, RDF.type, urijoin(item)) )
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    74
            except xy.UnsupportedVocabulary:
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    75
                pass
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    76
            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
    77
                rtype = rschema.type
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    78
                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
    79
                    continue
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    80
                for eschema in eschemas:
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    81
                    if eschema.final:
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    82
                        try:
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    83
                            value = entity.cw_attr_cache[rtype]
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    84
                        except KeyError:
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    85
                            continue # assuming rtype is Bytes
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    86
                        if value is not None:
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    87
                            add( (cwuri, CW[rtype], Literal(value)) )
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    88
                            try:
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    89
                                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
    90
                                    add( (cwuri, urijoin(item[1]), Literal(value)) )
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    91
                            except xy.UnsupportedVocabulary:
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    92
                                pass
6665fa16aa45 [web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    93
                    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
    94
                        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
    95
                            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
    96
                                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
    97
                                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
    98
                                    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
    99
                                        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
   100
                                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
   101
                                    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
   102
                            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
   103
                                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
   104
10317
b81adb5e32f9 [web/views] add text/n3 rdf view
Julien Cristau <julien.cristau@logilab.fr>
parents: 10234
diff changeset
   105
b81adb5e32f9 [web/views] add text/n3 rdf view
Julien Cristau <julien.cristau@logilab.fr>
parents: 10234
diff changeset
   106
    class RDFN3View(RDFView):
b81adb5e32f9 [web/views] add text/n3 rdf view
Julien Cristau <julien.cristau@logilab.fr>
parents: 10234
diff changeset
   107
        __regid__ = 'n3rdf'
b81adb5e32f9 [web/views] add text/n3 rdf view
Julien Cristau <julien.cristau@logilab.fr>
parents: 10234
diff changeset
   108
        format = 'n3'
b81adb5e32f9 [web/views] add text/n3 rdf view
Julien Cristau <julien.cristau@logilab.fr>
parents: 10234
diff changeset
   109
        content_type = 'text/n3'