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 datetime
import re
import xml.etree.ElementTree as etree import xml.etree.ElementTree as etree
class Event: class Event:
@ -16,6 +17,9 @@ class Event:
self.card_dumps = card_dumps self.card_dumps = card_dumps
self.fees = fees 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): def add_course(self, *args):
for n in args: for n in args:
self.courses.append(n) self.courses.append(n)
@ -75,21 +79,12 @@ class Event:
def read_ttime_cnf(self, ttime_file): def read_ttime_cnf(self, ttime_file):
self.add_course(*courses_from_ttime_conf(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)) self.add_o_class(*classes_from_ttime_conf(ttime_file, self.courses))
def read_ttime_db(self, ttime_file): def read_ttime_db(self, ttime_file):
if type(ttime_file) == str: with open(ttime_file) as f:
f_list = ttime_file.splitlines() data = f.readlines()
elif isinstance(ttime_file, io.TextIOBase): self.runners = [Runner.from_ttime_string(i) for i in data if i]
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
def read_mtr_file(self, mtr_file): def read_mtr_file(self, mtr_file):
self.card_dumps = CardDump.list_from_mtr_f(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. # The runner object stores all the data specific to a runner.
class Runner: class Runner:
def __init__(self, runner_id: int, first: str, last: str, club=None, club_id=None, 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): fork=0, start_time=None, fee_id=None, fee=None):
self.id = runner_id self.id = runner_id
self.first = first self.first = first
@ -191,46 +186,40 @@ class Runner:
self.club = club self.club = club
self.club_id = club_id self.club_id = club_id
self.country = country self.country = country
self.card = card self.card_id = card_id
self.o_class = o_class self.o_class = o_class
self.fork = fork self.fork = fork
self.start_time = start_time self.start_time = start_time
self.fee_id = fee_id 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 #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 = '' country = ''
name = tt_line[2].split(',') name = runner_data[2].split(',')
try: try:
first = name[1].strip() first = name[1].strip()
except: except:
first = '' first = ''
last = name[0] last = name[0]
try: try:
club = tt_line[4] club = runner_data[4]
except: except:
club = "None" club = ''
try: try:
card = int(tt_line[6]) card = int(runner_data[6])
except: except:
card = 0 card = 0
runner_o_class = None
try: try:
raw_class_str = tt_line[3] o_class = runner_data[3]
except: except:
# VELDIG MIDLERTIDIG o_class = ''
runner_o_class = None options = runner_data[5].split(',')
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(',')
try: try:
club_id = options[options.index('A')+3] club_id = options[options.index('A')+3]
except: except:
@ -240,8 +229,7 @@ class Runner:
except: except:
start_time = None start_time = None
return Runner(eventorid, first, last, club=club, club_id=club_id, return Runner(eventorid, first, last, club=club, club_id=club_id,
country=country, card=card, o_class_str=raw_class_str, country=country, card_id=card, o_class=o_class, start_time=start_time)
o_class=runner_o_class, start_time=start_time)
def fullname(self): def fullname(self):
return '{} {}'.format(self.first, self.last) return '{} {}'.format(self.first, self.last)
@ -381,18 +369,13 @@ class CardDump:
return(CardDump(card, controls, splits, read_time, s_time, f_time)) return(CardDump(card, controls, splits, read_time, s_time, f_time))
def list_from_mtr_f(mtr_f): def list_from_mtr_f(mtr_file):
if type(mtr_f) == str: with open(mtr_file) as f:
f_list = mtr_f.splitlines() data = [i.replace('"','').split(',') for i in f.readlines()]
print(data)
rows = []
cards = [] cards = []
# hver rad er brikkenummer med tilhørende info # hver rad er brikkenummer med tilhørende info
csvreader = csv.reader(f_list) for row in data:
for row in csvreader:
if len(row) == 0:
continue
rows.append(row)
controls = [] controls = []
splits = [] splits = []
# postkodene kommer på oddetall fra og med den 11. De blir hevet inn i controls # 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. # TODO: Take string instead of file.
def courses_from_ttime_conf(ttime_file): def courses_from_ttime_conf(ttime_file):
if type(ttime_file) == str: with open(ttime_file, 'r') as f:
conf = ttime_file.splitlines() data = f.readlines()
elif isinstance(ttime_file, io.TextIOBase):
conf = ttime_file.readlines()
courses = [] courses = []
for line in conf: for line in data:
if '-codes' in line: if '-codes' in line:
code_list = re.search(r'(?<=\")(.*?)(?=\")', line).group().split(';') code_list = re.search(r'(?<=\")(.*?)(?=\")', line).group().split(';')
loops = 0 loops = 0
@ -514,12 +494,10 @@ def courses_from_ttime_conf(ttime_file):
return courses return courses
def classes_from_ttime_conf(ttime_file, courses): def classes_from_ttime_conf(ttime_file, courses):
if type(ttime_file) == str: with open(ttime_file, 'r') as f:
conf = ttime_file.splitlines() data = f.readlines()
elif isinstance(ttime_file, io.TextIOBase):
conf = ttime_file.readlines()
o_classes = [] o_classes = []
for line in conf: for line in data:
if '-courses' in line: if '-courses' in line:
raw_courselist = re.search(r'(?<=\")(.*?)(?=\")', line).group().split(';') raw_courselist = re.search(r'(?<=\")(.*?)(?=\")', line).group().split(';')
loops = 0 loops = 0

View File

@ -1,9 +1,14 @@
import sys import sys
sys.path.insert(0, '../../') sys.path.insert(0, '../../otime')
import otime import otime
def main(): 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__': if __name__ == '__main__':
main() main()