otime/otime/file_io.py

41 lines
1.5 KiB
Python

from yaml import load, dump
try:
from yaml import CLoader as Loader, CDumper as Dumper
except ImportError:
from yaml import Loader, Dumper
from copy import deepcopy
import otime
# Disse funksjonene er for å kunne lese og skrive seperate config, mtr og databasefiler.
def write_config(event, file_path):
output_event = deepcopy(event)
output_event.runners = []
output_event.card_dumps = []
with open(file_path, 'w') as f:
dump(output_event, f)
def write_card_dumps(event, file_path):
card_dumps = deepcopy(event.card_dumps)
with open(file_path, 'w') as f:
dump(card_dumps, f)
def write_runners_csv(event, file_path):
with open(file_path, 'w') as f:
for i in event.runners:
f.write(f'{i.id};{i.status_override};{i.first}, {i.last};{i.o_class};{i.club};{i.card_id};{i.fork};{i.start_time}\n')
def event_from_yaml_and_csv(config_path, mtr_path, csv_path):
with open(mtr_path, 'r') as f:
card_dumps = load(f, Loader=Loader)
with open(config_path, 'r') as f:
event = load(f, Loader=Loader)
with open(csv_path, 'r') as f:
data = [i.split(';') for i in f.readlines()]
for i in data: i[2] = i[2].split(',')
runners = [otime.Runner(id=i[0], status_override=i[1], first=i[2][0], last=i[2][1].strip(), o_class=i[3], club=i[4], card_id=i[5], fork=i[6], start_time=i[7]) for i in data]
event.card_dumps = card_dumps
event.runners = runners
return event