|
1 # copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
|
2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
|
3 # |
|
4 # This file is part of CubicWeb. |
|
5 # |
|
6 # CubicWeb is free software: you can redistribute it and/or modify it under the |
|
7 # terms of the GNU Lesser General Public License as published by the Free |
|
8 # Software Foundation, either version 2.1 of the License, or (at your option) |
|
9 # any later version. |
|
10 # |
|
11 # CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
|
12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|
13 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
|
14 # details. |
|
15 # |
|
16 # You should have received a copy of the GNU Lesser General Public License along |
|
17 # with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
|
18 """base xml and rss views""" |
|
19 |
|
20 __docformat__ = "restructuredtext en" |
|
21 _ = unicode |
|
22 |
|
23 from yams import xy |
|
24 |
|
25 from cubicweb.schema import VIRTUAL_RTYPES |
|
26 from cubicweb.view import EntityView |
|
27 from cubicweb.web.views.xmlrss import SERIALIZERS |
|
28 |
|
29 try: |
|
30 import rdflib |
|
31 except ImportError: |
|
32 rdflib = None |
|
33 |
|
34 if rdflib is not None: |
|
35 RDF = rdflib.Namespace('http://www.w3.org/1999/02/22-rdf-syntax-ns#') |
|
36 CW = rdflib.Namespace('http://ns.cubicweb.org/cubicweb/0.0/') |
|
37 from rdflib import Literal, URIRef, Namespace |
|
38 |
|
39 def urijoin(item): |
|
40 base, ext = item |
|
41 return URIRef(Namespace(base)[ext]) |
|
42 |
|
43 SKIP_RTYPES = VIRTUAL_RTYPES | set(['cwuri', 'is', 'is_instance_of']) |
|
44 |
|
45 class RDFView(EntityView): |
|
46 """rdf view for entities""" |
|
47 __regid__ = 'rdf' |
|
48 title = _('rdf') |
|
49 templatable = False |
|
50 content_type = 'text/xml' # +rdf |
|
51 |
|
52 def call(self): |
|
53 graph = rdflib.Graph() |
|
54 graph.bind('cw', CW) |
|
55 for prefix, xmlns in xy.XY.prefixes.items(): |
|
56 graph.bind(prefix, rdflib.Namespace(xmlns)) |
|
57 for i in xrange(self.cw_rset.rowcount): |
|
58 entity = self.cw_rset.complete_entity(i, 0) |
|
59 self.entity2graph(graph, entity) |
|
60 self.w(graph.serialize().decode('utf-8')) |
|
61 |
|
62 def entity2graph(self, graph, entity): |
|
63 cwuri = URIRef(entity.cwuri) |
|
64 add = graph.add |
|
65 add( (cwuri, RDF.type, CW[entity.e_schema.type]) ) |
|
66 try: |
|
67 for item in xy.xeq(entity.e_schema.type): |
|
68 add( (cwuri, RDF.type, urijoin(item)) ) |
|
69 except xy.UnsupportedVocabulary: |
|
70 pass |
|
71 for rschema, eschemas, role in entity.e_schema.relation_definitions('relation'): |
|
72 rtype = rschema.type |
|
73 if rtype in SKIP_RTYPES or rtype.endswith('_permission'): |
|
74 continue |
|
75 for eschema in eschemas: |
|
76 if eschema.final: |
|
77 try: |
|
78 value = entity.cw_attr_cache[rtype] |
|
79 except KeyError: |
|
80 continue # assuming rtype is Bytes |
|
81 if value is not None: |
|
82 add( (cwuri, CW[rtype], Literal(value)) ) |
|
83 try: |
|
84 for item in xy.xeq('%s %s' % (entity.e_schema.type, rtype)): |
|
85 add( (cwuri, urijoin(item[1]), Literal(value)) ) |
|
86 except xy.UnsupportedVocabulary: |
|
87 pass |
|
88 else: |
|
89 for related in entity.related(rtype, role, entities=True): |
|
90 if role == 'subject': |
|
91 add( (cwuri, CW[rtype], URIRef(related.cwuri)) ) |
|
92 try: |
|
93 for item in xy.xeq('%s %s' % (entity.e_schema.type, rtype)): |
|
94 add( (cwuri, urijoin(item), URIRef(related.cwuri)) ) |
|
95 except xy.UnsupportedVocabulary: |
|
96 pass |
|
97 else: |
|
98 add( (URIRef(related.cwuri), CW[rtype], cwuri) ) |
|
99 |
|
100 |