[dataio] Add a ``rqlquery()`` function
authorSimon Chabot <simon.chabot@logilab.fr>
Thu, 29 Nov 2012 14:09:00 +0100
changeset 172 0c1c26ddc444
parent 171 87249081b27d
child 173 4e2afdca3f0c
[dataio] Add a ``rqlquery()`` function Execute a rql query on a given cubicweb host, and parse and format the results to the data structure used by nazca alignement process. The cubicweb host is expected to return a csv file (thanks to the csvexport view) *** [dataio] ``rqlquery()`` handles indexes formatting. The output stream get by urllib in ``rqlquery`` is forwarded to the ``parsefile`` function, which already support indexes formatting.
dataio.py
--- a/dataio.py	Thu Nov 29 14:06:18 2012 +0100
+++ b/dataio.py	Thu Nov 29 14:09:00 2012 +0100
@@ -18,6 +18,7 @@
 from os.path import exists as fileexists
 
 import csv
+import urllib
 
 try:
     from SPARQLWrapper import SPARQLWrapper, JSON
@@ -41,6 +42,20 @@
                 return data.decode(encoding)
             return data
 
+def rqlquery(host, rql, indexes=None):
+    """ Run the rql query on the given cubicweb host
+    """
+
+    if host.endswith('/'):
+        host = host[:-1]
+
+    indexes = indexes or []
+    filehandle = urllib.urlopen('%(host)s/view?'
+                                'rql=%(rql)s&vid=csvexport'
+                                % {'rql': rql, 'host': host})
+    filehandle.readline()#Skip the first line
+    return parsefile(filehandle, delimiter=';', indexes=indexes);
+
 def sparqlquery(endpoint, query, indexes=None):
     """ Run the sparql query on the given endpoint, and wrap the items in the
     indexes form. If indexes is empty, keep raw output"""