-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcsv_parse.py
More file actions
executable file
·83 lines (68 loc) · 2.36 KB
/
csv_parse.py
File metadata and controls
executable file
·83 lines (68 loc) · 2.36 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env python2
import codecs
import csv
from field_values import CheckboxField, DropdownField, TextField
def validation_warning(error_string):
print("VALIDATION WARNING: %s" % error_string)
def validate_csv_row(row_dict):
expected_columns = ['field_type', 'field_name', 'field_value']
missing_expected_columns = filter(
lambda column: column not in row_dict.keys(),
expected_columns,
)
if len(missing_expected_columns) != 0:
validation_warning(
"csv row %s is missing required keys %s" % (
row_dict,
missing_expected_columns
)
)
return False
expected_field_types = [
'text', 'dropdown', 'checkbox'
]
field_type = row_dict['field_type'].lower()
if field_type not in expected_field_types:
validation_warning(
"field_type %s is not one of exected values %s" % (
row_dict,
expected_field_types
)
)
return False
return True
def get_fields_from_file(csvfile):
fields = []
reader = csv.DictReader(csvfile)
for row in reader:
if not validate_csv_row(row):
continue
field_type = row["field_type"].lower()
field_name = row['field_name']
field_value = row['field_value']
if field_type == "text":
fields.append(TextField(field_name, field_value))
elif field_type == "dropdown":
fields.append(DropdownField(field_name, field_value))
elif field_type == "checkbox":
if not CheckboxField.validate_value(field_value):
validation_warning(
'Invalid value "%s" for checkbox field "%s"' % (
row['field_value'],
row['field_name'],
))
continue
fields.append(CheckboxField(field_name, field_value))
else:
print('Unexpected field_type "%s" encountered in row %s' % (
field_type,
row
)
)
exit(1)
return fields
def get_fields(csvfilename):
with codecs.open(csvfilename, 'rb', encoding='ascii', errors='ignore') as csvfile:
return get_fields_from_file(csvfile)
if __name__ == "__main__":
print get_fields("data.csv")