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):
# 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')
# <ClassResult>
class_result = ET.SubElement(root, 'ClassResult')
# <Class>
t = ET.SubElement(class_result, 'Class')
xml_child(t, 'Name', i.name)
# <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)
def create_result_file(event, file_path, o_classes=[]):
results = event.get_result(o_classes)
for n in runners_ranked:
person_result = ET.SubElement(class_result, 'PersonResult')
# <Person>
person = ET.SubElement(person_result, 'Person')
xml_child(person, 'Id', n.id)
# <Name>
name = ET.SubElement(person, 'Name')
xml_child(name, 'Family', n.last)
xml_child(name, 'Given', n.first)
# </Name>
# </Person>
# <Organisation>
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')
# </Organisation>
# <Result>
result = ET.SubElement(person_result, 'Result')
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:
# <ClassResult>
class_result = ET.SubElement(root, 'ClassResult')
# <Class>
t = ET.SubElement(class_result, 'Class')
xml_child(t, 'Name', i.name)
# <PersonResult>
# 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':
# <TimeBehind>
xml_child(result, 'TimeBehind', n.totaltime() - runners_ranked[0].totaltime())
# </TimeBehind>
xml_child(result, 'Position', n.rank(self.runners))
xml_child(result, 'Status', n.status())
# <SplitTime>
# 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
# </SplitTime>
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())
for n in i.runner_results:
person_result = ET.SubElement(class_result, 'PersonResult')
# <Person>
person = ET.SubElement(person_result, 'Person')
xml_child(person, 'Id', n.id)
# <Name>
name = ET.SubElement(person, 'Name')
xml_child(name, 'Family', n.last)
xml_child(name, 'Given', n.first)
# </Name>
# </Person>
# <Organisation>
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')
# </Organisation>
# <Result>
result = ET.SubElement(person_result, 'Result')
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':
# <TimeBehind>
xml_child(result, 'TimeBehind', n.total_time - i.runner_results[0].total_time)
# </TimeBehind>
xml_child(result, 'Position', n.place)
xml_child(result, 'Status', n.status)
# <SplitTime>
# 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)
# </SplitTime>
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())
# </Result>
# </PersonResult>
# </Class>
ET.indent(root, space=' ', level=0)
return tree
xml_child(result, 'Status', n.status)
else:
xml_child(result, 'Status', n.status)
# </Result>
# </PersonResult>
# </Class>
ET.indent(root, space=' ', level=0)
tree.write(file_path)
def runners_from_xml_entries(xml_file):
tree = ET.parse(xml_file)

View File

@ -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):

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))
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:

View File

@ -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()