-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathFinishLynx.py
283 lines (232 loc) · 7.91 KB
/
FinishLynx.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import wx
import io
import os
import csv
import math
import time
import operator
import datetime
import random
import Model
import Utils
def getLynxDir( race ):
fileName = Utils.getFileName()
baseName = os.path.splitext(fileName)[0]
while baseName.endswith('-'):
baseName = baseName[:-1]
return baseName + '-lynx'
def hhmmssmsFromSeconds( t ):
fract, secs = math.modf( t )
ms = round( fract * 1000000.0 )
secs = int( secs )
hh = secs // (60*60)
mm = (secs // 60) % 60
ss = secs % 60
return hh, mm, ss, ms
def strToSeconds( s = '' ):
secs = 0.0
for f in s.split(':'):
try:
n = float(f)
except ValueError:
n = 0.0
secs = secs * 60.0 + n
return secs
def strToInt( s ):
try:
return int(s.strip())
except ValueError:
return s
def getStatus( p ):
p = p.upper().strip()
if p.isdigit():
return Model.Rider.Finisher
if p == 'DNS':
return Model.Rider.DNS
if 'Q' in p:
return Model.Rider.DQ
return Model.Rider.DNF # default DNF
def ReadLIF( fname ):
race = Model.race
if not race:
return
reader = csv.reader( fname )
# Place, ID, lane, last name, first name, affiliation, <time>, <license>, <delta time>, <ReacTime>, <splits>, time trial start time, user 1, user 2, user 3
fields = ('place', 'id', 'lane', 'last_name', 'first_name', 'affiliation', 'time', 'license', 'delta_time', 'react_time', 'splits', 'tt_start_time')
int_fields = {'id', 'lane'}
time_fields = {'time', 'react_time', 'delta_time', 'tt_start_time'}
def getFields( raceStart, row ):
record = {f:None for f in fields}
del record['splits']
record['race_times'] = []
record['lap_times'] = []
record['status'] = Model.Rider.Finisher
for i, f in enumerate(fields):
try:
v = row[i]
except IndexError:
break
if f == 'splits':
# "splits" is a comma separated field of "race_time (lap_time)" pairs.
race_times = []
lap_times = []
for split in v.split(','):
sv = split.split(' ')
try:
race_times.append( strToSeconds(sv[0]) )
lap_times.append( strToSeconds(sv[1][1:-1]) ) # Remove brackets.
except IndexError:
break
record['race_times'] = race_times
record['lap_times'] = lap_times
continue
if f == 'place':
record['status'] = getStatus( v )
if f in time_fields:
v = strToSeconds( v )
elif f in int_fields:
v = strToInt( v )
record[f] = v
return record
for i in range(3):
with open(fname, encoding='utf8') as f:
s = f.read()
# Check for trailing newline.
if not (s and s.endswith('\n')):
# Empty file or no newline. File is likely being written. Wait a little and try again.
time.sleep( 0.5 + random.random() )
continue
raceStart = None
reader = csv.reader( io.StringIO(s) )
for i, row in enumerate(reader):
if i == 0:
# Get the race start time from the header.
raceStart = strToSeconds( row[-1] )
race.startTime = datetime.datetime( *(tuple(int(v) for v in race.date.split('-')) + hhmmssmsFromSeconds(raceStart)) )
continue
try:
yield getFields(raceStart, row)
except Exception as e:
pass
break
else:
raise ValueError( 'FinishLynx results file is empty or does not end with newline' )
def ImportLIF( fname ):
race = Model.race
if not race:
return
race.resetAllCaches()
for r in ReadLIF( fname ):
rider = race.getRider( r['id'] )
rider.times = []
if not race.isTimeTrial:
rider.firstTime = None
for t in r['race_times']:
race.addTime( r['id'], t, False )
rider.setStatus( r['status'] )
race.setChanged()
#-----------------------------------------------------------------------
def Export( folder=None ):
''' Export the race in FinishLynx file format (lynx.ppl, lynx.evt, lynx.sch). '''
''' Write the files into a -lynx folder in the CrossMgr race folder (if no folder given). '''
''' Ignore the category start offsets and put everyone into one start for FinishLynx. '''
''' Assume that the categories were started with the correct offsets on import. '''
race = Model.race
if not race:
return
try:
externalInfo = race.excelLink.read()
except Exception:
externalInfo = {}
folder = folder or getLynxDir( race )
if not os.path.isdir(folder):
os.mkdir( folder )
fnameBase = os.path.join( folder, 'lynx' )
# Create the people reference file.
# ID number, last name, first name, affiliation
fields = ('LastName', 'FirstName', 'Team')
with open(fnameBase + '.ppl', 'w', newline='', encoding='utf8') as f:
writer = csv.writer( f )
for id, info in sorted( externalInfo.items(), key=operator.itemgetter(0) ):
writer.writerow( [id] + [externalInfo.get(field,'') for field in fields] )
# Event number, round number, heat number, event name
# <tab, space or comma>ID, lane # lane=0 as there are no assigned lanes.
with open(fnameBase + '.evt', 'w', encoding='utf8') as f:
f.write( '1,1,1,{}\n'.format( os.path.splitext(race.getFileName(includeMemo=False))[0] ) )
for id in sorted( externalInfo.keys() ):
f.write( ',{},0\n'.format(id) )
# event number, round number, heat number
with open(fnameBase + '.sch', 'w', encoding='utf8') as f:
f.write( '1,1,1\n' )
#-----------------------------------------------------------------------
class FinishLynxDialog( wx.Dialog ):
def __init__( self, parent, id = wx.ID_ANY ):
super().__init__( parent, id, "FinishLynx",
style=wx.DEFAULT_DIALOG_STYLE|wx.TAB_TRAVERSAL )
mainSizer = wx.BoxSizer( wx.VERTICAL )
fgs = wx.FlexGridSizer( 2, 4, 4 )
self.exportButton = wx.Button( self, label=_('Export CrossMgr Data to FinishLynx Files (linx.ppl, linx.evt, linx.sch)') )
self.exportButton.Bind( wx.EVT_BUTTON, self.onExport )
fgs.Add( self.exportButton )
self.exportText = wx.StaticText( self, label=_('') )
fgs.Add( self.exportText )
self.importButton = wx.Button( self, label=_('Import FinishLynx Results Files into CrossMgr (*.lif)') )
self.importButton.Bind( wx.EVT_BUTTON, self.onImport )
self.importText = wx.StaticText( self, label=_('') )
fgs.Add( self.importButton )
fgs.Add( self.importText )
fgs.AddGrowableCol( 1 )
mainSizer.Add( fgs, flag=wx.EXPAND|wx.ALL, border=8 )
btnSizer = self.CreateStdDialogButtonSizer( wx.CLOSE )
if btnSizer:
mainSizer.Add( btnSizer, flag=wx.ALL|wx.EXPAND, border = 8 )
self.SetSizerAndFit( mainSizer )
def onExport( self, event ):
race = Model.race
if not Model.race:
return
folder = getLynxDir( race )
pplname = os.path.join( folder, 'lynx.ppl' )
if (os.path.isfile(pplname) and
wx.OK != wx.MessageBox(
'{}\n\n\t{}\n\n{}'.format(
_("FinishLynx files already exist in."),
folder,
_("Replace them?"),
),
_("FinishLynx Export"), wx.OK|wx.CANCEL, self )
):
return
try:
Export()
wx.MessageBox( '{}.\n{}:\n\n {}'.format( _("Export successful"), _(".ppl, .evt, .sch files written to folder"), getLynxDir(race) ), _("FinishLynx Export"), wx.OK, self )
except Exception as e:
wx.MessageBox( '{}:\n\n\t{}'.format( _("Export failure"), e), _("FinishLynx Export"), wx.OK, self )
def onImport( self, event ):
race = Model.race
if not Model.race:
return
with wx.FileDialog(self, _("FinishLynx Results Import "), defaultDir=getLynxDir(race), wildcard="FinishLynx Results (*.lif)|*.lif",
style=wx.FD_OPEN | wx.FD_FILE_MUST_EXIST) as fileDialog:
if fileDialog.ShowModal() == wx.ID_CANCEL:
return
pathname = fileDialog.GetPath()
try:
ImportLIF( pathname )
wx.MessageBox( _("Import successful"), _("FinishLynx Import"), wx.OK, self )
Utils.refresh()
except Exception as e:
wx.MessageBox( '{}:\n\n\t{}'.format( _("Import failure"), e), _("FinishLynx Import"), wx.OK, self )
if __name__ == "__main__":
Model.race = Model.Race()
Model.race._populate()
#Export( 'finishlynx' )
ImportLIF( '/home/edward/Downloads/Wave 1 _1 Default.lif' )
'''
app = wx.App(False)
mainWin = wx.Frame(None,title="CrossMan", size=(1024,600))
finishLynx = FinishLynxDialog( mainWin )
Model.newRace()
finishLynx.ShowModal()
app.MainLoop()
'''