web/views/sparql.py
author Rémi Cardona <remi.cardona@logilab.fr>, Julien Cristau <julien.cristau@logilab.fr>
Thu, 26 Nov 2015 11:30:54 +0100
changeset 10935 049209b9e9d6
parent 10666 7f6b5f023884
permissions -rw-r--r--
[qunit] stop dealing with filesystem paths qunit tests need a few things served by cubicweb: - qunit itself, which is now handled by CWDevtoolsStaticController (serving files in cubicweb/devtools/data) - standard cubicweb or cubes data files, handled by the DataController - the tests themselves and their dependencies. These can live in <apphome>/data or <apphome>/static and be served by one of the STATIC_CONTROLLERS This avoids having to guess in CWSoftwareRootStaticController where to serve things from (some files may be installed, others are in the source tree), and should hopefully make it possible to have these tests pass when using tox, and to write qunit tests for cubes, outside of cubicweb itself. This requires modifying the tests to only declare URL paths instead of filesystem paths, and moving support files below test/data/static.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7166
dde161937d3e [time zone] support for TZDatetime and TZTime data type
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6841
diff changeset
     1
# copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4721
diff changeset
     2
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4721
diff changeset
     3
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4721
diff changeset
     4
# This file is part of CubicWeb.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4721
diff changeset
     5
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4721
diff changeset
     6
# CubicWeb is free software: you can redistribute it and/or modify it under the
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4721
diff changeset
     7
# terms of the GNU Lesser General Public License as published by the Free
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4721
diff changeset
     8
# Software Foundation, either version 2.1 of the License, or (at your option)
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4721
diff changeset
     9
# any later version.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4721
diff changeset
    10
#
5424
8ecbcbff9777 replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5421
diff changeset
    11
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4721
diff changeset
    12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4721
diff changeset
    13
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4721
diff changeset
    14
# details.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4721
diff changeset
    15
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4721
diff changeset
    16
# You should have received a copy of the GNU Lesser General Public License along
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4721
diff changeset
    17
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
6582
8eb7883b4223 [pylint] fix a bug of pylint detected errors and i18n pb (calling builtins._ instead of req._)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5621
diff changeset
    18
