author | Julien Cristau <julien.cristau@logilab.fr> |
Tue, 13 Mar 2012 14:20:16 +0100 | |
branch | stable |
changeset 8301 | 1385e05afe52 |
parent 8035 | f98012ec7c53 |
child 10234 | a12e4459eee5 |
permissions | -rw-r--r-- |
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 |
6665fa16aa45
[web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
50 |
content_type = 'text/xml' # +rdf |
6665fa16aa45
[web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
51 |
|
6665fa16aa45
[web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
52 |
def call(self): |
6665fa16aa45
[web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
53 |
graph = rdflib.Graph() |
6665fa16aa45
[web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
54 |
graph.bind('cw', CW) |
6665fa16aa45
[web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
55 |
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
|
56 |
graph.bind(prefix, rdflib.Namespace(xmlns)) |
6665fa16aa45
[web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
57 |
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
|
58 |
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
|
59 |
self.entity2graph(graph, entity) |
6665fa16aa45
[web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
60 |
self.w(graph.serialize().decode('utf-8')) |
6665fa16aa45
[web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
61 |
|
7519
4e531c8697f8
[rdf] add method so one can call entity.view('rdf')
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7016
diff
changeset
|
62 |
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
|
63 |
self.call() |
4e531c8697f8
[rdf] add method so one can call entity.view('rdf')
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7016
diff
changeset
|
64 |
|
7016
6665fa16aa45
[web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
65 |
def entity2graph(self, graph, entity): |
6665fa16aa45
[web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
66 |
cwuri = URIRef(entity.cwuri) |
6665fa16aa45
[web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
67 |
add = graph.add |
6665fa16aa45
[web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
68 |
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
|
69 |
try: |
6665fa16aa45
[web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
70 |
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
|
71 |
add( (cwuri, RDF.type, urijoin(item)) ) |
6665fa16aa45
[web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
72 |
except xy.UnsupportedVocabulary: |
6665fa16aa45
[web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
73 |
pass |
6665fa16aa45
[web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
74 |
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
|
75 |
rtype = rschema.type |
6665fa16aa45
[web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
76 |
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
|
77 |
continue |
6665fa16aa45
[web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
78 |
for eschema in eschemas: |
6665fa16aa45
[web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
79 |
if eschema.final: |
6665fa16aa45
[web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
80 |
try: |
6665fa16aa45
[web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
81 |
value = entity.cw_attr_cache[rtype] |
6665fa16aa45
[web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
82 |
except KeyError: |
6665fa16aa45
[web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
83 |
continue # assuming rtype is Bytes |
6665fa16aa45
[web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
84 |
if value is not None: |
6665fa16aa45
[web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
85 |
add( (cwuri, CW[rtype], Literal(value)) ) |
6665fa16aa45
[web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
86 |
try: |
6665fa16aa45
[web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
87 |
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
|
88 |
add( (cwuri, urijoin(item[1]), Literal(value)) ) |
6665fa16aa45
[web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
89 |
except xy.UnsupportedVocabulary: |
6665fa16aa45
[web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
90 |
pass |
6665fa16aa45
[web/views] add a generic rdf view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
91 |
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
|
92 |
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
|
93 |
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
|
94 |
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
|
95 |
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
|
96 |
for item in xy.xeq('%s %s' % (entity.e_schema.type, rtype)): |
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 |
add( (cwuri, urijoin(item), 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
|
98 |
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
|
99 |
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
|
100 |
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
|
101 |
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
|
102 |