[sparql] handle empty result of _sparqlexecute (closes #235720)

authorSamuel Trégouët <samuel.tregouet@logilab.fr>
changesetaed668ce06ea
branchdefault
phasepublic
hiddenno
parent revision#5b87b09238f4 [normalize] Correctly check for 0 or None in normalization indexes, closes #241166
child revision#4aedb4dd72e0 preparing 0.5.1
files modified by this revision
utils/dataio.py
# HG changeset patch
# User Samuel Trégouët <samuel.tregouet@logilab.fr>
# Date 1397135133 -7200
# Thu Apr 10 15:05:33 2014 +0200
# Node ID aed668ce06ea0abd4ee2015a5d7aa4b40c79394b
# Parent 5b87b09238f4e5457f684b599b38b8fe38ae916b
[sparql] handle empty result of _sparqlexecute (closes #235720)

diff --git a/utils/dataio.py b/utils/dataio.py
@@ -121,13 +121,15 @@
1              return []
2 
3  def sparqlquery(endpoint, query, indexes=None, autocaste_data=True, raise_on_error=False):
4      """ Run the sparql query on the given endpoint, and wrap the items in the
5      indexes form. If indexes is empty, keep raw output"""
6 +    results = []
7      rawresults = _sparqlexecute(endpoint, query, raise_on_error)
8 +    if not rawresults:
9 +        return results
10      labels = rawresults['head']['vars']
11 -    results = []
12      indexes = indexes or []
13      if autocaste_data:
14          transform = autocast
15      else:
16          def transform(*args): return args
@@ -146,14 +148,16 @@
17 
18  def sparqljson(endpoint, query, lang_order=('fr', 'en'), raise_on_error=False):
19      """ Execute and format the results of a sparql query.
20      Sort the litterals using lang_order.
21      """
22 +    data = {}
23      rawresults = _sparqlexecute(endpoint, query, raise_on_error)
24 +    if not rawresults:
25 +        return data
26      results = rawresults["results"]["bindings"]
27      data_lang = {}
28 -    data = {}
29      for row in results:
30          for k, v in row.iteritems():
31              if v['type'] == 'uri':
32                  # Uri, keep it in a set
33                  data.setdefault(k, set()).add(v['value'])