Skip to content

Commit

Permalink
Improved Error report.
Browse files Browse the repository at this point in the history
Refactored getting the default return structure for reading files.
Added more strict uci_id checking.
  • Loading branch information
esitarski committed Jul 29, 2024
1 parent 56e2c7e commit acb2ca0
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 21 deletions.
10 changes: 8 additions & 2 deletions SeriesMgr/Errors.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import wx
import wx.grid as gridlib

import os
import SeriesModel
from ReorderableGrid import ReorderableGrid
import Utils

def shorterFilename( fileName ):
fileName = fileName.replace('\\','/')
components = fileName.split('/')
return os.path.join( *components[-3:] )

class Errors(wx.Panel):
#----------------------------------------------------------------------
ErrorCol = 0
Expand Down Expand Up @@ -43,8 +49,8 @@ def refresh( self ):
model = SeriesModel.model
Utils.AdjustGridSize( self.grid, len(model.errors) )
for row, (r, e) in enumerate(model.errors):
self.grid.SetCellValue( row, self.RaceCol, r.fileName )
self.grid.SetCellValue( row, self.ErrorCol, '{}'.format(e) )
self.grid.SetCellValue( row, self.RaceCol, shorterFilename(r.fileName) )
self.grid.SetCellValue( row, self.ErrorCol, f'{e}' )
wx.CallAfter( self.gridAutoSize )

def commit( self ):
Expand Down
23 changes: 14 additions & 9 deletions SeriesMgr/GetModelInfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ def safe_upper( f ):
return f

def fix_uci_id( uci_id ):
# From a str, int, float or None ,and convert to a uci id as best as possible.
# Do not do the remainder test.
uci_id = str(uci_id or '').strip()
if uci_id.endswith('.0'): # Correct if the uci_id is a floating point number.
uci_id = uci_id[-2:]
Expand Down Expand Up @@ -165,12 +167,6 @@ def full_name( self ):
def __repr__( self ):
return '\n({})'.format( ', '.join( '"{}"'.format(p) for p in (self.raceName, self.team, self.full_name, self.categoryName) ) )

def ExtractRaceResults( fileName ):
if os.path.splitext(fileName)[1] == '.cmn':
return ExtractRaceResultsCrossMgr( fileName )
else:
return ExtractRaceResultsExcel( fileName )

def toInt( n ):
if n == 'DNF':
return SeriesModel.rankDNF
Expand All @@ -179,8 +175,11 @@ def toInt( n ):
except Exception:
return n

def getRaceResultRet():
return { 'success':True, 'explanation':'success', 'raceResults':[], 'licenseLinkTemplate':None, 'isUCIDataride':False, 'pureTeam':False, 'resultsType':0 }

def ExtractRaceResultsExcel( raceFileName ):
ret = { 'success':True, 'explanation':'success', 'raceResults':[], 'licenseLinkTemplate':None, 'isUCIDataride':False, 'pureTeam':False, 'resultsType':0 }
ret = getRaceResultRet()

if not os.path.exists( raceFileName ):
ret['success'] = False
Expand Down Expand Up @@ -358,7 +357,7 @@ def FixExcelSheetLocal( fileName, race ):
race.excelLink.fileName = newFileName

def ExtractRaceResultsCrossMgr( raceFileName ):
ret = { 'success':True, 'explanation':'success', 'raceResults':[], 'licenseLinkTemplate':None, 'isUCIDataride':False, 'pureTeam':False, 'resultsType':0 }
ret = getRaceResultRet()

try:
with open(raceFileName, 'rb') as fp, Model.LockRace() as race:
Expand All @@ -374,7 +373,7 @@ def ExtractRaceResultsCrossMgr( raceFileName ):

except Exception as e:
ret['success'] = False
ret['explanation'] = e
ret['explanation'] = str(e)
return ret

race = Model.race
Expand Down Expand Up @@ -465,6 +464,12 @@ def ExtractRaceResultsCrossMgr( raceFileName ):
ret['raceResults'] = raceResults
return ret

