Start using newly defined types

This commit is contained in:
Christian Wolf 2022-12-03 21:46:19 +01:00
parent 27c8f9ac70
commit 240824e808
4 changed files with 91 additions and 135 deletions

View File

@ -181,17 +181,8 @@ class BatchWorker:
htmlCandidatesPreview = locator.findPreviewRoundCandidates(self.config.importHtmlPath()) htmlCandidatesPreview = locator.findPreviewRoundCandidates(self.config.importHtmlPath())
self.l.debug('Found HTML file candidates for preview rounds: %s', htmlCandidatesPreview) self.l.debug('Found HTML file candidates for preview rounds: %s', htmlCandidatesPreview)
previewWorker = solo_turnier.worker.PreviewWorker() worker = solo_turnier.worker.Worker()
self.l.info('Filtering for pure preview rounds.') worker.collectAllData(htmlCandidatesPreview, self.config.importCSVPath())
parsers = previewWorker.filterFilesPreview(htmlCandidatesPreview)
self.l.debug('Remaining files: %s', parsers.keys())
self.l.info('Extracting person data from the preview rounds.')
previewWorker.extractPersonsFromPreview(parsers)
csvReader = solo_turnier.reader.CSVResultReader(self.config.importCSVPath())
self.l.info('Loading the total result CSV file %s', self.config.importCSVPath())
csvRows = csvReader.extractResult()
# csvReader = solo_turnier.reader.AllResultReader(self.config.importCSVPath()) # csvReader = solo_turnier.reader.AllResultReader(self.config.importCSVPath())
# self.l.info('Loading the total result CSV file %s', self.config.importCSVPath()) # self.l.info('Loading the total result CSV file %s', self.config.importCSVPath())

View File

@ -0,0 +1,18 @@
import pytest
import solo_turnier.types as types
def test_HtmlPreviewParticipant_eq():
name = 'Max Mustermann'
id = 123
group = 'Kin'
participant = types.HtmlPreviewParticipant(name, id, group)
l = []
assert participant not in l
l.append(participant)
assert participant in l
assert types.HtmlPreviewParticipant(name, id, group) in l
assert types.HtmlPreviewParticipant('Maxime Musterfrau', id, group) not in l
assert types.HtmlPreviewParticipant(name, 234, group) not in l
assert types.HtmlPreviewParticipant(name, id, 'Jun') not in l

View File

