149 lines
4.7 KiB
Python
149 lines
4.7 KiB
Python
import logging
|
|
from tabulate import tabulate
|
|
import pprint
|
|
|
|
import solo_turnier
|
|
from solo_turnier import types
|
|
|
|
sections = ("Kin.", "Jun.", "Jug.", "Sonst")
|
|
sectionMap = {
|
|
"Kin.": "Kinder",
|
|
"Jun.": "Junioren",
|
|
"Jug.": "Jugend",
|
|
"Sonst": "Undefiniert",
|
|
}
|
|
|
|
|
|
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:
|
|
name = f"{person.name} ({person.id})"
|
|
else:
|
|
name = person.name
|
|
|
|
ret = [name]
|
|
for result in mappedResults:
|
|
if result is None:
|
|
ret.append("")
|
|
elif result.finalist == False:
|
|
ret.append("x")
|
|
elif result.place == result.placeTo:
|
|
ret.append(f"{result.place}. ({result.class_})")
|
|
else:
|
|
ret.append(f"{result.place}.-{result.placeTo}. ({result.class_})")
|
|
return ret
|
|
|
|
def getTabularData(self, data, section):
|
|
sortedPersons, self.showIds = self.worker.sortPersonsInGroup(
|
|
self.groups[section]
|
|
)
|
|
|
|
tableData = []
|
|
for person in sortedPersons:
|
|
tableData.append(self.getRowData(person, data[person]))
|
|
|
|
return tableData
|
|
|
|
|
|
class ConsoleOutputter(AbstractOutputter):
|
|
def __init__(self):
|
|
super().__init__()
|
|
self.l = logging.getLogger("solo_turnier.output.console")
|
|
|
|
def __outputSection(self, data, section):
|
|
tableData = self.getTabularData(data, section)
|
|
tableData = [["Name"] + self.dances] + tableData
|
|
|
|
print(f"Einzeltanzwettbewerb der {sectionMap[section]}")
|
|
print(tabulate(tableData, headers="firstrow", tablefmt="fancy_grid"))
|
|
print()
|
|
|
|
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
|
|
|
|
def _outputGroup(
|
|
self,
|
|
group: solo_turnier.group.Group,
|
|
groupResults: solo_turnier.types.GroupTableData,
|
|
):
|
|
if group is not None:
|
|
print(f"Einzeltanzwettbewerb der Gruppe {group}")
|
|
else:
|
|
print("Einzeltanzwettbewerbe ohne eindeutige Gruppenzuordnung")
|
|
|
|
tableData = [["Tanz"] + groupResults.dances]
|
|
participants = list(groupResults.resultsInGroup.keys())
|
|
participants.sort(key=lambda x: (x.id, x.name))
|
|
|
|
for participant in participants:
|
|
results = groupResults.resultsInGroup[participant]
|
|
|
|
self.l.log(5, "Results of %s: %s", participant, results)
|
|
|
|
def mapResultColumn(result: types.SingleParticipantResult):
|
|
def getPlace(place, placeTo):
|
|
if placeTo is None:
|
|
return f"{place}."
|
|
else:
|
|
return f"{place}.-{placeTo}."
|
|
|
|
if result is None:
|
|
return ""
|
|
|
|
placeNative = str(result.nativePlace)
|
|
place = str(result.place)
|
|
lineOne = f"{placeNative}"
|
|
# lineTwo = f"[{place} in {result.competitionClass}]"
|
|
|
|
lines = [lineOne]
|
|
if not result.finalist:
|
|
lines = ["kein/e Finalist/in"] + lines
|
|
|
|
return "\n".join(lines)
|
|
|
|
mappedResults = map(mapResultColumn, results)
|
|
tableRow = [f"{participant.name} ({participant.id})"] + list(mappedResults)
|
|
tableData.append(tableRow)
|
|
|
|
self.l.log(5, "table data: %s", pprint.pformat(tableData))
|
|
print(tabulate(tableData, headers="firstrow", tablefmt="fancy_grid"))
|
|
|
|
def output(self, data: types.Stage5):
|
|
for idx, group in enumerate(data.resultsPerGroup):
|
|
if idx > 0:
|
|
print()
|
|
|
|
self.l.debug("Output for group %s", group)
|
|
|
|
self._outputGroup(group, data.resultsPerGroup[group])
|
|
# 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)
|