-
Notifications
You must be signed in to change notification settings - Fork 2
/
indico_to_yaml.py
82 lines (63 loc) · 2.57 KB
/
indico_to_yaml.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# =============================================================================
# @file indico_to_yaml.py
# @author Albert Puig ([email protected])
# @date 07.04.2016
# =============================================================================
"""Get indico registration list and convert it to yaml."""
import os
import argparse
import csv
import yaml
def load_csv_registration(file_name):
"""Loads the CSV-formatted registration.
Assumes the first row contains the column definitions.
Arguments:
file_name (str): File to load.
Returns:
list: Registrants as `dict` according to the column
definitions.
Raises:
OSError: If the file does not exist.
"""
if not os.path.exists(file_name):
raise OSError("Input CSV doesn't exist -> %s" % file_name)
with open(file_name) as csv_file:
registrants_csv = [row for row in csv.reader(csv_file, delimiter=',')]
col_definitions = [c.replace('\ufeff', '') for c in registrants_csv.pop(0)]
print('col_definitions are', col_definitions)
registrants = []
for registrant_row in registrants_csv:
registrants.append({col_definitions[index]: val
for index, val in enumerate(registrant_row)})
return registrants
def build_yaml_def(registrants_list):
"""Convert the registrants list into a YAML object.
Keeps the registrant ID, name, email and university and puts them
into the 'registrants' YAML list.
Arguments:
registrants_list (list): List of dictionaries defining
the registered people.
Returns:
str: YAML string.
Raises:
KeyError: If some of the required columns is missing.
"""
output_list = []
for registrant in registrants_list:
output_list.append({'id': registrant['ID'],
'name': registrant['Name'],
'email': registrant['Email Address'],
'institute': registrant['Affiliation'] or '\-'})
return yaml.dump({'registrants': output_list}, default_flow_style=False)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('inputfile', action='store', type=str, help="Input CSV file")
parser.add_argument('outputfile', action='store', type=str, help="Output YAML file")
args = parser.parse_args()
with open(args.outputfile, 'w') as out:
out.write('---\n')
out.write(build_yaml_def(load_csv_registration(args.inputfile)))
out.write('...\n')
# EOF