solo-auswertung/src/solo_turnier/output.py

159 lines
5.1 KiB
Python
Raw Normal View History

import logging
from tabulate import tabulate
2023-09-13 14:07:55 +00:00
import pprint
import solo_turnier
2023-09-13 14:07:55 +00:00
from solo_turnier import types
2023-11-19 16:07:20 +00:00
sections = ("Kin.", "Jun.", "Jug.", "Sonst")
sectionMap = {
2023-11-19 16:07:20 +00:00
"Kin.": "Kinder",
"Jun.": "Junioren",
"Jug.": "Jugend",
"Sonst": "Undefiniert",
}
2023-11-19 16:07:20 +00:00
class AbstractOutputter:
def __init__(self):
self.worker = solo_turnier.workers.DataWorker.DataWorker()
self.groups = []
self.dances = []
self.showIds = False
def getRowData(self, person: solo_turnier.worker.ResultPerson, results):
mappedResults = self.worker.mapPersonResultsToDanceList(results, self.dances)
if self.showIds:
2023-11-19 16:07:20 +00:00
name = f"{person.name} ({person.id})"
else:
name = person.name
ret = [name]
for result in mappedResults:
if result is None:
2023-11-19 16:07:20 +00:00
ret.append("")
elif result.finalist == False:
2023-11-19 16:07:20 +00:00
ret.append("x")
elif result.place == result.placeTo:
2023-11-19 16:07:20 +00:00
ret.append(f"{result.place}. ({result.class_})")
else:
2023-11-19 16:07:20 +00:00
ret.append(f"{result.place}.-{result.placeTo}. ({result.class_})")
return ret
def getTabularData(self, data, section):
2023-11-19 16:07:20 +00:00
sortedPersons, self.showIds = self.worker.sortPersonsInGroup(
self.groups[section]
)
tableData = []
for person in sortedPersons:
tableData.append(self.getRowData(person, data[person]))
return tableData
2023-11-19 16:07:20 +00:00
class ConsoleOutputter(AbstractOutputter):
def __init__(self):
super().__init__()
2023-11-19 16:07:20 +00:00
self.l = logging.getLogger("solo_turnier.output.console")
def __outputSection(self, data, section):
tableData = self.getTabularData(data, section)
2023-11-19 16:07:20 +00:00
tableData = [["Name"] + self.dances] + tableData
print(f"Einzeltanzwettbewerb der {sectionMap[section]}")
2023-11-19 16:07:20 +00:00
print(tabulate(tableData, headers="firstrow", tablefmt="fancy_grid"))
print()
2023-11-22 15:33:02 +00:00
def _reshapeRow(
self,
results: list[solo_turnier.types.SingleParticipantResult],
dances: list[str],
) -> list[solo_turnier.types.SingleParticipantResult]:
ret = [None for x in dances]
for result in results:
if result.dance not in dances:
self.l.error(
"Result in unknown dance found in table. This is a bug. (%s)",
result,
)
continue
idx = dances.index(result.dance)
ret[idx] = result
return ret
2023-11-19 16:07:20 +00:00
def _outputGroup(
self,
group: solo_turnier.group.Group,
groupResults: solo_turnier.types.GroupTableData,
2023-11-19 16:07:20 +00:00
):
if group is not None:
print(f"Einzeltanzwettbewerb der Gruppe {group}")
else:
print("Einzeltanzwettbewerbe ohne eindeutige Gruppenzuordnung")
2023-11-19 16:07:20 +00:00
tableData = [["Tanz"] + groupResults.dances]
participants = list(groupResults.resultsInGroup.keys())
2023-09-13 14:07:55 +00:00
participants.sort(key=lambda x: (x.id, x.name))
2023-09-13 14:07:55 +00:00
for participant in participants:
results = groupResults.resultsInGroup[participant]
2023-11-22 15:33:02 +00:00
self.l.log(5, "Results of %s: %s", participant, results)
2023-11-19 16:07:20 +00:00
2023-09-13 14:07:55 +00:00
def mapResultColumn(result: types.SingleParticipantResult):
def getPlace(place, placeTo):
if placeTo is None:
2023-11-19 16:07:20 +00:00
return f"{place}."
2023-09-13 14:07:55 +00:00
else:
2023-11-19 16:07:20 +00:00
return f"{place}.-{placeTo}."
2023-09-13 14:07:55 +00:00
if result is None:
2023-11-19 16:07:20 +00:00
return ""
placeNative = str(result.nativePlace)
place = str(result.place)
lineOne = f"{placeNative}"
lines = [lineOne]
groupCompetition = result.competitionGroup
if isinstance(groupCompetition, solo_turnier.group.CombinedGroup):
lineTwo = f"[{place} in {groupCompetition}]"
lines.append(lineTwo)
2023-09-13 14:07:55 +00:00
if not result.finalist:
2023-11-19 16:07:20 +00:00
lines = ["kein/e Finalist/in"] + lines
return "\n".join(lines)
2023-09-13 14:07:55 +00:00
mappedResults = map(mapResultColumn, results)
2024-03-14 18:07:32 +00:00
participantName = f"{participant.name} ({participant.id})"
2024-03-14 12:08:27 +00:00
if participant.club is not None:
2024-03-14 18:07:32 +00:00
participantName = f"{participantName}, {participant.club}"
2024-03-14 12:08:27 +00:00
tableRow = [f"{participantName}"] + list(mappedResults)
2023-09-13 14:07:55 +00:00
tableData.append(tableRow)
2023-11-19 16:07:20 +00:00
self.l.log(5, "table data: %s", pprint.pformat(tableData))
print(tabulate(tableData, headers="firstrow", tablefmt="fancy_grid"))
2023-09-13 14:07:55 +00:00
def output(self, data: types.Stage5):
for idx, group in enumerate(data.resultsPerGroup):
2023-09-13 14:07:55 +00:00
if idx > 0:
print()
2023-11-19 16:07:20 +00:00
self.l.debug("Output for group %s", group)
2023-09-13 14:07:55 +00:00
self._outputGroup(group, data.resultsPerGroup[group])
2023-09-13 14:07:55 +00:00
# self.groups = self.worker.collectPersonsInGroups(data)
# self.dances = self.worker.getAllDancesInCompetitions(data)
# for section in sections:
# if len(self.groups[section]) > 0:
# self.__outputSection(data, section)