Compare commits

...

5 Commits

6 changed files with 137 additions and 37 deletions

41
otime/file_io.py Normal file
View File

@ -0,0 +1,41 @@
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

View File

@ -1,5 +1,6 @@
import datetime
import xml.etree.ElementTree as ET
import otime
def xml_child(parent, tag, content):
# Used to make creating xml files easier
@ -73,6 +74,7 @@ def create_result_file(event, file_path, o_classes=[]):
for code, split in zip(i.course.codes, n.splits):
st = ET.SubElement(result, 'SplitTime')
xml_child(st, 'ControlCode', code)
if split != 0: xml_child(st, 'Time', split)
else:
xml_child(result, 'Status', n.status)
else:
@ -119,8 +121,8 @@ def runners_from_xml_entries(xml_file):
card = None
start_time = None
runners.append(Runner(rid, first, last, club=club_name, club_id=club_id,
country=country,card=card, o_class_str=class_str,
runners.append(otime.Runner(rid, first, last, club=club_name, club_id=club_id,
country=country,card_id=card, o_class_str=class_str,
start_time=start_time, fee_id=fee_id))
return runners
@ -150,7 +152,7 @@ def fees_from_xml_entries(xml_file):
except AttributeError:
to_birth_date = None
fee_objs.append(Fee(fee_id, name, currency, amount,
fee_objs.append(otime.Fee(fee_id, name, currency, amount,
from_birth_date=from_birth_date, to_birth_date=to_birth_date))
return fee_objs
@ -170,5 +172,36 @@ def courses_from_xml(xml_file):
controls.remove('STA1')
controls.remove('FIN1')
controls = [int(l) for l in controls]
courseobjs.append(Course(name, controls))
courseobjs.append(otime.Course(name, controls))
return courseobjs
def event_from_xml_entries(xml_file):
tree = ET.parse(xml_file)
root = tree.getroot()
url = '{http://www.orienteering.org/datastandard/3.0}'
event_el = root.find(f'./{url}Event')
event_id = int(event_el.find(f'./{url}Id').text)
name = event_el.find(f'./{url}Name').text
organiser = event_el.find(f'./{url}Organiser/{url}Name').text
start_ds = event_el.find(f'./{url}StartTime/{url}Date').text
start_ts = event_el.find(f'./{url}StartTime/{url}Time').text[:-1]
start_time = datetime.datetime.fromisoformat(f'{start_ds}T{start_ts}')
end_ds = event_el.find(f'./{url}EndTime/{url}Date').text
end_ts = event_el.find(f'./{url}EndTime/{url}Time').text[:-1]
end_time = datetime.datetime.fromisoformat(f'{end_ds}T{end_ts}')
person_entries = root.findall(f'./{url}PersonEntry')
class_names = []
for p_entry in person_entries:
class_names.append(p_entry.find(f'./{url}Class/{url}Name').text)
o_classes = [otime.OClass(i, []) for i in set(class_names)]
runners = runners_from_xml_entries(xml_file)
fees = fees_from_xml_entries(xml_file)
return otime.Event(event_id, name, organiser=organiser, runners=runners,
fees=fees, start_time=start_time, end_time=end_time, o_classes=o_classes)

36
otime/main.py Normal file
View File

@ -0,0 +1,36 @@
import argparse
import file_io
import iof_xml
# Main entrypoint for now. Cli with two options; init will make the files needed and run will start the program from the specified directory
def main():
parser = argparse.ArgumentParser(description='Otime very alpha version 😁')
subparsers = parser.add_subparsers(dest='command')
parser_init = subparsers.add_parser('init', help='INIT')
parser_init.add_argument('--entries', required=True, dest='entries_file', action='store', default='./',
help='The xml file with entries')
parser_init.add_argument('--courses', required=True, dest='courses_file', action='store', default='./',
help='The xml with courses')
parser_init.add_argument('--dir', required=False, dest='dir', action='store', default='./',
help='Specify a directort, if not set the files are created in the current directory')
args = parser.parse_args()
if args.command == 'init':
init_dir(args.dir, args.entries_file, args.courses_file)
def init_dir(project_dir, entries_xml_file, courses_xml_file):
# Lager mappe med en config fil, en csv fil med løpere og en fil med mtr data
csv_db_path = project_dir + '/runners.csv'
config_path = project_dir + '/config.yaml'
mtr_path = project_dir + 'mtr.yaml'
event = iof_xml.event_from_xml_entries(entries_xml_file)
event.courses = iof_xml.courses_from_xml(courses_xml_file)
file_io.write_runners_csv(event, csv_db_path)
file_io.write_config(event, config_path)
file_io.write_card_dumps(event, mtr_path)
if __name__ == '__main__':
main()

View File

@ -7,7 +7,7 @@ import pdf
class Runner:
def __init__(self, id: int, first: str, last: str, club=None, club_id=None,
country=None, card_id=None, o_class_str=None, o_class=None,
fork=0, start_time=None, fee_id=None, fee=None):
fork=0, start_time=None, fee_id=None, fee=None, status_override=''):
self.id = id
self.first = first
self.last = last
@ -19,7 +19,7 @@ class Runner:
self.fork = fork
self.start_time = start_time
self.fee_id = fee_id
self.status_override = ''
self.status_override = status_override
def __repr__(self):
return(f'name({self.fullname()})')
@ -289,6 +289,7 @@ class Event:
return False
def get_runner_splits(self, id):
# Tida brukt frem til hver post, ikke tida fra forrige post
try:
runner = self.get_runner(id)
card_dump = self.get_card_dump(runner.card_id)
@ -304,17 +305,14 @@ class Event:
for n, control in enumerate(course.codes):
if control not in card_dump.controls:
continue
split_debt = 0
while True:
try:
punched_control, split = next(split_iter)
except StopIteration:
break
if punched_control == control:
splits[n] = split + split_debt
splits[n] = split
break
else:
split_debt += split
return splits
def get_runner_controls(self, id):
@ -339,29 +337,6 @@ class Event:
self.add_fees(*fees_from_xml_entries(xml_file))
# Må endres
def from_xml_entries(xml_file):
tree = ET.parse(xml_file)
root = tree.getroot()
url = '{http://www.orienteering.org/datastandard/3.0}'
event_el = root.find(f'./{url}Event')
event_id = int(event_el.find(f'./{url}Id').text)
name = event_el.find(f'./{url}Name').text
organiser = event_el.find(f'./{url}Organiser/{url}Name').text
start_ds = event_el.find(f'./{url}StartTime/{url}Date').text
start_ts = event_el.find(f'./{url}StartTime/{url}Time').text[:-1]
start_time = datetime.datetime.fromisoformat(f'{start_ds}T{start_ts}')
end_ds = event_el.find(f'./{url}EndTime/{url}Date').text
end_ts = event_el.find(f'./{url}EndTime/{url}Time').text[:-1]
end_time = datetime.datetime.fromisoformat(f'{end_ds}T{end_ts}')
runners = runners_from_xml_entries(xml_file)
fees = fees_from_xml_entries(xml_file)
return Event(event_id, name, organiser=organiser, runners=runners,
fees=fees, start_time=start_time, end_time=end_time)
def read_xml_courses(self, xml_file):
self.courses = courses_from_xml(xml_file)

View File

@ -44,7 +44,7 @@ def create_split_result_list(event, file_path, o_classes=[]):
col_width = pdf.epw / 4 # distribute content evenly
for class_result in results:
col_width = pdf.epw / (21+len(class_result.course.codes))
col_width = 10
pdf.write(txt=class_result.name)
pdf.ln(line_height)
for runner in class_result.runner_results:

View File

@ -3,16 +3,31 @@ sys.path.insert(0, '../../otime')
import otime
import pdf
import iof_xml
import file_io
def main():
def ttime_testing():
event = otime.Event(0, 'TEEEST', start_time=None, end_time=None,organiser='Tygbe')
event.read_ttime_cnf('tt.cnf')
event.read_ttime_db('db.csv')
event.read_mtr_file('mtr.csv')
pdf.create_split_result_list(event, 'output/result.pdf')
iof_xml.create_result_file(event, 'output/result.xml')
iof_xml.create_result_file(event, '/home/trygve/Prosjekter/simple-liveresults/resultater/Resultater.xml')
print(event.get_runner_status('1400'))
results = event.get_result()
print([pdf.format_m_s(i) for i in event.get_runner_splits('17')])
print([pdf.format_m_s(i) for i in event.get_card_dump(event.get_runner('17').card_id).splits])
file_io.write_config(event, 'output/config.yaml')
file_io.write_card_dumps(event, 'output/mtr.yaml')
file_io.write_runners_csv(event, 'output/runners.csv')
print(file_io.event_from_yaml_and_csv('output/config.yaml', 'output/mtr.yaml', 'output/runners.csv'))
def xml_testing():
event = iof_xml.event_from_xml_entries('entries.xml')
event.courses = iof_xml.courses_from_xml('course.xml')
file_io.write_config(event, 'output/config.yaml')
file_io.write_card_dumps(event, 'output/mtr.yaml')
file_io.write_runners_csv(event, 'output/runners.csv')
print(file_io.event_from_yaml_and_csv('output/config.yaml', 'output/mtr.yaml', 'output/runners.csv'))
if __name__ == '__main__':
main()
xml_testing()