web/views/sparql.py
author Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
Thu, 23 Jul 2009 15:28:41 +0200
changeset 2456 aa25d6b244c8
parent 2444 4e61d9e4befb
child 2529 736893a990c4
permissions -rw-r--r--
new cwuri metadata + a few tests fixes on the way
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2422
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     1
"""SPARQL integration
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     2
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     3
:organization: Logilab
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     4
:copyright: 2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2.
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     5
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     6
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     7
"""
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
     8
v__docformat__ = "restructuredtext en"
2424
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
     9
2422
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    10
import rql
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    11
from yams import xy
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    12
2424
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    13
from lxml import etree
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    14
from lxml.builder import E
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    15
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    16
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
    17
from cubicweb.web import Redirect, form, formfields, formwidgets as fwdgs
2422
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    18
from cubicweb.web.views import forms, urlrewrite
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    19
from cubicweb.spa2rql import Sparql2rqlTranslator
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    20
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    21
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    22
class SparqlForm(forms.FieldsForm):
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    23
    id = 'sparql'
2444
4e61d9e4befb [cleanup] fix typo
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2426
diff changeset
    24
    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
    25
    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
    26
                                                (_('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
    27
                                       widget=fwdgs.Radio,
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
    28
                                       initial='table')
2422
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    29
    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
    30
    @property
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    31
    def action(self):
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    32
        return self.req.url()
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    33
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    34
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    35
class SparqlFormView(form.FormViewMixIn, StartupView):
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    36
    id = 'sparql'
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    37
    def call(self):
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    38
        form = self.vreg.select('forms', 'sparql', self.req)
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    39
        self.w(form.form_render())
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    40
        sparql = self.req.form.get('sparql')
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
        vid = self.req.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
    42
        if sparql:
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    43
            try:
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
    44
                qinfo = Sparql2rqlTranslator(self.schema).translate(sparql)
2422
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    45
            except rql.TypeResolverException, ex:
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    46
                self.w(self.req._('can not resolve entity types:') + u' ' + unicode('ex'))
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    47
            except UnsupportedQuery:
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    48
                self.w(self.req._('we are not yet ready to handle this query'))
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    49
            except xy.UnsupportedVocabulary, ex:
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    50
                self.w(self.req._('unknown vocabulary:') + u' ' + unicode('ex'))
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
    51
            if vid == '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
    52
                url = self.build_url('view', rql=qinfo.finalize(), vid=vid)
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
    53
                raise Redirect(url)
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
    54
            rset = self.req.execute(qinfo.finalize())
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
    55
            self.wview(vid, rset, 'null')
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
    56
2424
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    57
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    58
## sparql resultset views #####################################################
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    59
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    60
YAMS_XMLSCHEMA_MAPPING = {
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    61
    'String': 'string',
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    62
    'Int': 'integer',
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    63
    'Float': 'float',
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    64
    'Boolean': 'boolean',
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    65
    'Datetime': 'dateTime',
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    66
    'Date': 'date',
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    67
    'Time': 'time',
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    68
    # 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
    69
    'Decimal': 'string',
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    70
    'Interval': 'duration',
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    71
    'Password': 'string',
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    72
    'Bytes': 'base64Binary',
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    73
    }
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    74
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    75
def xmlschema(yamstype):
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    76
    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
    77
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    78
class SparqlResultXmlView(AnyRsetView):
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    79
    """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
    80
    """
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
    81
    id = 'sparqlxml'
2424
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    82
    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
    83
    templatable = False
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    84
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    85
    def call(self):
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    86
        # XXX handle UNION
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    87
        rqlst = self.rset.syntax_tree().children[0]
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    88
        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
    89
        results = E.results()
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    90
        for rowidx in xrange(len(self.rset)):
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    91
            result = E.result()
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    92
            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
    93
                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
    94
            results.append(result)
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    95
        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
    96
                          results)
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    97
        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
    98
        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
    99
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
   100
    def cell_binding(self, row, col, varname):
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
   101
        celltype = self.rset.description[row][col]
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
   102
        if self.schema.eschema(celltype).is_final():
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
   103
            cellcontent = self.view('cell', self.rset, row=row, col=col)
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
   104
            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
   105
                                       datatype=xmlschema(celltype)),
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
   106
                             name=varname)
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
   107
        else:
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
   108
            entity = self.entity(row, col)
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
   109
            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
   110
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
   111
    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
   112
        """overriden to set the correct filetype and filename"""
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
   113
        self.req.set_content_type(self.content_type,
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
   114
                                  filename='sparql.xml',
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
   115
                                  encoding=self.req.encoding)