@ -21,19 +21,27 @@ class CSVResultRow:
return f'{self.name} ({self.id}, {self.club}) is in {self.group} {self.class_} and danced the {self.dance} in {self.competitionGroup} {self.competitionClass} getting place {self.place}-{self.placeTo}' return f'{self.name} ({self.id}, {self.club}) is in {self.group} {self.class_} and danced the {self.dance} in {self.competitionGroup} {self.competitionClass} getting place {self.place}-{self.placeTo}'
class HtmlPreviewParticipant: class HtmlPreviewParticipant:
def __init__(self, name, place, finalist): def __init__(self, name, id, participant_group):
self.name = name self.name = name
self.place = place self.id = id
self.finalist = finalist self.group = group.GroupParser().parseClass(participant_group)
def __eq__(self, o):
if type(o) != HtmlPreviewParticipant:
return False
return all(map(lambda x, y: x == y, (self.name, self.id, self.group), (o.name, o.id, o.group)))
def __repr__(self): def __repr__(self):
return f'{self.name} (with place {self.place})' return f'{self.id}: {self.name} ({self.group})'
class HtmlPreviewImport: class HtmlPreviewImport:
def __init__(self, title: str, participants: dict[int, HtmlPreviewParticipant]): def __init__(self, participants: dict[int, HtmlPreviewParticipant]):
self.title = title
self.participants = participants self.participants = participants
def __repr__(self):
return str(self.participants)
class HtmlCompetitionResultRow: class HtmlCompetitionResultRow:
def __init__(self, name, id, dance, group, class_, place, placeTo, finalist): def __init__(self, name, id, dance, group, class_, place, placeTo, finalist):
self.dance = dance self.dance = dance
@ -76,38 +84,6 @@ class HtmlSingleCompetitionResult:
self.placeTo = placeTo self.placeTo = placeTo
self.finalist = finalist self.finalist = finalist
class HtmlCompetitionResultForDance:
def __init__(self, dance: str, results: dict[int, HtmlSingleCompetitionResult]):
self.dance = dance
self.results = results
def get(self, id: int) ->HtmlSingleCompetitionResult:
return self.results[id]
class HtmlCompetitionResultForClass:
def __init__(
self,
class_: competition_class.Class_t,
results: dict[str, HtmlCompetitionResultForDance]
):
self.class_ = class_
self.results = results
def get(self, dance: str) -> HtmlCompetitionResultForDance:
return self.results[dance]
class HtmlCompetitionResultForGroup:
def __init__(
self,
group: group.Group_t,
results: dict[competition_class.Class_t, HtmlCompetitionResultForClass]
):
self.group = group
self.results = results
def get(self, class_: str) -> HtmlCompetitionResultForClass:
return self.results[class_]
class HtmlCompetitionTotalResults: class HtmlCompetitionTotalResults:
def __init__(self): def __init__(self):
self.results = {} self.results = {}
@ -124,7 +100,7 @@ class HtmlCompetitionTotalResults:
l.append(result) l.append(result)
self.results[tup] = l self.results[tup] = l
class State4: class State3:
def __init__( def __init__(
self, self,
csvRows: list[CSVResultRow], csvRows: list[CSVResultRow],

View File

@ -1,8 +1,11 @@
import logging import logging
from pprint import pformat from pprint import pformat
import solo_turnier
from solo_turnier import html_parser from solo_turnier import html_parser
from .reader import ResultRow from .reader import ResultRow
from .types import HtmlCompetitionResultRow as CompetitionResult
from . import types
class HtmlPerson: class HtmlPerson:
def __init__(self, name, id, group): def __init__(self, name, id, group):
@ -59,65 +62,18 @@ class ResultPerson:
text = str(self) text = str(self)
return text.__hash__() return text.__hash__()
class CompetitionResult:
def __init__(self, dance, group, class_, place, placeTo, id, competitionGroup, competitionClass):
self.dance = dance
self.group = group
self.class_ = class_
self.place = place
self.placeTo = placeTo
self.id = int(id)
self.competitionGroup = competitionGroup
self.competitionClass = competitionClass
self.finalist = None
@staticmethod
def extractFromResultRow(row: ResultRow):
return CompetitionResult(
dance=row.dance,
group=row.group,
class_=row.class_,
place=row.place, placeTo=row.placeTo,
id=row.id,
competitionGroup=row.competitionGroup,
competitionClass=row.competitionClass
)
def __repr__(self):
if self.place == self.placeTo:
result = f'{self.place}.'
else:
result = f'{self.place}.-{self.placeTo}.'
if self.finalist == True:
finalist = '[F]'
else:
finalist = ''
return f'Result[{self.id}]({self.group} {self.class_} {self.dance} as {result}{finalist})'
def __eq__(self, o):
if not isinstance(o, CompetitionResult):
return False
return (
self.dance == o.dance and
self.competitionClass == o.competitionClass and
self.competitionGroup == o.competitionGroup and
self.place == o.place and self.placeTo == o.placeTo and
self.id == o.id
)
class ImportNotParsableException(Exception): class ImportNotParsableException(Exception):
pass pass
ParserList_t = dict[str, html_parser.HtmlParser]
class PreviewWorker: class PreviewWorker:
def __init__(self): def __init__(self):
self.l = logging.getLogger('solo_turnier.worker.PreviewWorker') self.l = logging.getLogger('solo_turnier.worker.PreviewWorker')
self.persons = None self.participants = {}
self.parsers = None
def filterFilesPreview(self, files: list[str]) -> dict[str, html_parser.HtmlParser]: def filterFilesPreview(self, files: list[str]) -> ParserList_t:
self.l.debug('Filtering the list of parsers by removing all non preview entries.') self.l.debug('Filtering the list of parsers by removing all non preview entries.')
ret = {} ret = {}
for file in files: for file in files:
@ -151,50 +107,41 @@ class PreviewWorker:
self.l.fatal('Cannot parse the parsed content of the preview file.') self.l.fatal('Cannot parse the parsed content of the preview file.')
raise ImportNotParsableException('Incompatible export file') raise ImportNotParsableException('Incompatible export file')
ids = []
names = []
indices = []
for index, e in enumerate(data['table'][0]):
if e['text'] == '':
continue
indices.append(index)
ids.append(e['text'])
names.append(e['meta'])
groups = []
if data['titles'][-1] == 'Startgruppe': if data['titles'][-1] == 'Startgruppe':
self.l.debug('Combined competition found. Extracting group from table') self.l.debug('Combined competition found. Extracting group from table required.')
groups = [data['table'][-1][idx]['text'] for idx in indices] extractGroup = True
else: else:
self.l.debug('Using group from the title.') self.l.debug('Using group from the title.')
group = parser.guessDataFromHtmlTitle(imported['title'])['group'] group = parser.guessDataFromHtmlTitle(imported['title'])['group']
groups = [group for i in indices] extractGroup = False
def __mappingFcn(id, name, group): for index, e in enumerate(data['table'][0]):
return HtmlPerson(name, id, group) if e['text'] == '':
# Skip empty columns
continue
currentPersons = list(map(__mappingFcn, ids, names, groups)) # Extract data from column
self.l.log(5, 'Extracted persons in preview round: %s', currentPersons) name = e['meta']
id = int(e['text'])
if extractGroup:
group = data['table'][-1][index]['text']
for p in currentPersons: participant = types.HtmlPreviewParticipant(name, id, group)
current = self.parsers.get(p, [])
current.append(parser)
self.parsers[p] = current
def __resetStructures(self): l = self.participants.get(id, [])
self.persons = None self.l.log(5, 'Checking for existence of %s in %s: %s', participant, l, participant in l)
self.parsers = {} if participant not in l:
l.append(participant)
self.participants[id] = l
def extractPersonsFromPreview(self, parsers): def importAllData(self, parsers: ParserList_t) -> types.HtmlPreviewImport:
self.__resetStructures() self.participants = {}
for file in parsers: for file in parsers:
self.l.debug('Extracting person data from %s', file) parser = parsers[file]
self.__extractPersonsFromSinglePreview(parsers[file]) self.__extractPersonsFromSinglePreview(parser)
self.persons = self.parsers.keys() return types.HtmlPreviewImport(self.participants)
self.l.log(5, 'Extracted person data: %s', pformat(self.parsers))
class DataWorker: class DataWorker:
def __init__(self): def __init__(self):
@ -346,3 +293,27 @@ class DataWorker:
return ret return ret
class Worker:
def __init__(self):
self.l = logging.getLogger('solo_turnier.worker.Worker')
def collectAllData(
self,
htmlCandidatesPreview: list[str],
csvFile: str
) -> types.State3:
previewWorker = PreviewWorker()
self.l.info('Filtering for pure preview rounds.')
parsers = previewWorker.filterFilesPreview(htmlCandidatesPreview)
self.l.debug('Remaining files: %s', parsers.keys())
self.l.info('Extracting person data from the preview rounds.')
previewImport = previewWorker.importAllData(parsers)
self.l.debug('Total preview import: %s', previewImport)
csvReader = solo_turnier.reader.CSVResultReader(csvFile)
self.l.info('Loading the total result CSV file %s', csvFile)
csvRows = csvReader.extractResult()
return None