"""SPARQL integration"""
2422
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    19
4716
55b6a3262071 fix some pylint detected errors
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    20
__docformat__ = "restructuredtext en"
10666
7f6b5f023884 [py3k] replace '_ = unicode' in global scope (closes #7589459)
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10663
diff changeset
    21
from cubicweb import _
2424
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    22
10609
e2d8e81bfe68 [py3k] import range using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents: 8696
diff changeset
    23
from six.moves import range
e2d8e81bfe68 [py3k] import range using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents: 8696
diff changeset
    24
2422
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    25
from yams import xy
5621
6f32f08d91f3 [sparql] improve error reporting and fix one bug
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 5434
diff changeset
    26
from rql import TypeResolverException
2422
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    27
2424
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    28
from lxml import etree
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    29
from lxml.builder import E
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    30
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    31
from cubicweb.view import StartupView, AnyRsetView
2426
0209d9bc45a8 possibility to choose results format, set a nicer file name on sparqlxml view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2424
diff changeset
    32
from cubicweb.web import Redirect, form, formfields, formwidgets as fwdgs
4719
aaed3f813ef8 kill dead/useless code as suggested by pylint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4716
diff changeset
    33
from cubicweb.web.views import forms
2529
736893a990c4 make fyzz/spa2rql a recommends only
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2444
diff changeset
    34
try:
4716
55b6a3262071 fix some pylint detected errors
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    35
    from cubicweb.spa2rql import Sparql2rqlTranslator, UnsupportedQuery
2529
736893a990c4 make fyzz/spa2rql a recommends only
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2444
diff changeset
    36
except ImportError:
736893a990c4 make fyzz/spa2rql a recommends only
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2444
diff changeset
    37
    # fyzz not available (only a recommends)
736893a990c4 make fyzz/spa2rql a recommends only
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2444
diff changeset
    38
    Sparql2rqlTranslator = None
2422
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    39
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    40
class SparqlForm(forms.FieldsForm):
3377
dd9d292b6a6d use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2789
diff changeset
    41
    __regid__ = 'sparql'
2444
4e61d9e4befb [cleanup] fix typo
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2426
diff changeset
    42
    sparql = formfields.StringField(help=_('type here a sparql query'))
2426
0209d9bc45a8 possibility to choose results format, set a nicer file name on sparqlxml view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2424
diff changeset
    43
    resultvid = formfields.StringField(choices=((_('table'), 'table'),
0209d9bc45a8 possibility to choose results format, set a nicer file name on sparqlxml view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2424
diff changeset
    44
                                                (_('sparql xml'), 'sparqlxml')),
0209d9bc45a8 possibility to choose results format, set a nicer file name on sparqlxml view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2424
diff changeset
    45
                                       widget=fwdgs.Radio,
4159
6b2b20c73d59 refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4045
diff changeset
    46
                                       value='table')
2422
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    47
    form_buttons = [fwdgs.SubmitButton()]
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    48
    @property
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    49
    def action(self):
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
    50
        return self._cw.url()
2422
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    51
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    52
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    53
class SparqlFormView(form.FormViewMixIn, StartupView):
3377
dd9d292b6a6d use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2789
diff changeset
    54
    __regid__ = 'sparql'
2422
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    55
    def call(self):
5621
6f32f08d91f3 [sparql] improve error reporting and fix one bug
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 5434
diff changeset
    56
        form = self._cw.vreg['forms'].select('sparql', self._cw)
6841
f04df13fc851 [sparql] fix an url generation bug breaking sparqlxml results view + remove a deprecation warning
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6582
diff changeset
    57
        form.render(w=self.w)
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
    58
        sparql = self._cw.form.get('sparql')
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
    59
        vid = self._cw.form.get('resultvid', 'table')
2422
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    60
        if sparql:
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    61
            try:
4045
f4a52abb6f4f cw 3.6 api update
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 3998
diff changeset
    62
                qinfo = Sparql2rqlTranslator(self._cw.vreg.schema).translate(sparql)
8695
358d8bed9626 [toward-py3k] rewrite to "except AnException as exc:" (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 7460
diff changeset
    63
            except TypeResolverException as exc:
5621
6f32f08d91f3 [sparql] improve error reporting and fix one bug
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 5434
diff changeset
    64
                self.w(self._cw._('can not resolve entity types:') + u' ' + unicode(exc))
2422
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    65
            except UnsupportedQuery:
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
    66
                self.w(self._cw._('we are not yet ready to handle this query'))
8695
358d8bed9626 [toward-py3k] rewrite to "except AnException as exc:" (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 7460
diff changeset
    67
            except xy.UnsupportedVocabulary as exc:
5621
6f32f08d91f3 [sparql] improve error reporting and fix one bug
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 5434
diff changeset
    68
                self.w(self._cw._('unknown vocabulary:') + u' ' + unicode(exc))
5434
b56420abc00f [sparql] bugfix
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 5424
diff changeset
    69
            else:
5621
6f32f08d91f3 [sparql] improve error reporting and fix one bug
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 5434
diff changeset
    70
                rql, args = qinfo.finalize()
5434
b56420abc00f [sparql] bugfix
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 5424
diff changeset
    71
                if vid == 'sparqlxml':
6841
f04df13fc851 [sparql] fix an url generation bug breaking sparqlxml results view + remove a deprecation warning
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6582
diff changeset
    72
                    url = self._cw.build_url('view', rql=rql % args, vid=vid)
5434
b56420abc00f [sparql] bugfix
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 5424
diff changeset
    73
                    raise Redirect(url)
5621
6f32f08d91f3 [sparql] improve error reporting and fix one bug
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 5434
diff changeset
    74
                rset = self._cw.execute(rql, args)
5434
b56420abc00f [sparql] bugfix
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 5424
diff changeset
    75
                self.wview(vid, rset, 'null')
2426
0209d9bc45a8 possibility to choose results format, set a nicer file name on sparqlxml view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2424
diff changeset
    76
2424
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    77
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    78
## sparql resultset views #####################################################
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    79
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    80
YAMS_XMLSCHEMA_MAPPING = {
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    81
    'String': 'string',
7166
dde161937d3e [time zone] support for TZDatetime and TZTime data type
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6841
diff changeset
    82
dde161937d3e [time zone] support for TZDatetime and TZTime data type
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6841
diff changeset
    83
    'Boolean': 'boolean',
2424
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    84
    'Int': 'integer',
7460
2455cdbeadca [schema] support for BigInt type. Closes #1720995
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7393
diff changeset
    85
    'BigInt': 'integer',
2424
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    86
    'Float': 'float',
7166
dde161937d3e [time zone] support for TZDatetime and TZTime data type
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6841
diff changeset
    87
2424
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    88
    'Datetime': 'dateTime',
7166
dde161937d3e [time zone] support for TZDatetime and TZTime data type
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6841
diff changeset
    89
    'TZDatetime': 'dateTime',
2424
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    90
    'Date': 'date',
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    91
    'Time': 'time',
7166
dde161937d3e [time zone] support for TZDatetime and TZTime data type
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6841
diff changeset
    92
    'TZTime': 'time',
dde161937d3e [time zone] support for TZDatetime and TZTime data type
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6841
diff changeset
    93
2424
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    94
    # XXX the following types don't have direct mapping
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    95
    'Decimal': 'string',
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    96
    'Interval': 'duration',
7166
dde161937d3e [time zone] support for TZDatetime and TZTime data type
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6841
diff changeset
    97
    'Bytes': 'base64Binary',
2424
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    98
    'Password': 'string',
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    99
    }
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
   100
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
   101
def xmlschema(yamstype):
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
   102
    return 'http://www.w3.org/2001/XMLSchema#%s' % YAMS_XMLSCHEMA_MAPPING[yamstype]
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
   103
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
   104
class SparqlResultXmlView(AnyRsetView):
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
   105
    """The spec can be found here: http://www.w3.org/TR/rdf-sparql-XMLres/
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
   106
    """
3377
dd9d292b6a6d use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2789
diff changeset
   107
    __regid__ = 'sparqlxml'
2424
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
   108
    content_type = 'application/sparql-results+xml'
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
   109
    templatable = False
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
   110
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
   111
    def call(self):
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
   112
        # XXX handle UNION
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
   113
        rqlst = self.cw_rset.syntax_tree().children[0]
2424
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
   114
        varnames = [var.name for var in rqlst.selection]
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
   115
        results = E.results()
10609
e2d8e81bfe68 [py3k] import range using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents: 8696
diff changeset
   116
        for rowidx in range(len(self.cw_rset)):
2424
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
   117
            result = E.result()
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
   118
            for colidx, varname in enumerate(varnames):
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
   119
                result.append(self.cell_binding(rowidx, colidx, varname))
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
   120
            results.append(result)
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
   121
        sparql = E.sparql(E.head(*(E.variable(name=name) for name in varnames)),
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
   122
                          results)
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
   123
        self.w(u'<?xml version="1.0"?>\n')
2426
0209d9bc45a8 possibility to choose results format, set a nicer file name on sparqlxml view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2424
diff changeset
   124
        self.w(etree.tostring(sparql, encoding=unicode, pretty_print=True))
2424
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
   125
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
   126
    def cell_binding(self, row, col, varname):
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
   127
        celltype = self.cw_rset.description[row][col]
4045
f4a52abb6f4f cw 3.6 api update
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 3998
diff changeset
   128
        if self._cw.vreg.schema.eschema(celltype).final:
7393
7824d61f41a2 fix some deprecation warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7166
diff changeset
   129
            cellcontent = self._cw.view('cell', self.cw_rset, row=row, col=col)
2424
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
   130
            return E.binding(E.literal(cellcontent,
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
   131
                                       datatype=xmlschema(celltype)),
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
   132
                             name=varname)
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
   133
        else:
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
   134
            entity = self.cw_rset.get_entity(row, col)
2424
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
   135
            return E.binding(E.uri(entity.absolute_url()), name=varname)
2426
0209d9bc45a8 possibility to choose results format, set a nicer file name on sparqlxml view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2424
diff changeset
   136
0209d9bc45a8 possibility to choose results format, set a nicer file name on sparqlxml view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2424
diff changeset
   137
    def set_request_content_type(self):
0209d9bc45a8 possibility to choose results format, set a nicer file name on sparqlxml view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2424
diff changeset
   138
        """overriden to set the correct filetype and filename"""
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
   139
        self._cw.set_content_type(self.content_type,
2426
0209d9bc45a8 possibility to choose results format, set a nicer file name on sparqlxml view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2424
diff changeset
   140
                                  filename='sparql.xml',
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
   141
                                  encoding=self._cw.encoding)
2529
736893a990c4 make fyzz/spa2rql a recommends only
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2444
diff changeset
   142
736893a990c4 make fyzz/spa2rql a recommends only
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2444
diff changeset
   143
def registration_callback(vreg):
736893a990c4 make fyzz/spa2rql a recommends only
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2444
diff changeset
   144
    if Sparql2rqlTranslator is not None:
10663
54b8a1f249fb [py3k] dict.itervalues → dict.values
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10609
diff changeset
   145
        vreg.register_all(globals().values(), __name__)