Starta å refaktorere metodene som importerer ttime filer

This commit is contained in:
Trygve 2023-11-02 22:13:41 +01:00
parent bc22d039d6
commit a4c1fe51ce
2 changed files with 44 additions and 61 deletions

View File

@ -1,4 +1,5 @@
import datetime
import re
import xml.etree.ElementTree as etree
class Event:
@ -16,6 +17,9 @@ class Event:
self.card_dumps = card_dumps
self.fees = fees
def __repr__(self):
return(f'id({self.id}), name({self.name}), organiser({self.organiser}), courses({self.courses}), o_classes({self.o_classes}), runners({self.runners})')
def add_course(self, *args):
for n in args:
self.courses.append(n)
@ -40,7 +44,7 @@ class Event:
def get_runner(self, name):
pass
def read_xml_entries(self, xml_file):
self.add_runners(*runners_from_xml_entries(xml_file))
self.add_fees(*fees_from_xml_entries(xml_file))
@ -75,21 +79,12 @@ class Event:
def read_ttime_cnf(self, ttime_file):
self.add_course(*courses_from_ttime_conf(ttime_file))
if isinstance(ttime_file, io.TextIOBase):
ttime_file.seek(0)
self.add_o_class(*classes_from_ttime_conf(ttime_file, self.courses))
def read_ttime_db(self, ttime_file):
if type(ttime_file) == str:
f_list = ttime_file.splitlines()
elif isinstance(ttime_file, io.TextIOBase):
f_list = csv.reader(ttime_file, delimiter=';',)
runnerarray = []
for row in f_list:
if len(row) == 0 or row[1] == '':
continue
runnerarray.append(Runner.from_string(row, self.o_classes))
self.runners = runnerarray
with open(ttime_file) as f:
data = f.readlines()
self.runners = [Runner.from_ttime_string(i) for i in data if i]
def read_mtr_file(self, mtr_file):
self.card_dumps = CardDump.list_from_mtr_f(mtr_file)
@ -183,7 +178,7 @@ class Event:
# The runner object stores all the data specific to a runner.
class Runner:
def __init__(self, runner_id: int, first: str, last: str, club=None, club_id=None,
country=None, card=None, o_class_str=None, o_class=None,
country=None, card_id=None, o_class_str=None, o_class=None,
fork=0, start_time=None, fee_id=None, fee=None):
self.id = runner_id
self.first = first
@ -191,46 +186,40 @@ class Runner:
self.club = club
self.club_id = club_id
self.country = country
self.card = card
self.card_id = card_id
self.o_class = o_class
self.fork = fork
self.start_time = start_time
self.fee_id = fee_id
def from_string(tt_line, o_classes):
def __repr__(self):
return(f'name({self.fullname()})')
def from_ttime_string(ttime_db_line):
# Reads 1 line of a ttime db
#https://web.archive.org/web/20191229124046/http://wiki.ttime.no/index.php/Developer
eventorid = tt_line[0]
runner_data = ttime_db_line.split(';')
eventorid = runner_data[0]
country = ''
name = tt_line[2].split(',')
name = runner_data[2].split(',')
try:
first = name[1].strip()
except:
first = ''
last = name[0]
try:
club = tt_line[4]
club = runner_data[4]
except:
club = "None"
club = ''
try:
card = int(tt_line[6])
card = int(runner_data[6])
except:
card = 0
runner_o_class = None
try:
raw_class_str = tt_line[3]
o_class = runner_data[3]
except:
# VELDIG MIDLERTIDIG
runner_o_class = None
else:
if raw_class_str != '':
for i in o_classes:
if i.name == raw_class_str:
runner_o_class = i
break
else:
runner_o_class = None
# TODO: Gjør sånn at den lager nye o klasser om den ikke finnes fra før
options = tt_line[5].split(',')
o_class = ''
options = runner_data[5].split(',')
try:
club_id = options[options.index('A')+3]
except:
@ -240,8 +229,7 @@ class Runner:
except:
start_time = None
return Runner(eventorid, first, last, club=club, club_id=club_id,
country=country, card=card, o_class_str=raw_class_str,
o_class=runner_o_class, start_time=start_time)
country=country, card_id=card, o_class=o_class, start_time=start_time)
def fullname(self):
return '{} {}'.format(self.first, self.last)
@ -381,18 +369,13 @@ class CardDump:
return(CardDump(card, controls, splits, read_time, s_time, f_time))
def list_from_mtr_f(mtr_f):
if type(mtr_f) == str:
f_list = mtr_f.splitlines()
rows = []
def list_from_mtr_f(mtr_file):
with open(mtr_file) as f:
data = [i.replace('"','').split(',') for i in f.readlines()]
print(data)
cards = []
# hver rad er brikkenummer med tilhørende info
csvreader = csv.reader(f_list)
for row in csvreader:
if len(row) == 0:
continue
rows.append(row)
for row in data:
controls = []
splits = []
# postkodene kommer på oddetall fra og med den 11. De blir hevet inn i controls
@ -496,13 +479,10 @@ class Fee:
# TODO: Take string instead of file.
def courses_from_ttime_conf(ttime_file):
if type(ttime_file) == str:
conf = ttime_file.splitlines()
elif isinstance(ttime_file, io.TextIOBase):
conf = ttime_file.readlines()
with open(ttime_file, 'r') as f:
data = f.readlines()
courses = []
for line in conf:
for line in data:
if '-codes' in line:
code_list = re.search(r'(?<=\")(.*?)(?=\")', line).group().split(';')
loops = 0
@ -514,12 +494,10 @@ def courses_from_ttime_conf(ttime_file):
return courses
def classes_from_ttime_conf(ttime_file, courses):
if type(ttime_file) == str:
conf = ttime_file.splitlines()
elif isinstance(ttime_file, io.TextIOBase):
conf = ttime_file.readlines()
with open(ttime_file, 'r') as f:
data = f.readlines()
o_classes = []
for line in conf:
for line in data:
if '-courses' in line:
raw_courselist = re.search(r'(?<=\")(.*?)(?=\")', line).group().split(';')
loops = 0

View File

@ -1,9 +1,14 @@
import sys
sys.path.insert(0, '../../')
sys.path.insert(0, '../../otime')
import otime
def main():
pass
event = otime.Event(0, 'TEEEST', start_time=None, end_time=None,organiser='Tygbe')
event.read_ttime_cnf('tt.cnf')
event.read_ttime_db('db.csv')
event.read_mtr_file('mtr.csv')
print(event)
print(event.card_dumps[1])
if __name__ == '__main__':
main()