Commits

Stefan Urbanek committed 036b6f9

handle max-results option to limit number of rows; give reason (exception message) why resource is not supported (possible mistype/fail); handle data transformation error and return

Comments (0)

Files changed (3)

example/dataproxy/app.py

 
         try:
             trans_module = transform.type_transformation_module(resource_type)
-        except:
+        except Exception as e:
             title = 'Resource type not supported'
-            msg = 'Transformation of resource of type %s is not supported' % resource_type
+            msg = 'Transformation of resource of type %s is not supported. Reason: %s' \
+                            % (resource_type, e)
             flow.http_response.status = '200 Error %s' % title 
             flow.http_response.body = error(title=title, msg=msg)
             return
             flow.http_response.body = error(title=title, msg=msg)
             return
             
-        result = trans_module.transform(flow, url, query)
+        try:
+            result = trans_module.transform(flow, url, query)
+        except Exception as e:
+            title = "Data Transformation Error"
+            msg = "Data transformation failed. Reason: %s" % e
+            # print "ERROR: %s" % e
+            flow.http_response.status = '200 %s' % title
+            flow.http_response.body = error(title=title, msg=msg)
+            return
+            
 
         indent=None
 

example/dataproxy/transform/csv_transform.py

     handle = urllib2.urlopen(url)
     reader = csv.reader(handle)
 
+    try:
+        max_results = int(query.getfirst("max-results"))
+    except:
+        raise ValueError("max-results should be an integer")
+
     rows = []
+    result_count = 0
     for row in reader:
         rows.append(row)
+        result_count += 1
+        if max_results and result_count >= max_results:
+            break
 
     handle.close
 
                 },
                 "response": rows
               }
+    if max_results:
+        result["max_results"] = max_results
     
     return result
 

example/dataproxy/transform/xls_transform.py

     resource_content = handle.read()
     handle.close()
 
+    try:
+        max_results = int(query.getfirst("max-results"))
+    except:
+        raise ValueError("max-results should be an integer")
+
     sheet_name = ''
     if flow.query.has_key('sheet'):
         sheet_number = int(flow.query.getfirst('sheet'))
         names.append(sheet_name)
     rows = []
     sheet = book.sheet_by_name(names[sheet_number])
+
+    # Get the rows
+    result_count = 0
     for rownum in range(sheet.nrows):
         vals = sheet.row_values(rownum)
         rows.append(vals)
+        result_count += 1
+        if max_results and result_count >= max_results:
+            break
 
     result = {
                 "header": {
                 },
                 "response": rows
               }
+    if max_results:
+        result["max_results"] = max_results
     
     return result