solo-auswertung/src/solo_turnier/output.py

112 lines
3.8 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
sections = ('Kin.', 'Jun.', 'Jug.', 'Sonst')
sectionMap = {
'Kin.': 'Kinder',
'Jun.': 'Junioren',
'Jug.': 'Jugend',
'Sonst': 'Undefiniert'
}
class AbstractOutputter:
def __init__(self):
self.worker = solo_turnier.worker.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()
2023-09-13 14:07:55 +00:00
def _outputGroup(self, group: solo_turnier.group.Group, groupResults: types.TotalGroupResult):
print(f"Einzeltanzwettbewerb der Gruppe {group}")
tableData = [['Tanz'] + groupResults.dances]
participants = list(groupResults.results.keys())
participants.sort(key=lambda x: (x.id, x.name))
2023-09-13 14:07:55 +00:00
for participant in participants:
results = groupResults.results[participant]
def mapResultColumn(result: types.SingleParticipantResult):
def getPlace():
if result.placeTo is None:
return f'{result.place}.'
else:
return f'{result.place}.-{result.placeTo}.'
if result is None:
return ''
place = getPlace()
if not result.finalist:
return f'kein/e Finalist/in\n({place} in {result.nativeClass})'
return f'{place} ({result.nativeClass})'
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.State4):
for idx, group in enumerate(data.groups):
if idx > 0:
print()
self.l.debug('Output for group %s', group)
self._outputGroup(group, data.results[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)