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,95 +1,87 @@
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=[]):
root = ET.Element('ResultList') results = event.get_result(o_classes)
root.set('xmlns', 'http://www.orienteering.org/datastandard/3.0')
root.set('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance') root = ET.Element('ResultList')
root.set('iofVersion', '3.0') root.set('xmlns', 'http://www.orienteering.org/datastandard/3.0')
root.set('createTime', datetime.datetime.now().isoformat(timespec='seconds')) root.set('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance')
root.set('creator', 'oTime') root.set('iofVersion', '3.0')
root.set('status', 'Complete') root.set('createTime', datetime.datetime.now().isoformat(timespec='seconds'))
tree = ET.ElementTree(root) root.set('creator', 'oTime')
event = ET.SubElement(root, 'Event') root.set('status', 'Complete')
xml_child(event, 'Id', self.id) tree = ET.ElementTree(root)
xml_child(event, 'Name', self.name) xml_event = ET.SubElement(root, 'Event')
for i in self.o_classes: xml_child(xml_event, 'Id', event.id)
print('Hmmmmmm') xml_child(xml_event, 'Name', event.name)
# <ClassResult> for i in results:
class_result = ET.SubElement(root, 'ClassResult') # <ClassResult>
# <Class> class_result = ET.SubElement(root, 'ClassResult')
t = ET.SubElement(class_result, 'Class') # <Class>
xml_child(t, 'Name', i.name) t = ET.SubElement(class_result, 'Class')
# <PersonResult> xml_child(t, 'Name', i.name)
runners_same_c = get_runners_in_class(self.runners, i) # <PersonResult>
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')
xml_child(person, 'Id', n.id) xml_child(person, 'Id', n.id)
# <Name> # <Name>
name = ET.SubElement(person, 'Name') name = ET.SubElement(person, 'Name')
xml_child(name, 'Family', n.last) xml_child(name, 'Family', n.last)
xml_child(name, 'Given', n.first) xml_child(name, 'Given', n.first)
# </Name> # </Name>
# </Person> # </Person>
# <Organisation> # <Organisation>
org = ET.SubElement(person_result, 'Organisation') org = ET.SubElement(person_result, 'Organisation')
xml_child(org, 'Id', n.club_id) xml_child(org, 'Id', n.club_id)
xml_child(org, 'Name', n.club) xml_child(org, 'Name', n.club)
country = ET.SubElement(org, 'Country') country = ET.SubElement(org, 'Country')
# TODO: hent land fra løperobjektet # TODO: hent land fra løperobjektet
country.text = 'Norway' country.text = 'Norway'
country.set('code', 'NOR') country.set('code', 'NOR')
# </Organisation> # </Organisation>
# <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.total_time - i.runner_results[0].total_time)
xml_child(result, 'TimeBehind', n.totaltime() - runners_ranked[0].totaltime()) # </TimeBehind>
# </TimeBehind> xml_child(result, 'Position', n.place)
xml_child(result, 'Position', n.rank(self.runners)) 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(i.course.codes, n.splits):
for code, split in zip(n.card_r.controls, n.card_r.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) # </SplitTime>
if code == n.res_codes()[-1]: elif n.status == 'MissingPunch':
break xml_child(result, 'Status', n.status)
# </SplitTime> for code, split in zip(i.course.codes, n.splits):
elif n.status() == 'Disqualified': st = ET.SubElement(result, 'SplitTime')
xml_child(result, 'Status', n.status()) xml_child(st, 'ControlCode', code)
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())
else: else:
xml_child(result, 'Status', n.status()) xml_child(result, 'Status', n.status)
# </Result> else:
# </PersonResult> xml_child(result, 'Status', n.status)
# </Class> # </Result>
ET.indent(root, space=' ', level=0) # </PersonResult>
return tree # </Class>
ET.indent(root, space=' ', level=0)
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()