def ExtractRaceResults( fileName ):
if os.path.splitext(fileName)[1] == '.cmn':
return ExtractRaceResultsCrossMgr( fileName )
else:
return ExtractRaceResultsExcel( fileName )

def AdjustForUpgrades( raceResults ):
upgradePaths = []
for path in SeriesModel.model.upgradePaths:
Expand Down
2 changes: 1 addition & 1 deletion SeriesMgr/Results.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def getHeaderNames():

def isUCIID( uci_id ):
uci_id = re.sub( '[^0-9]', '', str(uci_id) )
return len(uci_id == 11) and sum( int(c) for c in uci_id[:9] ) % 97 == int(uci_id[9:] )
return len(uci_id == 11) and int(uci_id[:-2]) % 97 == int(uci_id[-2:])

def formatUCIID( uci_id ):
uci_id = re.sub( '[^0-9]', '', str(uci_id) )
Expand Down
32 changes: 24 additions & 8 deletions SeriesMgr/SeriesModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ def __get__(self, obj, objtype):
# Support instance methods.
return functools.partial(self.__call__, obj)

def ValidateUCIID( uci_id ):
# Returns a valid UCI ID string, or '' if the uci_id is invalid.
# Strict: checks length and remainder.
uci_id = str(uci_id).replace(' ','')
if uci_id.endswith('.0'): # If the uci id was a float, remove the decimal.
uci_id = uci_id[:-2]
return uci_id if (len(uci_id) == 11 and uci_id.isdigit() and int(uci_id[:-2]) % 97 == int(uci_id[-2:])) else ''

def RaceNameFromPath( p, isUCIDataride=False ):
if isUCIDataride:
folderName = os.path.basename( os.path.dirname(p) )
Expand Down Expand Up @@ -736,16 +744,24 @@ def extractAllRaceResults( self, adjustForUpgrades=True, isIndividual=True ):
# Remove race results with missing keys. Add corresponding errors to list.
if self.riderKey == self.KeyByUciId:
# If matching by UCI ID, remove all riders without a UCI ID and record those missing one as errors.
self.errors.extend(
(rr.raceInSeries, f'{keyErrorPrefix}Missing UCI ID: ({rr.categoryName}) {rr.lastName}, {rr.firstName}') for rr in raceResults if not rr.uci_id
)
raceResults = [rr for rr in raceResults if rr.uci_id]
raceResultsKeep = []
for rr in raceResults:
uci_id_existing = rr.uci_id or ''
rr.uci_id = ValidateUCIID( rr.uci_id )
if not rr.uci_id:
self.errors.append( (rr.raceInSeries, f'{keyErrorPrefix}Invalid UCI ID: ({rr.categoryName}) {rr.lastName}, {rr.firstName} - [{uci_id_existing}]') )
else:
raceResultsKeep.append( rr )
raceResults = raceResultsKeep
elif self.riderKey == self.KeyByLicense:
# If matching by license, remove all riders without a license and record those missing one as errors.
self.errors.extend(
(rr.raceInSeries, f'{keyErrorPrefix}Missing License: ({rr.categoryName}) {rr.lastName}, {rr.firstName}') for rr in raceResults if not rr.license
)
raceResults = [rr for rr in raceResults if rr.license]
raceResultsKeep = []
for rr in raceResults:
if not rr.license:
self.errors.append( (rr.raceInSeries, f'{keyErrorPrefix}Missing License: ({rr.categoryName}) {rr.lastName}, {rr.firstName}') )
else:
raceResultsKeep.append( rr )
raceResults = raceResultsKeep

# Set the change flag if the errors change.
self.changed |= (oldErrors != self.errors)
Expand Down
2 changes: 1 addition & 1 deletion SeriesMgr/Version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
AppVerName="SeriesMgr 3.0.41-private"
AppVerName="SeriesMgr 3.0.42-private"

0 comments on commit acb2ca0

Please sign in to comment.