forked from esitarski/CrossMgr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCrossResultsExport.py
113 lines (90 loc) · 2.96 KB
/
CrossResultsExport.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import csv
import Utils
import Model
from GetResults import GetResults
from ReadSignOnSheet import SyncExcelLink
def formatTimeGap( secs, forceHours=False ):
return Utils.formatTimeGap(
secs,
forceHours=forceHours,
separateWithQuotes=False,
)
CrossResultsFields = (
('Place', 'pos'),
('Time', 'lastTime'),
('Last Name', 'LastName'),
('First Name', 'FirstName'),
('Team', 'Team'),
('License', 'License'),
)
lenCrossResultsFields = len(CrossResultsFields)
def CrossResultsExport( fname ):
race = Model.race
if not race:
return
SyncExcelLink( race )
hasField = [False] * len(CrossResultsFields)
hasField[0] = True
hasField[1] = True
# Check for what fields are actually filled in.
publishCategories = race.getCategories( startWaveOnly = False, uploadOnly = True )
for cat in publishCategories:
results = GetResults( cat )
if not results:
continue
for rr in results:
for i, (field, cmgrField) in enumerate(CrossResultsFields):
if getattr(rr, cmgrField, None):
hasField[i] = True
if not hasField[2]:
return False, _('"LastName" must be linked to a column in the Excel sheetl')
# Filter the fields by what exists in the data.
crossResultsFields = [CrossResultsFields[i][0] for i in range(len(hasField)) if hasField[i]]
year, month, day = race.date.split( '-' )
def toInt( n ):
try:
return int(n.split()[0])
except Exception:
return n
maxLaps = 1
for cat in publishCategories:
results = GetResults( cat )
if not results:
continue
maxLaps = max( maxLaps, max(rr.laps for rr in results) )
if maxLaps == 1 or maxLaps > 99:
maxLaps = 0
lapHeaders = ['lap'] * maxLaps
with open(fname, 'w', encoding='utf8', newline='') as csvFile:
csvWriter = csv.writer( csvFile, delimiter = ',', lineterminator = '\n' )
csvWriter.writerow( crossResultsFields + lapHeaders )
for cat in publishCategories:
results = GetResults( cat )
if not results:
continue
csvWriter.writerow( [cat.fullname] )
for rr in results:
try:
finishTime = formatTimeGap(rr.lastTime - rr.raceTimes[0], forceHours=True) if rr.status == Model.Rider.Finisher else ''
except Exception as e:
finishTime = ''
dataRow = []
for field in crossResultsFields:
dataRow.append( {
'Place': lambda : 'DNP' if rr.pos in {'NP', 'OTL', 'PUL'} else toInt(rr.pos),
'Time': lambda : finishTime,
'Last Name': lambda : getattr(rr, 'LastName', ''),
'First Name': lambda : getattr(rr, 'FirstName', ''),
'Team': lambda : getattr(rr, 'Team', ''),
'License': lambda : getattr(rr, 'License', ''),
}[field]() )
# Lap Times.
for i in range(maxLaps):
try:
lapTime = formatTimeGap(rr.lapTimes[i])
except (AttributeError, IndexError) as e:
lapTime = ''
dataRow.append( lapTime )
csvWriter.writerow( ['{}'.format(d) for d in dataRow] )
csvWriter.writerow( [] ) # Blank line separates each category.
return True, _('Success')