Ejemplo n.º 1
0
 async def stream_fn(r):
     nonlocal data
     writer = csv.writer(LimitedWriter(r, self.ds.config("max_csv_mb")))
     first = True
     next = None
     while first or (next and stream):
         try:
             if next:
                 kwargs["_next"] = next
             if not first:
                 data, _, _ = await self.data(request, database, hash,
                                              **kwargs)
             if first:
                 await writer.writerow(headings)
                 first = False
             next = data.get("next")
             for row in data["rows"]:
                 if not expanded_columns:
                     # Simple path
                     await writer.writerow(row)
                 else:
                     # Look for {"value": "label": } dicts and expand
                     new_row = []
                     for cell in row:
                         if isinstance(cell, dict):
                             new_row.append(cell["value"])
                             new_row.append(cell["label"])
                         else:
                             new_row.append(cell)
                     await writer.writerow(new_row)
         except Exception as e:
             print("caught this", e)
             await r.write(str(e))
             return
Ejemplo n.º 2
0
 async def stream_fn(r):
     nonlocal data
     writer = csv.writer(LimitedWriter(r,
                                       self.ds.setting("max_csv_mb")))
     first = True
     next = None
     while first or (next and stream):
         try:
             if next:
                 kwargs["_next"] = next
             if not first:
                 data, _, _ = await self.data(request, database, hash,
                                              **kwargs)
             if first:
                 await writer.writerow(headings)
                 first = False
             next = data.get("next")
             for row in data["rows"]:
                 if any(isinstance(r, bytes) for r in row):
                     new_row = []
                     for column, cell in zip(headings, row):
                         if isinstance(cell, bytes):
                             # If this is a table page, use .urls.row_blob()
                             if data.get("table"):
                                 pks = data.get("primary_keys") or []
                                 cell = self.ds.absolute_url(
                                     request,
                                     self.ds.urls.row_blob(
                                         database,
                                         data["table"],
                                         path_from_row_pks(
                                             row, pks, not pks),
                                         column,
                                     ),
                                 )
                             else:
                                 # Otherwise generate URL for this query
                                 cell = self.ds.absolute_url(
                                     request,
                                     path_with_format(
                                         request=request,
                                         format="blob",
                                         extra_qs={
                                             "_blob_column":
                                             column,
                                             "_blob_hash":
                                             hashlib.sha256(
                                                 cell).hexdigest(),
                                         },
                                         replace_format="csv",
                                     ),
                                 )
                         new_row.append(cell)
                     row = new_row
                 if not expanded_columns:
                     # Simple path
                     await writer.writerow(row)
                 else:
                     # Look for {"value": "label": } dicts and expand
                     new_row = []
                     for heading, cell in zip(data["columns"], row):
                         if heading in expanded_columns:
                             if cell is None:
                                 new_row.extend(("", ""))
                             else:
                                 assert isinstance(cell, dict)
                                 new_row.append(cell["value"])
                                 new_row.append(cell["label"])
                         else:
                             new_row.append(cell)
                     await writer.writerow(new_row)
         except Exception as e:
             print("caught this", e)
             await r.write(str(e))
             return