-
Notifications
You must be signed in to change notification settings - Fork 2
/
iol.py
62 lines (56 loc) · 2.09 KB
/
iol.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
"""
Shared methods for performing ETL on DATIM metadata
"""
def get_as_csv(rows, start_columns=None, include_columns=None, exclude_columns=None,
include_extras=True):
""" Returm as CSV """
if not rows:
return ''
if not isinstance(exclude_columns, list):
exclude_columns = []
# Build CSV column definition
if include_columns:
csv_columns = include_columns
else:
csv_columns = list(rows[0].keys())
if 'extras' in csv_columns:
csv_columns.remove('extras')
# Add unique extra attributes to the CSV column definitions
for row in rows:
if 'extras' not in row:
continue
for attr_key in row['extras']:
column_key = 'attr:%s' % attr_key
if column_key not in csv_columns:
if (include_columns and column_key in include_columns) or (not include_columns and column_key not in exclude_columns):
csv_columns.append('attr:%s' % attr_key)
# Apply start columns to CSV column definitions
if start_columns:
count = 0
for key in start_columns:
if key in csv_columns:
csv_columns.remove(key)
csv_columns.insert(count, key)
count += 1
# Remove exclude columns from CSV column definitions
if not include_columns and exclude_columns:
for key in exclude_columns:
if key in csv_columns:
csv_columns.remove(key)
# Generate the CSV output
import csv
import io
output_stream = io.StringIO()
writer = csv.DictWriter(output_stream, fieldnames=csv_columns)
writer.writeheader()
for row in rows:
csv_row = {}
for csv_column in csv_columns:
if csv_column[:5] == 'attr:':
extra_key = csv_column[5:]
if 'extras' in row and extra_key in row['extras']:
csv_row[csv_column] = row['extras'][extra_key]
else:
csv_row[csv_column] = row.get(csv_column)
writer.writerow(csv_row)
return output_stream.getvalue().strip('\r\n')