diff --git a/otime/iof_xml.py b/otime/iof_xml.py index 95339d5..5ff19f8 100644 --- a/otime/iof_xml.py +++ b/otime/iof_xml.py @@ -1,95 +1,87 @@ -import xml.etree.ElementTree as etree +import datetime +import xml.etree.ElementTree as ET def xml_child(parent, tag, content): # Used to make creating xml files easier e = ET.SubElement(parent, tag) e.text = str(content) - def create_result_xml(self): - root = ET.Element('ResultList') - root.set('xmlns', 'http://www.orienteering.org/datastandard/3.0') - root.set('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance') - root.set('iofVersion', '3.0') - root.set('createTime', datetime.datetime.now().isoformat(timespec='seconds')) - root.set('creator', 'oTime') - root.set('status', 'Complete') - tree = ET.ElementTree(root) - event = ET.SubElement(root, 'Event') - xml_child(event, 'Id', self.id) - xml_child(event, 'Name', self.name) - for i in self.o_classes: - print('Hmmmmmm') - # - class_result = ET.SubElement(root, 'ClassResult') - # - t = ET.SubElement(class_result, 'Class') - xml_child(t, 'Name', i.name) - # - runners_same_c = get_runners_in_class(self.runners, i) - runners_ranked = rank_runners(runners_same_c, i) - # Put the OK runners first and Active last - runners_sorted = [i for i in runners_same_c if i not in runners_ranked] - runners_ranked.extend(runners_sorted) +def create_result_file(event, file_path, o_classes=[]): + results = event.get_result(o_classes) + + root = ET.Element('ResultList') + root.set('xmlns', 'http://www.orienteering.org/datastandard/3.0') + root.set('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance') + root.set('iofVersion', '3.0') + root.set('createTime', datetime.datetime.now().isoformat(timespec='seconds')) + root.set('creator', 'oTime') + root.set('status', 'Complete') + tree = ET.ElementTree(root) + xml_event = ET.SubElement(root, 'Event') + xml_child(xml_event, 'Id', event.id) + xml_child(xml_event, 'Name', event.name) + for i in results: + # + class_result = ET.SubElement(root, 'ClassResult') + # + t = ET.SubElement(class_result, 'Class') + xml_child(t, 'Name', i.name) + # - for n in runners_ranked: - person_result = ET.SubElement(class_result, 'PersonResult') - # - person = ET.SubElement(person_result, 'Person') - xml_child(person, 'Id', n.id) - # - name = ET.SubElement(person, 'Name') - xml_child(name, 'Family', n.last) - xml_child(name, 'Given', n.first) - # - # - # - org = ET.SubElement(person_result, 'Organisation') - xml_child(org, 'Id', n.club_id) - xml_child(org, 'Name', n.club) - country = ET.SubElement(org, 'Country') - # TODO: hent land fra løperobjektet - country.text = 'Norway' - country.set('code', 'NOR') - # - # - result = ET.SubElement(person_result, 'Result') + for n in i.runner_results: + person_result = ET.SubElement(class_result, 'PersonResult') + # + person = ET.SubElement(person_result, 'Person') + xml_child(person, 'Id', n.id) + # + name = ET.SubElement(person, 'Name') + xml_child(name, 'Family', n.last) + xml_child(name, 'Given', n.first) + # + # + # + org = ET.SubElement(person_result, 'Organisation') + xml_child(org, 'Id', n.club_id) + xml_child(org, 'Name', n.club) + country = ET.SubElement(org, 'Country') + # TODO: hent land fra løperobjektet + country.text = 'Norway' + country.set('code', 'NOR') + # + # + result = ET.SubElement(person_result, 'Result') - # TODO: Dette bør skrives om til å bruke Runner metoder så mye som mulig. - if hasattr(n, 'card_r') and len(n.card_r.splits) > 2: - xml_child(result, 'StartTime', n.card_r.s_time.isoformat()) - xml_child(result, 'FinishTime', n.card_r.f_time.isoformat()) - xml_child(result, 'Time', n.totaltime()) - if n.status() == 'OK': - # - xml_child(result, 'TimeBehind', n.totaltime() - runners_ranked[0].totaltime()) - # - xml_child(result, 'Position', n.rank(self.runners)) - xml_child(result, 'Status', n.status()) - # - # TODO: ta utgangspunkt i løypa, ikke det brikka har stempla - for code, split in zip(n.card_r.controls, n.card_r.splits): - st = ET.SubElement(result, 'SplitTime') - xml_child(st, 'ControlCode', code) - xml_child(st, 'Time', split) - if code == n.res_codes()[-1]: - break - # - elif n.status() == 'Disqualified': - xml_child(result, 'Status', n.status()) - for code, split in zip(n.res_codes(), n.res_splits()): - st = ET.SubElement(result, 'SplitTime') - xml_child(st, 'ControlCode', code) - if split is not None: - xml_child(st, 'Time', split) - else: - xml_child(result, 'Status', n.status()) + if n.status == 'OK' or n.status == 'MissingPunch': + xml_child(result, 'StartTime', n.start_time.isoformat()) + xml_child(result, 'FinishTime', n.end_time.isoformat()) + xml_child(result, 'Time', n.total_time) + if n.status == 'OK': + # + xml_child(result, 'TimeBehind', n.total_time - i.runner_results[0].total_time) + # + xml_child(result, 'Position', n.place) + xml_child(result, 'Status', n.status) + # + # TODO: ta utgangspunkt i løypa, ikke det brikka har stempla + for code, split in zip(i.course.codes, n.splits): + st = ET.SubElement(result, 'SplitTime') + xml_child(st, 'ControlCode', code) + xml_child(st, 'Time', split) + # + elif n.status == 'MissingPunch': + xml_child(result, 'Status', n.status) + for code, split in zip(i.course.codes, n.splits): + st = ET.SubElement(result, 'SplitTime') + xml_child(st, 'ControlCode', code) else: - xml_child(result, 'Status', n.status()) - # - # - # - ET.indent(root, space=' ', level=0) - return tree + xml_child(result, 'Status', n.status) + else: + xml_child(result, 'Status', n.status) + # + # + # + ET.indent(root, space=' ', level=0) + tree.write(file_path) def runners_from_xml_entries(xml_file): tree = ET.parse(xml_file) diff --git a/otime/otime.py b/otime/otime.py index 05796f2..fd045f0 100644 --- a/otime/otime.py +++ b/otime/otime.py @@ -152,6 +152,11 @@ class CardDump: else: s_time = read_time f_time = read_time + + # Remove mtr from splits: + if controls[-1] == 250: + controls.pop(-1) + splits.pop(-1) cards.append(CardDump(int(row[6]), controls, splits, read_time, s_time, f_time)) return cards @@ -183,6 +188,7 @@ class RunnerResult: self.first = first self.last = last self.club = club + self.club_id = club_id self.country = country self.card_id = card_id self.o_class = o_class @@ -207,7 +213,7 @@ class ClassResult: self.runner_results = runner_results class Event: - def __init__(self, eventid, name, start_time=None, end_time=None, + def __init__(self, eventid=0, name=None, start_time=None, end_time=None, organiser=None, courses=[], o_classes=[], runners=[], card_dumps=[], fees=[]): self.id = eventid @@ -397,13 +403,13 @@ def produce_class_result(event, o_class_name) -> ClassResult: other_runners = [i for i in runners if i not in ok_runners and i not in dsq_runners] results = [RunnerResult(i.id, i.first, i.last, event.get_runner_status(i.id), ok_runners.index(i)+1, event.get_runner_time(i.id), event.get_runner_splits(i.id), - event.get_runner_end_clock(i.id), i.club, i.country, i.card_id, i.o_class, event.get_runner_controls(i.id)) for i in ok_runners] + event.get_runner_end_clock(i.id), i.club, i.country, i.card_id, i.o_class, event.get_runner_controls(i.id), start_time=event.get_card_dump(i.card_id).s_time) for i in ok_runners] results += [RunnerResult(i.id, i.first, i.last, event.get_runner_status(i.id), 0, event.get_runner_time(i.id), event.get_runner_splits(i.id), - event.get_runner_end_clock(i.id), i.club, i.country, i.card_id, i.o_class, event.get_runner_controls(i.id)) for i in dsq_runners] + event.get_runner_end_clock(i.id), i.club, i.country, i.card_id, i.o_class, event.get_runner_controls(i.id), start_time=event.get_card_dump(i.card_id).s_time) for i in dsq_runners] results += [RunnerResult(i.id, i.first, i.last, event.get_runner_status(i.id), 0, event.get_runner_time(i.id), event.get_runner_splits(i.id), event.get_runner_end_clock(i.id), i.club, i.country, i.card_id, i.o_class, event.get_runner_controls(i.id)) for i in other_runners] - return ClassResult(o_class.name, o_class.course, results) + return ClassResult(o_class.name, event.get_course(o_class.course), results) # TODO: Take string instead of file. def courses_from_ttime_conf(ttime_file): diff --git a/otime/pdf.py b/otime/pdf.py index 6bec629..227994e 100644 --- a/otime/pdf.py +++ b/otime/pdf.py @@ -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)) + col_width = pdf.epw / (21+len(class_result.course.codes)) pdf.write(txt=class_result.name) pdf.ln(line_height) for runner in class_result.runner_results: diff --git a/test_files/sc3_23/sc3_test.py b/test_files/sc3_23/sc3_test.py index 6bcd60f..c4cf475 100644 --- a/test_files/sc3_23/sc3_test.py +++ b/test_files/sc3_23/sc3_test.py @@ -2,6 +2,7 @@ import sys sys.path.insert(0, '../../otime') import otime import pdf +import iof_xml def main(): event = otime.Event(0, 'TEEEST', start_time=None, end_time=None,organiser='Tygbe') @@ -9,6 +10,7 @@ def main(): 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') print(event.get_runner_status('1400')) results = event.get_result()