Create central type classes

This commit is contained in:
Christian Wolf 2022-12-03 14:29:35 +01:00
parent 15eec712f2
commit 15982a4ac0
4 changed files with 88 additions and 43 deletions

View File

@ -189,12 +189,22 @@ class BatchWorker:
self.l.info('Extracting person data from the preview rounds.') self.l.info('Extracting person data from the preview rounds.')
previewWorker.extractPersonsFromPreview(parsers) previewWorker.extractPersonsFromPreview(parsers)
csvReader = solo_turnier.reader.AllResultReader(self.config.importCSVPath()) csvReader = solo_turnier.reader.CSVResultReader(self.config.importCSVPath())
self.l.info('Importing the total result CSV file %s', self.config.importCSVPath()) self.l.info('Loading the total result CSV file %s', self.config.importCSVPath())
csvData = csvReader.readFile() csvRows = csvReader.extractResult()
self.l.info('CSV file has been read')
# csvReader = solo_turnier.reader.AllResultReader(self.config.importCSVPath())
worker = solo_turnier.worker.DataWorker() # self.l.info('Loading the total result CSV file %s', self.config.importCSVPath())
# csvData = csvReader.readFile()
# self.l.info('CSV file has been read')
# csvExtractor = solo_turnier.worker.CSVExtractor()
# self.l.info('Importing CSV data into internal structures')
# csvRows = csvExtractor.mapCSVImport(csvData)
# worker = solo_turnier.worker.DataWorker()
# self.l.info('Checking for feasible HTML export files in "%s"', self.config.importHtmlPath()) # self.l.info('Checking for feasible HTML export files in "%s"', self.config.importHtmlPath())
# htmlCandidates = locator.findCandidates(self.config.importHtmlPath()) # htmlCandidates = locator.findCandidates(self.config.importHtmlPath())
@ -213,9 +223,6 @@ class BatchWorker:
# self.l.info('Processing the imported data') # self.l.info('Processing the imported data')
# csvExtractor = solo_turnier.worker.CSVExtractor()
# self.l.debug('Importing CSV data into internal structures')
# csvRows = csvExtractor.mapCSVImport(csvData)
# self.l.debug('Combining results from CSV for individual users') # self.l.debug('Combining results from CSV for individual users')
# data = worker.combineRowsByPerson(csvRows) # data = worker.combineRowsByPerson(csvRows)

View File

@ -5,29 +5,12 @@ import os
import logging import logging
import re import re
from pprint import pformat from pprint import pformat
from .types import ResultRow
class ResultRow:
def __init__(self, firstName, lastName, club, id, group, class_, dance, place, placeTo, competitionGroup, competitionClass):
self.firstName = firstName
self.lastName = lastName
self.name = f'{firstName} {lastName}'
self.club = club
self.id = id
self.group = group
self.class_ = class_
self.dance = dance
self.place = place
self.placeTo = placeTo
self.competitionGroup = competitionGroup
self.competitionClass = competitionClass
def __str__(self):
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 CSVResultReader: class CSVResultReader:
def __init__(self, fileName: str): def __init__(self, fileName: str):
self.fileName = fileName self.fileName = fileName
self.l = logging.getLogger('solo_turnier.reader.CSVResultReader')
def readFile(self): def readFile(self):
with open(self.fileName, 'r') as fp: with open(self.fileName, 'r') as fp:
@ -45,30 +28,34 @@ class CSVResultReader:
'data': rows[1:] 'data': rows[1:]
} }
l = logging.getLogger('solo_turnier.reader.all_results') self.l.log(5, 'Imported results from allresults.csv file: %s', (ret))
l.log(5, 'Imported results from allresults.csv file: %s', (ret))
return ret return ret
def extractResult(self, entries = None) -> list[ResultRow]: def extractResult(self, entries = None) -> list[ResultRow]:
if entries is None: if entries is None:
entries = self.readFile() entries = self.readFile()
groupParser = solo_turnier.group.GroupParser()
classParser = solo_turnier.competition_class.CompetitionClassParser()
def __processRow(row): def __processRow(row):
result = ResultRow( result = ResultRow(
competitionGroup=self.__mapGroup(row[2]), competitionGroup=groupParser.parseClass(row[2]),
competitionClass=self.__mapClass(row[3]), competitionClass=classParser.parseClass(row[3]),
dance=row[4], dance=row[4],
id=row[5], id=row[5],
firstName=row[6], lastName=row[7], firstName=row[6], lastName=row[7],
club=row[10], club=row[10],
place=row[12], placeTo=row[13], place=row[12], placeTo=row[13],
group=self.__mapGroup(row[15]), class_=self.__mapClass(row[16]) group=groupParser.parseClass(row[15]),
class_=classParser.parseClass(row[16])
) )
self.l.log(5, 'Found row in CSV: %s', result) self.l.log(5, 'Found row in CSV: %s', result)
return result return result
ret = map(__processRow, imported['data']) ret = list(map(__processRow, entries['data']))
self.l.log(5, 'Extracted rows from CSV data: %s', ret)
return ret return ret
class CSVExtractor: class CSVExtractor:

