-
Notifications
You must be signed in to change notification settings - Fork 20
/
GetMatchingExcelFile.py
53 lines (43 loc) · 1.47 KB
/
GetMatchingExcelFile.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
import os
import re
import glob
import Utils
reExcelFileVersion = re.compile(r' \((\d+)\)\.(xls|xlsx)$')
def removeExcelFileVersion( fname ):
ext = os.path.splitext(fname)[1]
fname = reExcelFileVersion.sub( '', fname )
if not fname.endswith(ext):
fname += ext
return fname
def getExcelFileVersion( fname ):
m = reExcelFileVersion.search( fname )
if not m:
return 0
return int(m.group(1))
def GetMatchingExcelFile( fileName, excelFileName ):
# Check if the file exists.
if os.path.isfile(excelFileName):
return excelFileName
baseName = Utils.plat_ind_basename(excelFileName)
dirName = os.path.dirname(fileName)
# Then check if a spreadsheet with the same name exists in the race directory.
newFileName = os.path.join( dirName, baseName )
if os.path.isfile(newFileName):
return newFileName
# Look for another spreadsheet with a more recent version number both in the race dir and the excel dir.
lastVersion = getExcelFileVersion( baseName )
baseMatchName = removeExcelFileVersion( baseName )
for src in (fileName, excelFileName):
dirName = os.path.dirname( src )
newFileName = None
for f in glob.glob('{}/*{}'.format(dirName, os.path.splitext(baseName)[1])):
baseNewName = os.path.basename( f )
if removeExcelFileVersion(baseNewName) != baseMatchName:
continue
newVersion = getExcelFileVersion( baseNewName )
if newVersion > lastVersion:
lastVersion = newVersion
newFileName = f
if newFileName:
break
return newFileName