|
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 from cubicweb import _ |
|
22 |
|
23 from six.moves import range |
|
24 |
|
25 from yams import xy |
|
26 |
|
27 from cubicweb.schema import VIRTUAL_RTYPES |
|
28 from cubicweb.view import EntityView |
|
29 from cubicweb.web.views.xmlrss import SERIALIZERS |
|
30 |
|
31 try: |
|
32 import rdflib |
|
33 except ImportError: |
|
34 rdflib = None |
|
35 |
|
36 if rdflib is not None: |
|
37 RDF = rdflib.Namespace('http://www.w3.org/1999/02/22-rdf-syntax-ns#') |
|
38 CW = rdflib.Namespace('http://ns.cubicweb.org/cubicweb/0.0/') |
|
39 from rdflib import Literal, URIRef, Namespace |
|
40 |
|
41 def urijoin(item): |
|
42 base, ext = item |
|
43 return URIRef(Namespace(base)[ext]) |
|
44 |
|
45 SKIP_RTYPES = VIRTUAL_RTYPES | set(['cwuri', 'is', 'is_instance_of']) |
|
46 |
|
47 class RDFView(EntityView): |
|
48 """rdf view for entities""" |
|
49 __regid__ = 'rdf' |
|
50 title = _('rdf export') |
|
51 templatable = False |
|
52 binary = True |
|
53 format = 'xml' |
|
54 content_type = 'text/xml' # +rdf |
|
55 |
|
56 def call(self): |
|
57 graph = rdflib.Graph() |
|
58 graph.bind('cw', CW) |
|
59 for prefix, xmlns in xy.XY.prefixes.items(): |
|
60 graph.bind(prefix, rdflib.Namespace(xmlns)) |
|
61 for i in range(self.cw_rset.rowcount): |
|
62 entity = self.cw_rset.complete_entity(i, 0) |
|
63 self.entity2graph(graph, entity) |
|
64 self.w(graph.serialize(format=self.format)) |
|
65 |
|
66 def entity_call(self, entity): |
|
67 self.call() |
|
68 |
|
69 def entity2graph(self, graph, entity): |
|
70 cwuri = URIRef(entity.cwuri) |
|
71 add = graph.add |
|
72 add( (cwuri, RDF.type, CW[entity.e_schema.type]) ) |
|
73 try: |
|
74 for item in xy.xeq(entity.e_schema.type): |
|
75 add( (cwuri, RDF.type, urijoin(item)) ) |
|
76 except xy.UnsupportedVocabulary: |
|
77 pass |
|
78 for rschema, eschemas, role in entity.e_schema.relation_definitions('relation'): |
|
79 rtype = rschema.type |
|
80 if rtype in SKIP_RTYPES or rtype.endswith('_permission'): |
|
81 continue |
|
82 for eschema in eschemas: |
|
83 if eschema.final: |
|
84 try: |
|
85 value = entity.cw_attr_cache[rtype] |
|
86 except KeyError: |
|
87 continue # assuming rtype is Bytes |
|
88 if value is not None: |
|
89 add( (cwuri, CW[rtype], Literal(value)) ) |
|
90 try: |
|
91 for item in xy.xeq('%s %s' % (entity.e_schema.type, rtype)): |
|
92 add( (cwuri, urijoin(item[1]), Literal(value)) ) |
|
93 except xy.UnsupportedVocabulary: |
|
94 pass |
|
95 else: |
|
96 for related in entity.related(rtype, role, entities=True, safe=True): |
|
97 if role == 'subject': |
|
98 add( (cwuri, CW[rtype], URIRef(related.cwuri)) ) |
|
99 try: |
|
100 for item in xy.xeq('%s %s' % (entity.e_schema.type, rtype)): |
|
101 add( (cwuri, urijoin(item[1]), URIRef(related.cwuri)) ) |
|
102 except xy.UnsupportedVocabulary: |
|
103 pass |
|
104 else: |
|
105 add( (URIRef(related.cwuri), CW[rtype], cwuri) ) |
|
106 |
|
107 |
|
108 class RDFN3View(RDFView): |
|
109 __regid__ = 'n3rdf' |
|
110 format = 'n3' |
|
111 content_type = 'text/n3' |