forked from Trygve/otime
228 lines
7.2 KiB
Python
228 lines
7.2 KiB
Python
import datetime
|
|
import csv
|
|
import sqlite3
|
|
import re
|
|
from rich.console import Console
|
|
from rich.table import Table
|
|
from rich.traceback import install
|
|
import xml.etree.ElementTree as ET
|
|
install(show_locals=True)
|
|
class runner:
|
|
def __init__(self, first, last, club, country, card, o_class, controls, splits):
|
|
self.first = first
|
|
self.last = last
|
|
self.club = club
|
|
self.country = country
|
|
self.card = card
|
|
self.o_class = o_class
|
|
self.controls = controls
|
|
self.splits = splits
|
|
|
|
def fullname(self):
|
|
return '{} {}'.format(self.first, self.last)
|
|
def check_codes(self):
|
|
# Returns False if not ok and touple if ok
|
|
return contains(self.o_class.course.codes, self.controls)
|
|
def totaltime(self):
|
|
return self.splits[-2]
|
|
|
|
def status(self):
|
|
if self.controls == []:
|
|
return 'Active'
|
|
elif self.check_codes():
|
|
return 'OK'
|
|
elif self.check_codes() == False:
|
|
return 'Disqualified'
|
|
|
|
|
|
class course:
|
|
def __init__(self, name, codes):
|
|
self.name = name
|
|
self.codes = codes
|
|
|
|
class o_class:
|
|
def __init__(self, name, course):
|
|
self.name = name
|
|
self.course = course
|
|
|
|
def courses_from_ttime_conf(ttime_file = 'sc_2021_ttime/ttime.cnf.txt'):
|
|
courses = []
|
|
conf = open(ttime_file).readlines()
|
|
for line in conf:
|
|
if '-codes' in line:
|
|
code_list = re.search(r'(?<=\")(.*?)(?=\")', line).group().split(';')
|
|
loops = 0
|
|
for n in code_list:
|
|
n = n.split(',')
|
|
loops += 1
|
|
n = list(map(int, n))
|
|
courses.append(course('course_'+str(loops), n))
|
|
return courses
|
|
def classes_from_ttime_conf(ttime_file, courses):
|
|
o_classes = []
|
|
conf = open(ttime_file).readlines()
|
|
for line in conf:
|
|
if '-courses' in line:
|
|
raw_courselist = re.search(r'(?<=\")(.*?)(?=\")', line).group().split(';')
|
|
loops = 0
|
|
for n in raw_courselist:
|
|
n = n.split(',')
|
|
for i in n:
|
|
o_classes.append(o_class(i,courses[loops]))
|
|
loops += 1
|
|
|
|
|
|
return o_classes
|
|
|
|
def xml_to_class(xml_file, o_class_list=[]):
|
|
tree = ET.parse(xml_file)
|
|
root = tree.getroot()
|
|
runnerarray = []
|
|
|
|
for person_root in root[1:]:
|
|
first = person_root[1][1][1].text
|
|
last = person_root[1][1][0].text
|
|
try:
|
|
club = person_root[2][1].text
|
|
except:
|
|
club = "None"
|
|
country = person_root[1][3].text
|
|
try:
|
|
card = int(person_root[3].text)
|
|
except:
|
|
card = 0
|
|
try:
|
|
xml_class_str = person_root[4][1].text
|
|
except:
|
|
# VELDIG MIDLERTIDIG
|
|
runner_o_class = o_class_list[0]
|
|
else:
|
|
for i in o_class_list:
|
|
if i.name == xml_class_str:
|
|
runner_o_class = i
|
|
break
|
|
# Gjør sånn at den lager nye o klasser om den ikke finnes fra før
|
|
runnerarray.append(runner(first, last, club, country, card, runner_o_class, [], []))
|
|
return runnerarray
|
|
def ttime_db_to_class(ttime_file, o_class_list=[]):
|
|
csvreader = csv.reader(open(ttime_file, 'r', encoding='latin_1'), delimiter=';',)
|
|
runnerarray = []
|
|
for row in csvreader:
|
|
country = ''
|
|
name = row[2].split(',')
|
|
first = name[1]
|
|
last = name[0]
|
|
try:
|
|
club = row[4]
|
|
except:
|
|
club = "None"
|
|
try:
|
|
card = int(row[6])
|
|
except:
|
|
card = 0
|
|
try:
|
|
raw_class_str = row[3]
|
|
except:
|
|
# VELDIG MIDLERTIDIG
|
|
runner_o_class = o_class_list[0]
|
|
else:
|
|
for i in o_class_list:
|
|
if i.name == raw_class_str:
|
|
runner_o_class = i
|
|
break
|
|
# TODO: Gjør sånn at den lager nye o klasser om den ikke finnes fra før
|
|
runnerarray.append(runner(first, last, club, country, card, runner_o_class, [], []))
|
|
return runnerarray
|
|
|
|
def ttime_mtr_to_class(csv_file, runnerarray):
|
|
csvreader = csv.reader(open(csv_file))
|
|
fields = next(csvreader)
|
|
|
|
fields = []
|
|
rows = []
|
|
# hver rad er brikkenummer med tilhørende info
|
|
for row in csvreader:
|
|
rows.append(row)
|
|
for row in rows:
|
|
controls = []
|
|
splits = []
|
|
# postkodene kommer på oddetall fra og med den 11. De blir hevet inn i controls
|
|
for item in row[11::2]:
|
|
if item != '000' and item != '0000' and item != '00000':
|
|
controls.append(int(item))
|
|
# strekktidene kommer på partall fra og med den 12. De blir hevet i splits.
|
|
for item in row[12::2]:
|
|
if item != '000' and item != '0000' and item != '00000':
|
|
splits.append(int(item))
|
|
# looper gjonnom løperobjektene og legger til poster og strekktider
|
|
for runner in runnerarray:
|
|
if runner.card == int(row[6]):
|
|
runner.controls = controls
|
|
runner.splits = splits
|
|
|
|
def contains(small, big):
|
|
for i in range(len(big)-len(small)+1):
|
|
for j in range(len(small)):
|
|
if big[i+j] != small[j]:
|
|
break
|
|
else:
|
|
return i, i+len(small)
|
|
return True
|
|
return False
|
|
|
|
def filter_runners_by_class(runners, o_class):
|
|
list_filtrd = []
|
|
for i in runners:
|
|
if i.o_class == o_class:
|
|
list_filtrd.append(i)
|
|
|
|
return list_filtrd
|
|
|
|
def gen_xml_result(runners, o_classes):
|
|
root = ET.Element('ResultList')
|
|
tree = ET.ElementTree(root)
|
|
event = ET.SubElement(root, 'Event')
|
|
for i in o_classes:
|
|
#<ClassResult>
|
|
class_result = ET.SubElement(event, 'ClassResult')
|
|
#<Class>
|
|
t = ET.SubElement(class_result, 'Class')
|
|
name = ET.SubElement(t, 'Name')
|
|
name.text = i.name
|
|
#<PersonResult>
|
|
for n in filter_runners_by_class(runners, i):
|
|
person_result = ET.SubElement(class_result, 'PersonResult')
|
|
#<Person>
|
|
person = ET.SubElement(person_result, 'Person')
|
|
eventorid = ET.SubElement(person, 'Id')
|
|
eventorid.text = '0'
|
|
#<Name>
|
|
name = ET.SubElement(person, 'Name')
|
|
family = ET.SubElement(name, 'Family')
|
|
family.text = n.last
|
|
given = ET.SubElement(name, 'Given')
|
|
given.text = n.first
|
|
#</Name>
|
|
#</Person>
|
|
#<Organisation>
|
|
org = ET.SubElement(person_result, 'Organisation')
|
|
orgid = ET.SubElement(org, 'Id')
|
|
orgid.text = '0'
|
|
name = ET.SubElement(org, 'Name')
|
|
name.text = 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')
|
|
status = ET.SubElement(result, 'Status')
|
|
status.text = n.status()
|
|
#</Result>
|
|
#</PersonResult>
|
|
#</Class>
|
|
|
|
tree.write('output.xml')
|
|
return ET.dump(root)
|