Coverage for portality/lib/report_to_csv.py: 100%

16 statements  

« prev     ^ index     » next       coverage.py v6.4.2, created at 2022-07-22 15:59 +0100

1import esprit, csv 

2 

3from portality.core import es_connection 

4from portality.util import ipt_prefix 

5 

6 

7def query_result_generator(query, model, page_size=1000, keepalive="1m", wrap=False): 

8 for result in model.iterate(q=query, page_size=page_size, keepalive=keepalive, wrap=wrap): 

9 yield result 

10 

11 

12def report_to_csv(result_generator, headers, output_map, output_file): 

13 with open(output_file, "w", encoding="utf-8") as f: 

14 writer = csv.writer(f) 

15 writer.writerow(headers) 

16 

17 for result in result_generator: 

18 record = output_map(result) 

19 row = [] 

20 for h in headers: 

21 row.append(record[h]) 

22 writer.writerow(row)