-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert_yaml_to_csv.py
60 lines (50 loc) · 2.06 KB
/
convert_yaml_to_csv.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
#!/usr/bin/env python3
import csv
import os
import shutil
import yaml
def build_context():
context = {}
contents = os.listdir()
context['data_backup_exists'] = 'data_backup' in contents
context['data_exists'] = 'data' in contents
context['ids_file_exists'] = 'ids.yaml' in contents
if context['data_backup_exists']:
backup_contents = os.listdir('data_backup/')
context['ids_in_backup'] = 'ids.yaml' in backup_contents
if context['data_exists']:
data_contents = os.listdir('data/')
context['hours_in_data'] = 'hours.csv' in data_contents
context['rfids_in_data'] = 'rfids.csv' in data_contents
return context
def convert_yaml_to_csv():
# open ids.yaml for reading
with open('ids.yaml', 'r') as f:
y = yaml.safe_load(f.read())
# dump access time labels to hours.csv
with open('data/hours.csv', 'w') as csvfile:
writer = csv.writer(csvfile, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
writer.writerow(["name", "start_hour", "end_hour"])
for level in y['levels']:
writer.writerow([level, y['levels'][level]['hours'][0], y['levels'][level]['hours'][1]])
# dump rfid entries to rfids.csv
with open('data/rfids.csv', 'w') as csvfile:
writer = csv.writer(csvfile, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
writer.writerow(['rfid', 'access_times', 'sponsor'])
for sponsor in y['rfids']:
for item in y['rfids'][sponsor]:
writer.writerow([item['id'], item['level'], sponsor])
def main():
ctx = build_context()
if not ctx['ids_file_exists']:
raise ValueError("ids.yaml not available, quitting")
if not ctx['data_backup_exists']:
os.mkdir('data_backup')
shutil.copy('ids.yaml', 'data_backup')
if not ctx['data_exists']:
os.mkdir('data')
if ctx.get('rfids_in_data'):
raise ValueError("rfids.csv already exists, quitting")
convert_yaml_to_csv()
if __name__ == '__main__':
main()