Dropping CSV parsing methods
This commit is contained in:
parent
e081769b6c
commit
04b43def9d
@ -3,7 +3,6 @@ from . import group
|
|||||||
from . import types
|
from . import types
|
||||||
|
|
||||||
from . import cli
|
from . import cli
|
||||||
from . import reader
|
|
||||||
from . import participant
|
from . import participant
|
||||||
|
|
||||||
from . import html_locator
|
from . import html_locator
|
||||||
|
@ -1,96 +0,0 @@
|
|||||||
import solo_turnier
|
|
||||||
import csv
|
|
||||||
import os
|
|
||||||
import logging
|
|
||||||
import re
|
|
||||||
from pprint import pformat
|
|
||||||
from .types import CSVResultRow as 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:
|
|
||||||
dialect = csv.Sniffer().sniff(fp.read(1024))
|
|
||||||
fp.seek(0)
|
|
||||||
|
|
||||||
csvReader = csv.reader(fp, dialect)
|
|
||||||
|
|
||||||
rows = []
|
|
||||||
for row in csvReader:
|
|
||||||
rows.append(row)
|
|
||||||
|
|
||||||
ret = {"header": rows[0], "data": rows[1:]}
|
|
||||||
|
|
||||||
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=groupParser.parseGroup(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=groupParser.parseGroup(row[15]),
|
|
||||||
class_=classParser.parseClass(row[16]),
|
|
||||||
)
|
|
||||||
self.l.log(5, "Found row in CSV: %s", result)
|
|
||||||
return result
|
|
||||||
|
|
||||||
ret = list(map(__processRow, entries["data"]))
|
|
||||||
|
|
||||||
self.l.log(5, "Extracted rows from CSV data: %s", ret)
|
|
||||||
return ret
|
|
||||||
|
|
||||||
|
|
||||||
class CSVExtractor:
|
|
||||||
def __init__(self):
|
|
||||||
self.l = logging.getLogger("solo_turnier.worker")
|
|
||||||
self.__groupMaps = {"Kinder": "Kin.", "Junioren": "Jun.", "Jugend": "Jug."}
|
|
||||||
self.__classMaps = {"Newcomer": "Newc.", "Beginner": "Beg.", "Advanced": "Adv."}
|
|
||||||
|
|
||||||
def __mapGroup(self, group):
|
|
||||||
return self.__groupMaps.get(group, group)
|
|
||||||
|
|
||||||
def __mapClass(self, class_):
|
|
||||||
return self.__classMaps.get(class_, class_)
|
|
||||||
|
|
||||||
def mapCSVImport(self, imported) -> list[ResultRow]:
|
|
||||||
ret = []
|
|
||||||
|
|
||||||
def __processRow(row):
|
|
||||||
result = ResultRow(
|
|
||||||
competitionGroup=self.__mapGroup(row[2]),
|
|
||||||
competitionClass=self.__mapClass(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]),
|
|
||||||
)
|
|
||||||
ret.append(result)
|
|
||||||
self.l.log(5, "Found row in CSV: %s", result)
|
|
||||||
|
|
||||||
for row in imported["data"]:
|
|
||||||
__processRow(row)
|
|
||||||
|
|
||||||
return ret
|
|
@ -1,10 +1,5 @@
|
|||||||
from .place import Place
|
from .place import Place
|
||||||
|
|
||||||
from .. import group
|
|
||||||
from .. import competition_class
|
|
||||||
|
|
||||||
|
|
||||||
from .csvResultRow import CSVResultRow
|
|
||||||
from .htmlPreviewParticipant import HtmlPreviewParticipant
|
from .htmlPreviewParticipant import HtmlPreviewParticipant
|
||||||
from .htmlParticipant import HtmlParticipant
|
from .htmlParticipant import HtmlParticipant
|
||||||
from .htmlPreviewImport import HtmlPreviewImport
|
from .htmlPreviewImport import HtmlPreviewImport
|
||||||
|
@ -1,30 +0,0 @@
|
|||||||
class CSVResultRow:
|
|
||||||
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}"
|
|
@ -1,17 +1,3 @@
|
|||||||
from .reader import ResultRow
|
|
||||||
|
|
||||||
# import logging
|
|
||||||
# from pprint import pformat
|
|
||||||
|
|
||||||
# import re
|
|
||||||
|
|
||||||
# import solo_turnier
|
|
||||||
|
|
||||||
# from .types import HtmlCompetitionResultRow as CompetitionResult
|
|
||||||
# from . import types
|
|
||||||
# from . import competition_class
|
|
||||||
|
|
||||||
|
|
||||||
class HtmlPerson:
|
class HtmlPerson:
|
||||||
def __init__(self, name, id, group):
|
def __init__(self, name, id, group):
|
||||||
self.name = name
|
self.name = name
|
||||||
@ -39,12 +25,6 @@ class ResultPerson:
|
|||||||
self.id = id
|
self.id = id
|
||||||
self.group = group
|
self.group = group
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def extractFromResultRow(row: ResultRow):
|
|
||||||
return ResultPerson(
|
|
||||||
firstName=row.firstName, lastName=row.lastName, club=row.club
|
|
||||||
)
|
|
||||||
|
|
||||||
def __eq__(self, o):
|
def __eq__(self, o):
|
||||||
if not isinstance(o, ResultPerson):
|
if not isinstance(o, ResultPerson):
|
||||||
return False
|
return False
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
from ..reader import ResultRow
|
|
||||||
from ..worker import ResultPerson
|
from ..worker import ResultPerson
|
||||||
from ..types import HtmlCompetitionResultRow as CompetitionResult
|
from ..types import HtmlCompetitionResultRow as CompetitionResult
|
||||||
from solo_turnier import html_parser
|
from solo_turnier import html_parser
|
||||||
@ -9,148 +8,6 @@ class DataWorker:
|
|||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.l = logging.getLogger("solo_turnier.worker.DataWorker")
|
self.l = logging.getLogger("solo_turnier.worker.DataWorker")
|
||||||
|
|
||||||
def combineRowsByPerson(
|
|
||||||
self, rows: list[ResultRow]
|
|
||||||
) -> dict[ResultPerson, list[CompetitionResult]]:
|
|
||||||
ret = {}
|
|
||||||
for row in rows:
|
|
||||||
result = CompetitionResult.extractFromResultRow(row)
|
|
||||||
|
|
||||||
if result.place == "-" or result.placeTo == "-":
|
|
||||||
continue
|
|
||||||
|
|
||||||
person = ResultPerson.extractFromResultRow(row)
|
|
||||||
if person not in ret:
|
|
||||||
ret[person] = []
|
|
||||||
ret[person].append(result)
|
|
||||||
return ret
|
|
||||||
|
|
||||||
def checkUniqueIds(self, data: dict[ResultPerson, list[CompetitionResult]]) -> bool:
|
|
||||||
unique = True
|
|
||||||
for person in data:
|
|
||||||
ids = set([c.id for c in data[person]])
|
|
||||||
if len(ids) == 1:
|
|
||||||
person.id = list(ids)[0]
|
|
||||||
else:
|
|
||||||
unique = False
|
|
||||||
|
|
||||||
return unique
|
|
||||||
|
|
||||||
"""
|
|
||||||
Return a tuple
|
|
||||||
The first one is True, if all persons could be unambiguously identified a group
|
|
||||||
The second one is True if there was the need to override a group but it was possible to extract from other data
|
|
||||||
The second one can be seen as a warning
|
|
||||||
"""
|
|
||||||
|
|
||||||
def consolidateGroups(
|
|
||||||
self, data: dict[ResultPerson, list[CompetitionResult]]
|
|
||||||
) -> tuple[bool, bool]:
|
|
||||||
ambiguous = False
|
|
||||||
warnChange = False
|
|
||||||
|
|
||||||
unambiguousGroups = set(["Kin.", "Jun.", "Jug."])
|
|
||||||
combinations = set(["Kin./Jun.", "Jun./Jug."])
|
|
||||||
|
|
||||||
for person in data:
|
|
||||||
groupsRaw = set([c.group for c in data[person]])
|
|
||||||
|
|
||||||
unknown = groupsRaw.difference(unambiguousGroups).difference(combinations)
|
|
||||||
if len(unknown) > 0:
|
|
||||||
raise Exception(
|
|
||||||
f"There were unknown groups found for {person}: {unknown}"
|
|
||||||
)
|
|
||||||
|
|
||||||
numUnambiguousGroups = len(groupsRaw.intersection(unambiguousGroups))
|
|
||||||
|
|
||||||
if numUnambiguousGroups == 0:
|
|
||||||
if len(groupsRaw) == 2:
|
|
||||||
warnChange = True
|
|
||||||
person.group = "Jun."
|
|
||||||
else:
|
|
||||||
ambiguous = True
|
|
||||||
if len(groupsRaw) == 1:
|
|
||||||
person.group = list(groupsRaw)[0]
|
|
||||||
|
|
||||||
elif numUnambiguousGroups == 1:
|
|
||||||
if len(groupsRaw.intersection(combinations)) > 0:
|
|
||||||
warnChange = True
|
|
||||||
|
|
||||||
person.group = list(groupsRaw.intersection(unambiguousGroups))[0]
|
|
||||||
|
|
||||||
else:
|
|
||||||
raise Exception(f"{person} cannot have different groups.")
|
|
||||||
|
|
||||||
return (not ambiguous, warnChange)
|
|
||||||
|
|
||||||
def _createHtmlLUT(self, htmlImports: list[html_parser.HtmlImport]):
|
|
||||||
ret = {}
|
|
||||||
parser = html_parser.HtmlParser("")
|
|
||||||
for imp in htmlImports:
|
|
||||||
parsed = parser.guessDataFromHtmlTitle(imp.title)
|
|
||||||
key = (parsed["group"], parsed["class_"], parsed["dance"])
|
|
||||||
ret[key] = imp
|
|
||||||
self.l.debug("LUT[%s] = %s", key, imp)
|
|
||||||
self.l.debug("LUT completed")
|
|
||||||
return ret
|
|
||||||
|
|
||||||
def mergeHtmlData(
|
|
||||||
self,
|
|
||||||
data: dict[ResultPerson, list[CompetitionResult]],
|
|
||||||
htmlImports: list[html_parser.HtmlImport],
|
|
||||||
):
|
|
||||||
lut = self._createHtmlLUT(htmlImports)
|
|
||||||
|
|
||||||
for person in data:
|
|
||||||
for competition in data[person]:
|
|
||||||
key = (
|
|
||||||
competition.competitionGroup,
|
|
||||||
competition.competitionClass,
|
|
||||||
competition.dance,
|
|
||||||
)
|
|
||||||
htmlImport = lut[key]
|
|
||||||
participant = htmlImport.participants[str(competition.id)]
|
|
||||||
if participant.name != person.name:
|
|
||||||
self.l.error(
|
|
||||||
f"Names for {person} and participant in HTML import ({participant}) do not match. Please check carefully."
|
|
||||||
)
|
|
||||||
competition.finalist = participant.finalist
|
|
||||||
|
|
||||||
def getAllDancesInCompetitions(
|
|
||||||
self, data: dict[ResultPerson, list[CompetitionResult]]
|
|
||||||
) -> list[str]:
|
|
||||||
allDances = [
|
|
||||||
"Samba",
|
|
||||||
"Cha Cha",
|
|
||||||
"Rumba",
|
|
||||||
"Paso Doble",
|
|
||||||
"Jive",
|
|
||||||
"Langs. Walzer",
|
|
||||||
"Tango",
|
|
||||||
"Wiener Walzer",
|
|
||||||
"Slowfox",
|
|
||||||
"Quickstep",
|
|
||||||
]
|
|
||||||
dancesPresent = {d: False for d in allDances}
|
|
||||||
|
|
||||||
for person in data:
|
|
||||||
for competition in data[person]:
|
|
||||||
dancesPresent[competition.dance] = True
|
|
||||||
|
|
||||||
return [d for d in allDances if dancesPresent[d]]
|
|
||||||
|
|
||||||
def collectPersonsInGroups(
|
|
||||||
self, data: dict[ResultPerson, list[CompetitionResult]]
|
|
||||||
) -> list[tuple[str, list[ResultPerson]]]:
|
|
||||||
groups = {
|
|
||||||
"Kin.": [p for p in data.keys() if p.group == "Kin."],
|
|
||||||
"Jun.": [p for p in data.keys() if p.group == "Jun."],
|
|
||||||
"Jug.": [p for p in data.keys() if p.group == "Jug."],
|
|
||||||
}
|
|
||||||
found = groups["Kin."] + groups["Jun."] + groups["Jug."]
|
|
||||||
groups["Sonst"] = [p for p in data.keys() if p not in found]
|
|
||||||
return groups
|
|
||||||
|
|
||||||
def sortPersonsInGroup(self, persons: list[ResultPerson]) -> list[ResultPerson]:
|
def sortPersonsInGroup(self, persons: list[ResultPerson]) -> list[ResultPerson]:
|
||||||
ids = [p.id for p in persons]
|
ids = [p.id for p in persons]
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user