25
src/solo_turnier/types.py Normal file
View File

@ -0,0 +1,25 @@
class ResultRow:
def __init__(self, firstName, lastName, club, id, group, class_, dance, place, placeTo, competitionGroup, competitionClass):
self.firstName = firstName
self.lastName = lastName
self.name = f'{firstName} {lastName}'
self.club = club
self.id = id
self.group = group
self.class_ = class_
self.dance = dance
self.place = place
self.placeTo = placeTo
self.competitionGroup = competitionGroup
self.competitionClass = competitionClass
def __repr__(self):
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 State4:
def __init__(
self,
csvRows: list[ResultRow]
):
self.csvRows = csvRows

View File

@ -1,6 +1,8 @@
import logging import logging
from pprint import pformat
from solo_turnier import html_parser from solo_turnier import html_parser
from .reader import ResultRow
class HtmlPerson: class HtmlPerson:
def __init__(self, name, id, group): def __init__(self, name, id, group):
@ -10,6 +12,14 @@ class HtmlPerson:
def __repr__(self): def __repr__(self):
return f'{self.name} ({self.id}, {self.group})' return f'{self.name} ({self.id}, {self.group})'
def __eq__(self, o):
if not isinstance(o, HtmlPerson):
return False
return str(self) == str(o)
def __hash__(self):
return str(self).__hash__()
class ResultPerson: class ResultPerson:
def __init__(self, firstName, lastName, club, id = None, group = None): def __init__(self, firstName, lastName, club, id = None, group = None):
@ -98,10 +108,14 @@ class CompetitionResult:
) )
class ImportNotParsableException(Exception):
pass
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.parsers = None
def filterFilesPreview(self, files: list[str]) -> dict[str, html_parser.HtmlParser]: def filterFilesPreview(self, files: list[str]) -> dict[str, html_parser.HtmlParser]:
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.')
@ -110,7 +124,7 @@ class PreviewWorker:
with open(file, 'r') as fp: with open(file, 'r') as fp:
text = fp.read() text = fp.read()
parser = html_parser.HtmlParser(text) parser = html_parser.HtmlParser(text, file)
try: try:
data = parser.guessDataFromHtmlTitle() data = parser.guessDataFromHtmlTitle()
@ -135,7 +149,7 @@ class PreviewWorker:
if data['titles'][0] != 'Wertungsrichter': if data['titles'][0] != 'Wertungsrichter':
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 Exception('Incompatible export file') raise ImportNotParsableException('Incompatible export file')
ids = [] ids = []
names = [] names = []
@ -157,21 +171,34 @@ class PreviewWorker:
group = parser.guessDataFromHtmlTitle(imported['title'])['group'] group = parser.guessDataFromHtmlTitle(imported['title'])['group']
groups = [group for i in indices] groups = [group for i in indices]
ret = [] def __mappingFcn(id, name, group):
for i in range(len(ids)): return HtmlPerson(name, id, group)
ret.append(HtmlPerson(names[i], ids[i], groups[i]))
self.l.log(5, ret) currentPersons = list(map(__mappingFcn, ids, names, groups))
return ret self.l.log(5, 'Extracted persons in preview round: %s', currentPersons)
for p in currentPersons:
current = self.parsers.get(p, [])
current.append(parser)
self.parsers[p] = current
def __resetStructures(self):
self.persons = None
self.parsers = {}
def extractPersonsFromPreview(self, parsers): def extractPersonsFromPreview(self, parsers):
self.__resetStructures()
for file in parsers: for file in parsers:
self.l.debug('Extracting person data from %s', file) self.l.debug('Extracting person data from %s', file)
self.__extractPersonsFromSinglePreview(parsers[file]) self.__extractPersonsFromSinglePreview(parsers[file])
self.persons = self.parsers.keys()
self.l.log(5, 'Extracted person data: %s', pformat(self.parsers))
class DataWorker: class DataWorker:
def __init__(self): def __init__(self):
self.l = logging.getLogger('solo_turnier.worker') self.l = logging.getLogger('solo_turnier.worker.DataWorker')
def combineRowsByPerson(self, rows: list[ResultRow]) -> dict[ResultPerson, list[CompetitionResult]]: def combineRowsByPerson(self, rows: list[ResultRow]) -> dict[ResultPerson, list[CompetitionResult]]:
ret = {} ret = {}
@ -319,4 +346,3 @@ class DataWorker:
return ret return ret