Skip to content
Browse files

Refs #9558 - Handle data annotations by sparql queries

  • Loading branch information...
1 parent ec955cd commit 3785c854ebe0e8267f1952e4e21cade7faa10532 @avoinea avoinea committed
Showing with 74 additions and 8 deletions.
  1. +3 −1 docs/HISTORY.txt
  2. +7 −2 eea/sparql/browser/sparql.py
  3. +64 −5 eea/sparql/converter/sparql2json.py
View
4 docs/HISTORY.txt
@@ -5,6 +5,8 @@ Changelog
----------------------
* Upgrade step: Within "Plone > Site setup > Add-ons" click on
upgrade button available for eea.sparql.
+* Feature: Handling specific annotations for data values
+ [voineali refs #9558]
* Bug fix: Fixed preview for queries returning empty fields
[szabozo0 refs #9608]
* Feature: Added quick preview for sparql queries
@@ -21,7 +23,7 @@ Changelog
2.2 - (2013-01-10)
------------------
-* Change: moved the olderVersions to the portalMessage div that displays the
+* Change: moved the olderVersions to the portalMessage div that displays the
old versions link for eea.sparql templates
[ichimdav #10182]
View
9 eea/sparql/browser/sparql.py
@@ -72,8 +72,12 @@ def json(self, **kwargs):
data = self.context.execute_query()
column_types = kwargs.get('column_types')
- #annotations = kwargs.get('annotations')
- return sortProperties(json.dumps(sparql2json(data, column_types)))
+ annotations = kwargs.get('annotations')
+ return sortProperties(json.dumps(
+ sparql2json(data,
+ column_types=column_types,
+ annotations=annotations)
+ ))
def sparql2exhibit(self):
""" Download sparql results as Exhibit JSON
@@ -381,6 +385,7 @@ def preview(self):
try:
result.append(u"<td> %s </td>" %cgi.escape(value.n3()))
except Exception, err:
+ logger.debug(err)
result.append(u"<td> %s </td>" %value)
result.append(u"</tr>")
result.append(u"</tbody>")
View
69 eea/sparql/converter/sparql2json.py
@@ -39,7 +39,30 @@ def __getitem__(self, n):
return sparql.unpack_row(self.rdfterm_rows[n],
convert_type=sparql_converters)
-def sparql2json(data, column_types=None):
+def text_annotation(text, annotations=()):
+ """ Extract text and annotation from given text based on
+ annotations dict
+
+ >>> text_annotation('4438868(s)', annotations=[
+ ... {'name': u'(s)', 'title': u'Eurostat estimate'},
+ ... {'name': u':', 'title': u'Not available'},
+ ... ])
+ (u'4438868', u'Eurostat estimate')
+
+ """
+ for annotation in annotations:
+ name = annotation.get('name')
+ if not name:
+ continue
+
+ if not isinstance(text, (str, unicode)):
+ text = repr(text)
+
+ if text.find(name) != -1:
+ return text.replace(name, ''), annotation.get('title', name)
+ return text, u''
+
+def sparql2json(data, **kwargs):
"""
Converts sparql to JSON
@@ -75,6 +98,9 @@ def sparql2json(data, column_types=None):
'string': u''}]
"""
+ column_types = kwargs.get('column_types')
+ annotations = kwargs.get('annotations')
+
data_result = data['result']
items = []
hasLabel = False
@@ -83,20 +109,38 @@ def sparql2json(data, column_types=None):
cols = mr.var_names
properties = {}
+ columns = []
for index, col in enumerate(cols):
if col.lower().endswith(":label") or col.lower()=="label":
- cols[index] = 'label'
+ columns.append('label')
hasLabel = True
- break
+ else:
+ columns.append(col)
+
+ if col in annotations:
+ columns.append(annotations[col].get('name'))
for index, row in enumerate(mr):
rowdata = {}
if not hasLabel:
rowdata['label'] = index
- for idx, item in enumerate(row):
- key = cols[idx].encode('utf8')
+ row = iter(row)
+
+ idx = -1
+ for order, key in enumerate(columns):
+ key = key.encode('utf8')
valueType = 'text'
+
+ if key.endswith('__annotations__'):
+ continue
+
+ try:
+ item = row.next()
+ except StopIteration:
+ item = u''
+
+ idx += 1
if isinstance(data_result['rows'][0][idx], sparql.Literal):
datatype = data_result['rows'][0][idx].datatype
if not datatype:
@@ -105,6 +149,13 @@ def sparql2json(data, column_types=None):
elif isinstance(data_result['rows'][0][idx], sparql.IRI):
valueType = 'url'
+ # Annotations
+ anno = annotations.get(key, None)
+ if anno:
+ item, annotation = text_annotation(
+ item, anno.get('annotations', []))
+ rowdata[anno.get('name')] = annotation
+
rowdata[key] = item
# Enforce column_types
@@ -133,6 +184,14 @@ def sparql2json(data, column_types=None):
"order" : idx
}
+ if anno:
+ properties[anno.get('name')] = {
+ "valueType": anno.get('valueType', u'text'),
+ "columnType": anno.get('columnType', u'annotations'),
+ "label": anno.get('label', key + u':annotations'),
+ "order": order + 1
+ }
+
items.append(rowdata)
return {'items': items, 'properties': properties}

0 comments on commit 3785c85

Please sign in to comment.
Something went wrong with that request. Please try again.