-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathontheday.py
executable file
·1136 lines (889 loc) · 42.8 KB
/
ontheday.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""OnTheDay.net Classes
This module contains functions used for importing and synchronizing race data with OnTheDay.net
via its REST API.
Many calls need an authentication that requests.get understands.
For example, simple authentication: get_race_list(('username', 'password'))
"""
#pylint: disable=wrong-spelling-in-comment
#pylint: disable=wrong-spelling-in-docstring
import argparse
import fnmatch
from getpass import getpass
import json
import os
import sys
import threading
from PyQt5.QtCore import QDate, QDateTime, QObject, QSettings, Qt, QTime, QTimer, pyqtSignal
from PyQt5.QtGui import QBrush
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QAbstractItemView, QCheckBox, QFileDialog, QHeaderView, QLabel, \
QLineEdit, QPushButton, QTableWidget, QTableWidgetItem, QWidget
from PyQt5.QtWidgets import QWizard, QWizardPage
from PyQt5.QtWidgets import QFormLayout, QHBoxLayout, QVBoxLayout
import requests
import keyring
import common
import defaults
from racemodel import MSECS_DNP, MSECS_UNINITIALIZED
__copyright__ = '''
Copyright (C) 2018-2019 Andrew Chew
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
'''
__author__ = common.AUTHOR
__credits__ = common.CREDITS
__license__ = common.LICENSE
__version__ = common.VERSION
__maintainer__ = common.MAINTAINER
__email__ = common.EMAIL
__status__ = common.STATUS
URL = 'https://ontheday.net'
HEADERS = {'User-Agent': '%s/%s' % (common.APPLICATION_NAME.replace(' ', ''),
common.VERSION)}
KEYRING_SERVICE = 'ontheday.net'
QSETTINGS_GROUP = 'ontheday'
QSETTINGS_KEY_USERNAME = 'username'
POLLING_INTERVAL_SECS = 15
class OnTheDayException(BaseException):
"""Exceptions generated by this module."""
ResultStatus = common.enum(
Ok=0,
Rejected=1
)
def check_auth(auth):
"""Check that the authenticator is good.
For this, we can do a simple transaction and make sure it comes back with
something.
"""
return len(get_race_list(auth)) > 0
def get_race_list(auth):
"""Gets the list of races that are visible from a particular authentication.
The returned race records are of the following form:
{
"url": URL to this race's event list
"name": Printable name of the race
"date": yyyy-mm-dd of the race
"tt_watch_start_time": reference clock (for time trials only)
"bulk_update_url": URL for posting results
"bulk_update_limit": number of results that can be posted in a single transaction
"bulk_update_fields": fields in a result dict
}
"""
full_race_list = []
next_url = URL + '/api/races/'
while next_url:
response = requests.get(next_url, auth=auth, headers=HEADERS,
timeout=defaults.REQUESTS_TIMEOUT_SECS)
if not response.ok:
response.raise_for_status()
response = json.loads(response.text)
full_race_list += response['results']
next_url = response['next']
# For each race in the full race list, filter out unsupported races, and get some details.
race_list = []
for race in full_race_list:
# Parse out the race id. This is a handy identifier to use to uniquely identify the race.
# url is of the form "https://ontheday.net/api/races/179/".
race['id'] = race['url'].rsplit('/', 2)[1]
# This stuff is available in the race's event list.
url = race['url']
response = requests.get(url, auth=auth, headers=HEADERS,
timeout=defaults.REQUESTS_TIMEOUT_SECS)
if not response.ok:
response.raise_for_status()
response = json.loads(response.text)
# Only time trials have a tt_watch_start_time.
if not 'tt_watch_start_time' in response.keys():
continue
race['tt_watch_start_time'] = response['tt_watch_start_time']
race['bulk_update_url'] = response['bulk_update_url']
race['bulk_update_limit'] = response['bulk_update_limit']
race['bulk_update_fields'] = response['bulk_update_fields']
race_list.append(race)
return race_list
def get_field_list(auth, race):
"""Gets the list of fields for a particular race.
The race is identified by passing a race record returned by get_race_list().
The returned field records are of the following form:
{
"name": Printable name of the field
"slug": OnTheDay.net slug line
"id": OnTheDay.net category ID
"category_index": OnTheDay.net category list index (unique within the event)
"number_start": First possible bib number
"number_stop": Last possible bib number
"time_start": Start time of the field
"entry_list_checksum": Checksum of the field (will change if racer results etc. change)
}
"""
field_list = []
url = race['url']
response = requests.get(url, auth=auth, headers=HEADERS,
timeout=defaults.REQUESTS_TIMEOUT_SECS)
if not response.ok:
response.raise_for_status()
response = json.loads(response.text)
events = response['events']
for event in events:
categories = event['categories']
# We don't care about OnTheDay.net's concept of an event, but the event
# has the category/field start time, so trickle that down into the
# category/field.
for category in categories:
category['time_start'] = event['time_start']
field_list += categories
return field_list
def get_racer_list(auth, field):
"""Gets the list of racers for a particular field.
The field is identified by passing a field record returned by get_field_list().
The returned racer records are of the following form:
{
"id": Racer ID, used for submitting results
"race_number": Bib number
"prereg": Pre-reged? (true/false)
"lastname": Racer last name
"firstname": Racer first name
"license": Racer USAC license number
"team": Racer team name
"racing_age": Racer racing age
"gender": Racer gender
"watch_start_time": Start time relative to reference clock
"watch_finish_time": Finish time relative to reference clock
"elapsed_time": Delta of start and finish, expressed as a time
"tt_dnf": True if DNF
}
"""
racer_list = []
url = field['category_entry_list_url']
response = requests.get(url, auth=auth, headers=HEADERS,
timeout=defaults.REQUESTS_TIMEOUT_SECS)
if not response.ok:
response.raise_for_status()
response = json.loads(response.text)
racer_list = response['entries']
return racer_list
def import_race(modeldb, auth, race):
"""Import a race.
The race is identified by passing a race record returned by get_race_list().
All of the fields and their racers will be imported. This function will
also set the race name and date.
"""
# Import racers (fields will be implicitly imported).
field_table_model = modeldb.field_table_model
field_list = get_field_list(auth, race)
for field in field_list:
# Add the field here. In case there are no racers in the field, we want the field added
# anyway.
# Also, we need to keep field metadata (especially the checksum, which we will use to
# check if there are any changes in the field).
metadata = {'ontheday': {'url': field['category_entry_list_url'],
'checksum': field['entry_list_checksum']}}
field_table_model.add_field(field['name'], '', json.dumps(metadata))
# This is wall clock, date of today, which is what the reference clock
# is by default (before it's set to something else).
reference_clock = modeldb.race_table_model.get_reference_clock_datetime()
# This is start time expressed as wall time, from OnTheDay.net.
# i.e. start_clock is the actual start time (not relative to reference).
racer_list = get_racer_list(auth, field)
for racer in racer_list:
add_racer_to_modeldb(modeldb, racer, field['name'], field['time_start'])
# Set race data.
race_table_model = modeldb.race_table_model
race_table_model.set_race_property(race_table_model.NAME, race['name'])
race_table_model.set_race_property(race_table_model.DATE, race['date'])
race_table_model.set_race_property('ontheday_race', json.dumps(race))
notes = 'Imported from OnTheDay.net on %s.' % QDateTime.currentDateTime().toString(Qt.ISODate)
race_table_model.set_race_property(race_table_model.NOTES, notes)
def add_racer_to_modeldb(modeldb, racer, field_name, field_start): #pylint: disable=too-many-branches
"""Adds a racer to the model, or updates an existing racer.
field_start is expressed as a string (i.e. "10:16:00")
"""
# Racers without a tt_finish_time_url are placeholder entries and should not show
# up in our racer list.
if not 'tt_finish_time_url' in racer:
return
metadata = {'ontheday': {'id': racer['id'],
'tt_finish_time_url': racer['tt_finish_time_url'],
'checksum': racer['checksum']}}
ontheday_watch_finish_time = racer['watch_finish_time']
ontheday_tt_dnf = racer['tt_dnf']
if ontheday_tt_dnf:
finish = MSECS_DNP
elif ontheday_watch_finish_time == '00:00:00':
finish = MSECS_UNINITIALIZED
else:
reference_datetime = QDateTime(QDate.currentDate())
date = reference_datetime.date()
time = QTime.fromString(ontheday_watch_finish_time, Qt.ISODateWithMs)
if time.isValid():
datetime = QDateTime(date, time)
finish = reference_datetime.msecsTo(datetime)
else:
finish = MSECS_UNINITIALIZED
if finish == MSECS_UNINITIALIZED:
status = ''
else:
status = 'remote'
if field_start:
start_clock = QDateTime(QDate.currentDate(),
QTime.fromString(field_start, Qt.ISODateWithMs))
else:
start_clock = QDateTime(QDate.currentDate(),
QTime.fromString(racer['watch_start_time'], Qt.ISODateWithMs))
start = QDateTime(QDate.currentDate()).msecsTo(start_clock)
if modeldb.racer_table_model.racer_exists(str(racer['race_number'])):
modeldb.racer_table_model.update_racer(racer['race_number'],
racer['firstname'],
racer['lastname'],
field_name,
racer['category'],
racer['team'],
racer['racing_age'],
start,
finish,
status,
json.dumps(metadata))
else:
modeldb.racer_table_model.add_racer(racer['race_number'],
racer['firstname'],
racer['lastname'],
field_name,
racer['category'],
racer['team'],
racer['racing_age'],
start,
finish,
status,
json.dumps(metadata))
def submit_results(auth, race, result_list):
"""Submits a list of results.
A result is a dict that takes the following form:
{'id': 137217,
'watch_finish_time': '00:18:30.9',
'tt_dnf': False,
'status': None}
To reset a result, use watch_finish_time of 00:00:00 and tt_dnf of False.
To submit a DNF, tt_dnf should be True, and watch_finish_time should be 00:00:00.
To submit a finish time, tt_dnf should be False, and watch_finish_time is a non-zero time.
Upon completion of this call, the status will have the following value:
ResultStatus.Ok means that the result has been committed.
ResultStatus.Rejected means the result has failed, and should not be resubmitted as is.
None means the result was not processed, and should be resubmitted.
URL: https://ontheday.net/api/entry/tt_finish_time/
"""
url = race['bulk_update_url']
headers = {**HEADERS, **{'content-type': 'application/json'}}
data = json.dumps(result_list)
response = requests.post(url, auth=auth, headers=headers, data=data,
timeout=defaults.REQUESTS_TIMEOUT_SECS)
_process_response_list(result_list, json.loads(response.text))
if not response.ok:
response.raise_for_status()
def _process_response_list(result_list, response_list):
"""Process list of result responses.
The REST response is a list, where each list entry corresponds to a submitted result. Process
each result response.
"""
for result, response in zip(result_list, response_list):
_process_response(result, response)
def _process_response(result, response):
"""Process individual response.
'non_field_errors' in the response is a list of strings, where those strings are JSON encodings
of dicts. Store this list of strings in 'error' key of the result.
"""
if 'non_field_errors' in response.keys():
result['status'] = ResultStatus.Rejected
result['errors'] = response['non_field_errors']
elif response.keys():
result['status'] = ResultStatus.Ok
def get_changes(auth, modeldb):
"""Return a list of "change" dicts (or an empty list if no changes).
A change dict is of the form:
category_name: Name of the changed category.
category_start: "time_start" stolen from the event record.
entry_list_checksum: The checksum of the field that changed.
entry_list: The list of ontheday entry records in that field.
The field_metadata was set up in import_race() and consists of 'url' and 'checksum'.
"""
race_table_model = modeldb.race_table_model
field_table_model = modeldb.field_table_model
changes = []
ontheday_race = json.loads(race_table_model.get_race_property('ontheday_race'))
response = requests.get(ontheday_race['url'], auth=auth, headers=HEADERS,
timeout=defaults.REQUESTS_TIMEOUT_SECS)
if not response.ok:
response.raise_for_status()
response = json.loads(response.text)
events = response['events']
for event in events:
for category in event['categories']:
metadata = json.loads(field_table_model.get_field_metadata(category['name']))
if category['entry_list_checksum'] == metadata['ontheday']['checksum']:
continue
response = requests.get(category['category_entry_list_url'], auth=auth, headers=HEADERS,
timeout=defaults.REQUESTS_TIMEOUT_SECS)
if not response.ok:
response.raise_for_status()
response = json.loads(response.text)
entries = response['entries']
change = {'category_name': category['name'],
'category_start': event['time_start'],
'entry_list_checksum': category['entry_list_checksum'],
'entry_list': entries}
changes.append(change)
return changes
class IntroductionPage(QWizardPage):
"""Introductory page of the import wizard that describes what we are about to do."""
def __init__(self, intro_text, parent=None):
"""Initialize the IntroductionPage instance."""
super().__init__(parent=parent)
self.setTitle('Introduction')
label = QLabel(intro_text)
label.setWordWrap(True)
self.setLayout(QVBoxLayout())
self.layout().addWidget(label)
class AuthenticationPage(QWizardPage):
"""Authentication page of the import wizard to get the OnTheDay.net username and password.
This class will look for a cached username, and if one exists, will auto-enter it into the
username field. It will then look for a password via keyring, and if exists, will auto-fill
the password field.
"""
USERNAME_FIELD = 'Username'
PASSWORD_FIELD = 'Password'
def __init__(self, parent=None):
"""Initialize the AuthenticationPage instance."""
super().__init__(parent=parent)
self.setTitle('Authentication')
self.setSubTitle('Please provide OnTheDay.net account login credentials.')
self.username_lineedit = QLineEdit()
self.password_lineedit = QLineEdit()
self.password_lineedit.setEchoMode(QLineEdit.Password)
form_widget = QWidget()
form_widget.setLayout(QFormLayout())
form_widget.layout().addRow(self.USERNAME_FIELD, self.username_lineedit)
form_widget.layout().addRow(self.PASSWORD_FIELD, self.password_lineedit)
self.status_label = QLabel()
self.status_label.setStyleSheet('QLabel{color:red;}')
self.setLayout(QVBoxLayout())
self.layout().addWidget(form_widget)
self.layout().addWidget(self.status_label)
# Register wizard page fields.
self.registerField(self.USERNAME_FIELD + '*', self.username_lineedit)
self.registerField(self.PASSWORD_FIELD + '*', self.password_lineedit)
def initializePage(self): #pylint: disable=invalid-name
"""Initialize page fields."""
self.setField(self.USERNAME_FIELD, '')
self.setField(self.PASSWORD_FIELD, '')
self.status_label.setText('')
# See if we have an OnTheDay.net username cached in our application settings.
settings = QSettings()
settings.beginGroup(QSETTINGS_GROUP)
username = settings.value(QSETTINGS_KEY_USERNAME)
settings.endGroup()
if not username:
return
# Autofill the username.
self.setField(self.USERNAME_FIELD, username)
# See if we have a password in our keyring for this username.
password = None
try:
password = keyring.get_password(KEYRING_SERVICE, username)
except keyring.errors.KeyringLocked:
pass
if not password:
return
self.setField(self.PASSWORD_FIELD, password)
def validatePage(self): #pylint: disable=invalid-name
"""Tries to access OnTheDay.net to make sure the account login credentials are okay.
Since we need to perform some kind of OnTheDay.net REST API operation, we might as well
try to grab the race list.
"""
username = self.field(self.USERNAME_FIELD)
password = self.field(self.PASSWORD_FIELD)
auth = (username, password)
try:
race_list = get_race_list(auth)
except requests.exceptions.HTTPError:
self.status_label.setText('Authentication failure')
return False
except (requests.exceptions.ConnectTimeout, requests.exceptions.Timeout):
self.status_label.setText('Timeout')
return False
self.wizard().auth = auth
self.wizard().race_list = race_list
# Cache the username, and stick the password into the keyring.
settings = QSettings()
settings.beginGroup(QSETTINGS_GROUP)
settings.setValue(QSETTINGS_KEY_USERNAME, username)
settings.endGroup()
keyring.set_password(KEYRING_SERVICE, username, password)
return True
class RaceSelectionPage(QWizardPage):
"""Race selection page of the import wizard to choose a race to import.
This class presents the list of races known by the logged in OnTheDay.net account. The user
is expected to choose a race to import.
"""
NAME_COLUMN = 0
DATE_COLUMN = 1
def __init__(self, parent=None):
"""Initialize the RaceSelectionPage instance."""
super().__init__(parent=parent)
self.setTitle('Race Selection')
self.setSubTitle('Please select a race to import.')
self.race_table_widget = QTableWidget()
self.race_table_widget.setColumnCount(2)
self.race_table_widget.setAlternatingRowColors(True)
self.race_table_widget.setHorizontalHeaderLabels(['Race', 'Date'])
self.race_table_widget.horizontalHeader().setHighlightSections(False)
self.race_table_widget.horizontalHeader().setStretchLastSection(True)
self.race_table_widget.verticalHeader().setVisible(False)
self.race_table_widget.setSelectionBehavior(QTableWidget.SelectRows)
self.race_table_widget.setSelectionMode(QAbstractItemView.SingleSelection)
self.race_table_widget.itemSelectionChanged.connect(self.completeChanged)
self.race_table_widget.itemSelectionChanged.connect(self.check_race_date)
self.status_label = QLabel()
self.status_label.setStyleSheet('QLabel{color:red;}')
self.setLayout(QVBoxLayout())
self.layout().addWidget(self.race_table_widget)
self.layout().addWidget(self.status_label)
def initializePage(self): #pylint: disable=invalid-name
"""Initialize page fields."""
self.race_table_widget.clearContents()
self.race_table_widget.setRowCount(len(self.wizard().race_list))
# Qt warns us that inserting items while sorting is enabled will mess with the insertion
# order, so disable sorting before populating the list, and then re-enable sorting
# afterwards.
self.race_table_widget.setSortingEnabled(False)
for row, race in enumerate(self.wizard().race_list):
# Make our name item (non-editable).
name_item = QTableWidgetItem(race['name'])
name_item.setFlags(name_item.flags() ^ Qt.ItemIsEditable)
name_item.setData(Qt.UserRole, race)
self.race_table_widget.setItem(row, self.NAME_COLUMN, name_item)
# Make our date item (non-editable).
date_item = QTableWidgetItem(race['date'])
date_item.setFlags(date_item.flags() ^ Qt.ItemIsEditable)
date_item.setData(Qt.UserRole, race)
self.race_table_widget.setItem(row, self.DATE_COLUMN, date_item)
self.race_table_widget.setSortingEnabled(True)
self.race_table_widget.sortByColumn(self.DATE_COLUMN, Qt.DescendingOrder)
self.race_table_widget.horizontalHeader().resizeSections(QHeaderView.ResizeToContents)
def isComplete(self): #pylint: disable=invalid-name
"""Make sure a race is selected."""
return len(self.race_table_widget.selectedItems()) > 0
def validatePage(self): #pylint: disable=invalid-name
"""No validation actually done here. Just store the selected race."""
if not self.race_table_widget.selectedItems():
return False
race = self.race_table_widget.selectedItems()[0].data(Qt.UserRole)
self.wizard().race = race
return True
def check_race_date(self):
"""See if the selected race is in the past, and update status label."""
race_date = None
for item in self.race_table_widget.selectedItems():
if item.column() == self.DATE_COLUMN:
race_date = QDate.fromString(item.text(), Qt.ISODate)
break
if race_date is None:
return
if race_date < QDate.currentDate():
self.status_label.setText('Warning: Race date is in the past.')
else:
self.status_label.setText('')
class FileSelectionPage(QWizardPage):
"""File selection page of the import wizard to choose a save file."""
def __init__(self, parent=None):
"""Initialize the FileSelectionPage instance."""
super().__init__(parent=parent)
self.setTitle('Save File')
self.setSubTitle('Please choose a save file for this race.')
self.file_lineedit = QLineEdit()
browse_button = QPushButton('Browse...')
file_browse_widget = QWidget()
file_browse_widget.setLayout(QHBoxLayout())
file_browse_widget.layout().addWidget(self.file_lineedit)
file_browse_widget.layout().addWidget(browse_button)
self.status_label = QLabel()
self.status_label.setStyleSheet('QLabel{color:red;}')
self.setLayout(QVBoxLayout())
self.layout().addWidget(file_browse_widget)
self.layout().addWidget(self.status_label)
file_dialog = common.FileDialog(self)
file_dialog.setAcceptMode(QFileDialog.AcceptSave)
file_dialog.setDefaultSuffix('rce')
file_dialog.setFileMode(QFileDialog.AnyFile)
file_dialog.setLabelText(QFileDialog.Accept, 'New')
file_dialog.setNameFilter('Race file (*.rce)')
browse_button.clicked.connect(file_dialog.exec)
file_dialog.fileSelected.connect(self.file_lineedit.setText)
self.file_lineedit.textChanged.connect(self.completeChanged)
def initializePage(self): #pylint: disable=invalid-name
"""Initialize page fields."""
default_filename = (self.wizard().race['name'] + ' ' +
self.wizard().race['date'] + '.rce')
self.file_lineedit.setText(default_filename)
def isComplete(self): #pylint: disable=invalid-name
"""Make sure a race is selected."""
filename = self.file_lineedit.text()
if not filename:
return False
if os.path.isfile(filename):
self.status_label.setText('Warming: File already exists!')
else:
self.status_label.setText('')
return True
def validatePage(self): #pylint: disable=invalid-name
"""No validation actually done here. Just store the selected filename."""
self.wizard().filename = self.file_lineedit.text()
return True
class ImportPage(QWizardPage):
"""Page that shows import progress and status."""
def __init__(self, parent=None):
"""Initialize the FileSelectionPage instance."""
super().__init__(parent=parent)
self.setTitle('Importing OnTheDay.net race')
label = QLabel('Import setup complete. Click "Finish" to begin the race import operation.')
label.setWordWrap(True)
self.reference_clock_checkbox = QCheckBox()
self.setLayout(QVBoxLayout())
self.layout().addWidget(label)
self.layout().addWidget(self.reference_clock_checkbox)
self.setButtonText(QWizard.FinishButton, 'Finish')
def initializePage(self): #pylint: disable=invalid-name
"""Initialize page fields."""
self.setSubTitle('Preparing to import %s' % (self.wizard().race['name'] + ' ' +
self.wizard().race['date']))
if ('tt_watch_start_time' in self.wizard().race.keys() and
self.wizard().race['tt_watch_start_time']):
self.reference_clock_checkbox.show()
self.reference_clock_checkbox.setChecked(True)
text = ('Import reference clock setting of %s' %
self.wizard().race['tt_watch_start_time'])
self.reference_clock_checkbox.setText(text)
else:
self.reference_clock_checkbox.hide()
def validatePage(self): #pylint: disable=invalid-name
"""No validation actually done here. Just store the reference clock import preferences."""
self.wizard().enable_reference_clock = self.reference_clock_checkbox.isChecked()
if self.wizard().enable_reference_clock:
reference_clock_date = QDate.currentDate()
reference_clock_time = QTime.fromString(self.wizard().race['tt_watch_start_time'],
Qt.ISODateWithMs)
self.wizard().reference_clock = QDateTime(reference_clock_date, reference_clock_time)
return True
class ImportWizard(QWizard):
"""OnTheDay.net import wizard.
The import wizard presents a number of pages that walks the user through importing an
OnTheDay.net race config.
The user will be asked for authentication information. A list of races will then be presented
for selection.
When the dialog completes, race will hold the race to be imported, filename will hold the
filename to use for the race data, and auth will hold an authentication thing that can be
used as the authenticator for the various REST functions.
"""
def __init__(self, parent=None):
"""Initialize the ImportWizard instance."""
super().__init__(parent=parent)
self.filename = None
self.auth = None
self.race = None
# Create the race selection page.
race_selection_page = QWizardPage()
race_selection_page.setTitle('Race Selection')
# Create the wizard and add our pages.
self.setWindowTitle('OnTheDay.net race config import')
self.addPage(IntroductionPage('This wizard will authenticate with OnTheDay.net and import '
'an existing race configuration. Optionally, a remote '
'connection to the race will be established (or this can be '
'done at a later time).'))
self.addPage(AuthenticationPage())
self.addPage(RaceSelectionPage())
self.addPage(FileSelectionPage())
self.addPage(ImportPage())
class RemoteSetupWizard(QWizard):
"""OnTheDay.net remote setup wizard.
"""
def __init__(self, race, parent=None):
"""Initialize the RemoteSetupWizard instance."""
super().__init__(parent=parent)
self.setWindowTitle('OnTheDay.net remote setup')
self.addPage(IntroductionPage('This wizard will set up the OnTheDay.net remote connection, '
'allowing race results to be pushed up to the OnTheDay.net '
'server as they are committed.'))
self.addPage(AuthenticationPage())
self.addPage(RaceSelectionPage(race))
class FieldStatisticsTable(QTableWidget):
"""QTableWidget subclass that shows statistics for the various fields in a race."""
WINDOW_TITLE = 'Field Statistics Monitor'
INTERVAL_SECS = 5 # Seconds between update end and next update start.
NAME_COLUMN = 0
FINISHED_COLUMN = 1
TOTAL_COLUMN = 2
def __init__(self, auth, race, interval_secs=INTERVAL_SECS, parent=None):
"""Initialize the FieldStatisticsTable instance."""
super().__init__(parent=parent)
self.setWindowTitle(' - '.join([self.WINDOW_TITLE, race['name']]))
self.auth = auth
field_list = get_field_list(self.auth, race)
self.field_list_lock = threading.Lock()
# Set up the table.
self.setColumnCount(3)
self.setAlternatingRowColors(True)
self.setHorizontalHeaderLabels(['Field', 'Finished', 'Total'])
self.horizontalHeader().setHighlightSections(False)
self.horizontalHeader().setStretchLastSection(True)
self.verticalHeader().setVisible(False)
self.setSelectionMode(QAbstractItemView.NoSelection)
self.clearContents()
self.setRowCount(len(field_list))
# Qt warns us that inserting items while sorting is enabled will mess with the insertion
# order, so disable sorting before populating the list, and then re-enable sorting
# afterwards.
self.setSortingEnabled(False)
for row, field in enumerate(field_list):
# Make our name item. Stash our field dict into this item to associate the field with
# this row, even if the table gets resorted.
item = QTableWidgetItem(field['name'])
item.field = field
self.setItem(row, self.NAME_COLUMN, item)
# Make our finished item.
item = QTableWidgetItem(0)
self.setItem(row, self.FINISHED_COLUMN, item)
# Make our total item.
item = QTableWidgetItem(0)
self.setItem(row, self.TOTAL_COLUMN, item)
self.setSortingEnabled(True)
self.sortByColumn(self.NAME_COLUMN, Qt.AscendingOrder)
self.horizontalHeader().resizeSections(QHeaderView.ResizeToContents)
self.read_settings()
# Make our single-shot update timer.
self.timer = QTimer(self)
self.timer.setInterval(interval_secs * 1000)
self.timer.setTimerType(Qt.VeryCoarseTimer)
self.timer.setSingleShot(True)
self.timer.timeout.connect(self.schedule_work)
# Simulate a timer timeout so that we immediately schedule work.
self.schedule_work()
def close(self):
"""Handle window close."""
self.write_settings()
def schedule_work(self):
"""Make our worker thread."""
worker_thread = self.WorkerThread(self)
worker_thread.done.connect(self.update_table)
worker_thread.start()
def update_table(self):
"""Update field statistics."""
for row in range(self.rowCount()):
field = self.item(row, self.NAME_COLUMN).field
with self.field_list_lock:
finished = field['finished']
total = field['total']
# Set finished and total.
self.item(row, self.FINISHED_COLUMN).setData(Qt.DisplayRole, finished)
self.item(row, self.TOTAL_COLUMN).setData(Qt.DisplayRole, total)
# Pick a background color for this row, depending on finished and total.
if total > 0:
if finished == 0:
brush = None
elif 0 < finished < total:
brush = QBrush(Qt.yellow)
elif finished == total:
brush = QBrush(Qt.green)
else:
brush = QBrush(Qt.red) # To show that something is horribly wrong.
else:
brush = None
for column in range(self.columnCount()):
self.item(row, column).setData(Qt.BackgroundRole, brush)
self.timer.start()
def read_settings(self):
"""Read settings."""
group_name = self.__class__.__name__
settings = QSettings()
settings.beginGroup(group_name)
if settings.contains('size'):
self.resize(settings.value('size'))
if settings.contains('pos'):
self.move(settings.value('pos'))
if settings.contains('horizontal_header_state'):
self.horizontalHeader().restoreState(settings.value('horizontal_header_state'))
settings.endGroup()
def write_settings(self):
"""Write settings."""
group_name = self.__class__.__name__
settings = QSettings()
settings.beginGroup(group_name)
settings.setValue('size', self.size())
settings.setValue('pos', self.pos())
settings.endGroup()
class WorkerThread(QObject, threading.Thread):
"""Worker thread that does the REST calls to gather the field statistics."""
def __init__(self, parent):
"""Initialize the OnTheDayRemoteWorker instance."""
super().__init__()
self.parent = parent
def run(self):
"""Submit a batch of results to OnTheDay.net."""
for row in range(self.parent.rowCount()):
field = self.parent.item(row, self.parent.NAME_COLUMN).field
racer_list = get_racer_list(self.parent.auth, field)
total = len(racer_list)
finished = 0
for racer in racer_list:
if racer['watch_finish_time'] != '00:00:00' or racer['tt_dnf']:
finished += 1
with self.parent.field_list_lock:
field['finished'] = finished
field['total'] = total
self.done.emit()
done = pyqtSignal()
class CommandLineTool():
"""Class that supports command-line ontheday.net tools."""
def __init__(self):
"""Command-line entry point to some useful OnTheDay.net stuff."""
# Define top-level parser.
parser = argparse.ArgumentParser()
parser.add_argument('--version', '-v', action='version',
version=common.APPLICATION_NAME + ' v' + common.VERSION)
parser.add_argument('--username', '-u')
parser.add_argument('--password', '-p')
parser.set_defaults(func=parser.print_help)
subparsers = parser.add_subparsers()
# Define 'list' parser.
list_parser = subparsers.add_parser('list')
list_parser.set_defaults(func=list_parser.print_help)
list_subparsers = list_parser.add_subparsers()
# Define 'list races' parser.
list_races_parser = list_subparsers.add_parser('races')
list_races_parser.set_defaults(func=self.list_races)
# Define 'list fields' parser.
list_fields_parser = list_subparsers.add_parser('fields')
list_fields_parser.set_defaults(func=self.list_fields)
list_fields_parser.add_argument('race_id')
# Define 'list racers' parser.
list_racers_parser = list_subparsers.add_parser('racers')
list_racers_parser.set_defaults(func=self.list_racers)
list_racers_parser.add_argument('race_id')
list_racers_parser.add_argument('field_name')
monitor_parser = subparsers.add_parser('monitor')
monitor_parser.set_defaults(func=self.monitor)
monitor_parser.add_argument('--interval', type=int, dest='monitor_interval',
default=FieldStatisticsTable.INTERVAL_SECS,