web/views/sparql.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Fri, 07 Oct 2011 11:47:42 +0200
branchstable
changeset 7917 436400e7f807
parent 7460 2455cdbeadca
child 8695 358d8bed9626
permissions -rw-r--r--
sample script to turn a pyro source into a datafeed source
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"
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
    21
_ = unicode
2424
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    22
2422
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    23
from yams import xy
5621
6f32f08d91f3 [sparql] improve error reporting and fix one bug
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 5434
diff changeset
    24
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
    25
2424
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    26
from lxml import etree
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    27
from lxml.builder import E
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    28
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    29
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
    30
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
    31
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
    32
try:
4716
55b6a3262071 fix some pylint detected errors
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    33
    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
    34
except ImportError:
736893a990c4 make fyzz/spa2rql a recommends only
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2444
diff changeset
    35
    # fyzz not available (only a recommends)
736893a990c4 make fyzz/spa2rql a recommends only
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2444
diff changeset
    36
    Sparql2rqlTranslator = None
2422
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    37
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    38
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
    39
    __regid__ = 'sparql'
2444
4e61d9e4befb [cleanup] fix typo
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2426
diff changeset
    40
    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
    41
    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
    42
                                                (_('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
    43
                                       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
    44
                                       value='table')
2422
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    45
    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
    46
    @property
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    47
    def action(self):
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
    48
        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
    49
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    50
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    51
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
    52
    __regid__ = 'sparql'
2422
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    53
    def call(self):
5621
6f32f08d91f3 [sparql] improve error reporting and fix one bug
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 5434
diff changeset
    54
        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
    55
        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
    56
        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
    57
        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
    58
        if sparql:
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    59
            try:
4045
f4a52abb6f4f cw 3.6 api update
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 3998
diff changeset
    60
                qinfo = Sparql2rqlTranslator(self._cw.vreg.schema).translate(sparql)
5621
6f32f08d91f3 [sparql] improve error reporting and fix one bug
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 5434
diff changeset
    61
            except TypeResolverException, exc:
6f32f08d91f3 [sparql] improve error reporting and fix one bug
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 5434
diff changeset
    62
                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
    63
            except UnsupportedQuery:
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
    64
                self.w(self._cw._('we are not yet ready to handle this query'))
5621
6f32f08d91f3 [sparql] improve error reporting and fix one bug
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 5434
diff changeset
    65
            except xy.UnsupportedVocabulary, exc:
6f32f08d91f3 [sparql] improve error reporting and fix one bug
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 5434
diff changeset
    66
                self.w(self._cw._('unknown vocabulary:') + u' ' + unicode(exc))
5434
b56420abc00f [sparql] bugfix
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 5424
diff changeset
    67
            else:
5621
6f32f08d91f3 [sparql] improve error reporting and fix one bug
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 5434
diff changeset
    68
                rql, args = qinfo.finalize()
5434
b56420abc00f [sparql] bugfix
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 5424
diff changeset
    69
                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
    70
                    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
    71
                    raise Redirect(url)
5621
6f32f08d91f3 [sparql] improve error reporting and fix one bug
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 5434
diff changeset
    72
                rset = self._cw.execute(rql, args)
5434
b56420abc00f [sparql] bugfix
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 5424
diff changeset
    73
                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
    74
2424
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    75
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    76
## sparql resultset views #####################################################
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
YAMS_XMLSCHEMA_MAPPING = {
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    79
    'String': 'string',
7166
dde161937d3e [time zone] support for TZDatetime and TZTime data type
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6841
diff changeset
    80
dde161937d3e [time zone] support for TZDatetime and TZTime data type
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6841
diff changeset
    81
    'Boolean': 'boolean',
2424
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    82
    'Int': 'integer',
7460
2455cdbeadca [schema] support for BigInt type. Closes #1720995
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7393
diff changeset
    83
    'BigInt': 'integer',
2424
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    84
    'Float': 'float',
7166
dde161937d3e [time zone] support for TZDatetime and TZTime data type
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6841
diff changeset
    85
2424
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    86
    'Datetime': 'dateTime',
7166
dde161937d3e [time zone] support for TZDatetime and TZTime data type
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6841
diff changeset
    87
    'TZDatetime': 'dateTime',
2424
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    88
    'Date': 'date',
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    89
    'Time': 'time',
7166
dde161937d3e [time zone] support for TZDatetime and TZTime data type
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6841
diff changeset
    90
    'TZTime': 'time',
dde161937d3e [time zone] support for TZDatetime and TZTime data type
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6841
diff changeset
    91
2424
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    92
    # 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
    93
    'Decimal': 'string',
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    94
    'Interval': 'duration',
7166
dde161937d3e [time zone] support for TZDatetime and TZTime data type
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6841
diff changeset
    95
    'Bytes': 'base64Binary',
2424
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    96
    'Password': 'string',
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    97
    }
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    98
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    99
def xmlschema(yamstype):
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
   100
    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
   101
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
   102
class SparqlResultXmlView(AnyRsetView):
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
   103
    """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
   104
    """
3377
dd9d292b6a6d use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2789
diff changeset
   105
    __regid__ = 'sparqlxml'
2424
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
   106
    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
   107
    templatable = False
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
   108
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
   109
    def call(self):
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
   110
        # XXX handle UNION
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
   111
        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
   112
        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
   113
        results = E.results()
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
   114
        for rowidx in xrange(len(self.cw_rset)):
2424
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
   115
            result = E.result()
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
   116
            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
   117
                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
   118
            results.append(result)
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
   119
        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
   120
                          results)
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
   121
        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
   122
        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
   123
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
   124
    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
   125
        celltype = self.cw_rset.description[row][col]
4045
f4a52abb6f4f cw 3.6 api update
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 3998
diff changeset
   126
        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
   127
            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
   128
            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
   129
                                       datatype=xmlschema(celltype)),
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
   130
                             name=varname)
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
   131
        else:
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
   132
            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
   133
            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
   134
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
   135
    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
   136
        """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
   137
        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
   138
                                  filename='sparql.xml',
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
   139
                                  encoding=self._cw.encoding)
2529
736893a990c4 make fyzz/spa2rql a recommends only
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2444
diff changeset
   140
736893a990c4 make fyzz/spa2rql a recommends only
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2444
diff changeset
   141
def registration_callback(vreg):
736893a990c4 make fyzz/spa2rql a recommends only
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2444
diff changeset
   142
    if Sparql2rqlTranslator is not None:
736893a990c4 make fyzz/spa2rql a recommends only
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2444
diff changeset
   143
        vreg.register_all(globals().values(), __name__)