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.')
previewWorker.extractPersonsFromPreview(parsers)
csvReader = solo_turnier.reader.AllResultReader(self.config.importCSVPath())
self.l.info('Importing the total result CSV file %s', self.config.importCSVPath())
csvData = csvReader.readFile()
self.l.info('CSV file has been read')
worker = solo_turnier.worker.DataWorker()
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())
# 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())
# htmlCandidates = locator.findCandidates(self.config.importHtmlPath())
@ -213,9 +223,6 @@ class BatchWorker:
# 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')
# data = worker.combineRowsByPerson(csvRows)

View File

@ -5,29 +5,12 @@ import os
import logging
import re
from pprint import pformat
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}'
from .types import ResultRow
class CSVResultReader:
def __init__(self, fileName: str):
self.fileName = fileName
self.l = logging.getLogger('solo_turnier.reader.CSVResultReader')
def readFile(self):
with open(self.fileName, 'r') as fp:
@ -45,30 +28,34 @@ class CSVResultReader:
'data': rows[1:]
}
l = logging.getLogger('solo_turnier.reader.all_results')
l.log(5, 'Imported results from allresults.csv file: %s', (ret))
self.l.log(5, 'Imported results from allresults.csv file: %s', (ret))
return ret
def extractResult(self, entries = None) -> list[ResultRow]:
if entries is None:
entries = self.readFile()
groupParser = solo_turnier.group.GroupParser()
classParser = solo_turnier.competition_class.CompetitionClassParser()
def __processRow(row):
result = ResultRow(
competitionGroup=self.__mapGroup(row[2]),
competitionClass=self.__mapClass(row[3]),
competitionGroup=groupParser.parseClass(row[2]),
competitionClass=classParser.parseClass(row[3]),
dance=row[4],
id=row[5],
firstName=row[6], lastName=row[7],
club=row[10],
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)
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
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
from pprint import pformat
from solo_turnier import html_parser
from .reader import ResultRow
class HtmlPerson:
def __init__(self, name, id, group):
@ -10,6 +12,14 @@ class HtmlPerson:
def __repr__(self):
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:
def __init__(self, firstName, lastName, club, id = None, group = None):
@ -98,10 +108,14 @@ class CompetitionResult:
)
class ImportNotParsableException(Exception):
pass
class PreviewWorker:
def __init__(self):
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]:
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:
text = fp.read()
parser = html_parser.HtmlParser(text)
parser = html_parser.HtmlParser(text, file)
try:
data = parser.guessDataFromHtmlTitle()
@ -135,7 +149,7 @@ class PreviewWorker:
if data['titles'][0] != 'Wertungsrichter':
self.l.fatal('Cannot parse the parsed content of the preview file.')
raise Exception('Incompatible export file')
raise ImportNotParsableException('Incompatible export file')
ids = []
names = []
@ -157,21 +171,34 @@ class PreviewWorker:
group = parser.guessDataFromHtmlTitle(imported['title'])['group']
groups = [group for i in indices]
ret = []
for i in range(len(ids)):
ret.append(HtmlPerson(names[i], ids[i], groups[i]))
def __mappingFcn(id, name, group):
return HtmlPerson(name, id, group)
self.l.log(5, ret)
return ret
currentPersons = list(map(__mappingFcn, ids, names, groups))
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):
self.__resetStructures()
for file in parsers:
self.l.debug('Extracting person data from %s', file)
self.__extractPersonsFromSinglePreview(parsers[file])
self.persons = self.parsers.keys()
self.l.log(5, 'Extracted person data: %s', pformat(self.parsers))
class DataWorker:
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]]:
ret = {}
@ -319,4 +346,3 @@ class DataWorker:
return ret