Dytta xml koden inn i egen fil.

This commit is contained in:
Trygve 2023-11-07 15:57:44 +01:00
parent d008355498
commit bf0b400708
4 changed files with 88 additions and 88 deletions

View File

@ -1,11 +1,14 @@
import xml.etree.ElementTree as etree import datetime
import xml.etree.ElementTree as ET
def xml_child(parent, tag, content): def xml_child(parent, tag, content):
# Used to make creating xml files easier # Used to make creating xml files easier
e = ET.SubElement(parent, tag) e = ET.SubElement(parent, tag)
e.text = str(content) e.text = str(content)
def create_result_xml(self): def create_result_file(event, file_path, o_classes=[]):
results = event.get_result(o_classes)
root = ET.Element('ResultList') root = ET.Element('ResultList')
root.set('xmlns', 'http://www.orienteering.org/datastandard/3.0') root.set('xmlns', 'http://www.orienteering.org/datastandard/3.0')
root.set('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance') root.set('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance')
@ -14,24 +17,18 @@ def xml_child(parent, tag, content):
root.set('creator', 'oTime') root.set('creator', 'oTime')
root.set('status', 'Complete') root.set('status', 'Complete')
tree = ET.ElementTree(root) tree = ET.ElementTree(root)
event = ET.SubElement(root, 'Event') xml_event = ET.SubElement(root, 'Event')
xml_child(event, 'Id', self.id) xml_child(xml_event, 'Id', event.id)
xml_child(event, 'Name', self.name) xml_child(xml_event, 'Name', event.name)
for i in self.o_classes: for i in results:
print('Hmmmmmm')
# <ClassResult> # <ClassResult>
class_result = ET.SubElement(root, 'ClassResult') class_result = ET.SubElement(root, 'ClassResult')
# <Class> # <Class>
t = ET.SubElement(class_result, 'Class') t = ET.SubElement(class_result, 'Class')
xml_child(t, 'Name', i.name) xml_child(t, 'Name', i.name)
# <PersonResult> # <PersonResult>
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)
for n in runners_ranked: for n in i.runner_results:
person_result = ET.SubElement(class_result, 'PersonResult') person_result = ET.SubElement(class_result, 'PersonResult')
# <Person> # <Person>
person = ET.SubElement(person_result, 'Person') person = ET.SubElement(person_result, 'Person')
@ -54,42 +51,37 @@ def xml_child(parent, tag, content):
# <Result> # <Result>
result = ET.SubElement(person_result, 'Result') result = ET.SubElement(person_result, 'Result')
# TODO: Dette bør skrives om til å bruke Runner metoder så mye som mulig. if n.status == 'OK' or n.status == 'MissingPunch':
if hasattr(n, 'card_r') and len(n.card_r.splits) > 2: xml_child(result, 'StartTime', n.start_time.isoformat())
xml_child(result, 'StartTime', n.card_r.s_time.isoformat()) xml_child(result, 'FinishTime', n.end_time.isoformat())
xml_child(result, 'FinishTime', n.card_r.f_time.isoformat()) xml_child(result, 'Time', n.total_time)
xml_child(result, 'Time', n.totaltime()) if n.status == 'OK':
if n.status() == 'OK':
# <TimeBehind> # <TimeBehind>
xml_child(result, 'TimeBehind', n.totaltime() - runners_ranked[0].totaltime()) xml_child(result, 'TimeBehind', n.total_time - i.runner_results[0].total_time)
# </TimeBehind> # </TimeBehind>
xml_child(result, 'Position', n.rank(self.runners)) xml_child(result, 'Position', n.place)
xml_child(result, 'Status', n.status()) xml_child(result, 'Status', n.status)
# <SplitTime> # <SplitTime>
# TODO: ta utgangspunkt i løypa, ikke det brikka har stempla # TODO: ta utgangspunkt i løypa, ikke det brikka har stempla
for code, split in zip(n.card_r.controls, n.card_r.splits): for code, split in zip(i.course.codes, n.splits):
st = ET.SubElement(result, 'SplitTime') st = ET.SubElement(result, 'SplitTime')
xml_child(st, 'ControlCode', code) xml_child(st, 'ControlCode', code)
xml_child(st, 'Time', split) xml_child(st, 'Time', split)
if code == n.res_codes()[-1]:
break
# </SplitTime> # </SplitTime>
elif n.status() == 'Disqualified': elif n.status == 'MissingPunch':
xml_child(result, 'Status', n.status()) xml_child(result, 'Status', n.status)
for code, split in zip(n.res_codes(), n.res_splits()): for code, split in zip(i.course.codes, n.splits):
st = ET.SubElement(result, 'SplitTime') st = ET.SubElement(result, 'SplitTime')
xml_child(st, 'ControlCode', code) xml_child(st, 'ControlCode', code)
if split is not None:
xml_child(st, 'Time', split)
else: else:
xml_child(result, 'Status', n.status()) xml_child(result, 'Status', n.status)
else: else:
xml_child(result, 'Status', n.status()) xml_child(result, 'Status', n.status)
# </Result> # </Result>
# </PersonResult> # </PersonResult>
# </Class> # </Class>
ET.indent(root, space=' ', level=0) ET.indent(root, space=' ', level=0)
return tree tree.write(file_path)
def runners_from_xml_entries(xml_file): def runners_from_xml_entries(xml_file):
tree = ET.parse(xml_file) tree = ET.parse(xml_file)

View File

@ -152,6 +152,11 @@ class CardDump:
else: else:
s_time = read_time s_time = read_time
f_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)) cards.append(CardDump(int(row[6]), controls, splits, read_time, s_time, f_time))
return cards return cards
@ -183,6 +188,7 @@ class RunnerResult:
self.first = first self.first = first
self.last = last self.last = last
self.club = club self.club = club
self.club_id = club_id
self.country = country self.country = country
self.card_id = card_id self.card_id = card_id
self.o_class = o_class self.o_class = o_class
@ -207,7 +213,7 @@ class ClassResult:
self.runner_results = runner_results self.runner_results = runner_results
class Event: 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=[], organiser=None, courses=[], o_classes=[], runners=[],
card_dumps=[], fees=[]): card_dumps=[], fees=[]):
self.id = eventid 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] 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), 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), 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), 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] 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. # TODO: Take string instead of file.
def courses_from_ttime_conf(ttime_file): def courses_from_ttime_conf(ttime_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 col_width = pdf.epw / 4 # distribute content evenly
for class_result in results: 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.write(txt=class_result.name)
pdf.ln(line_height) pdf.ln(line_height)
for runner in class_result.runner_results: for runner in class_result.runner_results:

View File

@ -2,6 +2,7 @@ import sys
sys.path.insert(0, '../../otime') sys.path.insert(0, '../../otime')
import otime import otime
import pdf import pdf
import iof_xml
def main(): def main():
event = otime.Event(0, 'TEEEST', start_time=None, end_time=None,organiser='Tygbe') 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_ttime_db('db.csv')
event.read_mtr_file('mtr.csv') event.read_mtr_file('mtr.csv')
pdf.create_split_result_list(event, 'output/result.pdf') pdf.create_split_result_list(event, 'output/result.pdf')
iof_xml.create_result_file(event, 'output/result.xml')
print(event.get_runner_status('1400')) print(event.get_runner_status('1400'))
results = event.get_result() results = event.get_result()