-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUtils.py
297 lines (264 loc) · 8.03 KB
/
Utils.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
#-----------------------------------------------------------------------
# Set translation locale.
#
import wx
locale = wx.Locale()
from Version import AppVerName
import gettext
initTranslationCalled = False
def initTranslation():
global initTranslationCalled
if not initTranslationCalled:
gettext.install(AppVerName.split(None, 1), './locale')
initTranslationCalled = True
initTranslation()
try:
from win32com.shell import shell, shellcon
except ImportError:
pass
import os
import re
import sys
import math
import subprocess
import datetime
import platform
import traceback
import unicodedata
def removeDiacritic(input):
'''
Accept a unicode string, and return a normal string (bytes in Python 3)
without any diacritical marks.
'''
if type(input) == str:
return input
else:
return unicodedata.normalize('NFKD', input).encode('ASCII', 'ignore')
'''
wx.ICON_EXCLAMATION Shows an exclamation mark icon.
wx.ICON_HAND Shows an error icon.
wx.ICON_ERROR Shows an error icon - the same as wxICON_HAND.
wx.ICON_QUESTION Shows a question mark icon.
wx.ICON_INFORMATION Shows an information (i) icon.
'''
def MessageOK( parent, message, title = '', iconMask = 0):
dlg = wx.MessageDialog(parent, message, title, wx.OK | iconMask)
dlg.ShowModal()
dlg.Destroy()
return True
def MessageOKCancel( parent, message, title = '', iconMask = 0):
dlg = wx.MessageDialog(parent, message, title, wx.OK | wx.CANCEL | iconMask )
response = dlg.ShowModal()
dlg.Destroy()
return True if response == wx.ID_OK else False
def MessageYesNoCancel( parent, message, title = '', iconMask = 0 ):
dlg = wx.MessageDialog(parent, message, title, wx.YES_NO | wx.CANCEL | iconMask )
response = dlg.ShowModal()
dlg.Destroy()
return response
def DeleteAllGridRows( grid ):
if grid.GetNumberRows() > 0:
grid.DeleteRows( 0, grid.GetNumberRows(), True )
def AdjustGridSize( grid, rowsRequired = None, colsRequired = None ):
# print 'AdjustGridSize: rowsRequired=', rowsRequired, ' colsRequired=', colsRequired
if rowsRequired is not None:
rowsRequired = int(rowsRequired)
d = grid.GetNumberRows() - rowsRequired
if d > 0:
grid.DeleteRows( rowsRequired, d )
elif d < 0:
grid.AppendRows( -d )
if colsRequired is not None:
colsRequired = int(colsRequired)
d = grid.GetNumberCols() - colsRequired
if d > 0:
grid.DeleteCols( colsRequired, d )
elif d < 0:
grid.AppendCols( -d )
try:
dirName = os.path.dirname(os.path.abspath(__file__))
except:
dirName = os.path.dirname(os.path.abspath(sys.argv[0]))
if os.path.basename(dirName) == 'library.zip':
dirName = os.path.dirname(dirName)
imageFolder = os.path.join(dirName, 'StageRaceGCImages')
htmlDocFolder = os.path.join(dirName, 'StageRaceGCHtmlDoc')
PlatformName = platform.system()
def writeLog( message ):
try:
dt = datetime.datetime.now()
dt = dt.replace( microsecond = 0 )
msg = u'{} ({}) {}{}'.format(
dt.isoformat(),
PlatformName,
message,
'\n' if not message or message[-1] != '\n' else ''
)
sys.stdout.write( removeDiacritic(msg) )
sys.stdout.flush()
except IOError:
pass
def disable_stdout_buffering():
'''
fileno = sys.stdout.fileno()
temp_fd = os.dup(fileno)
sys.stdout.close()
os.dup2(temp_fd, fileno)
os.close(temp_fd)
sys.stdout = os.fdopen(fileno, "w", 0)
'''
def logCall( f ):
def _getstr( x ):
return u'{}'.format(x) if not isinstance(x, wx.Object) else u'<<{}>>'.format(x.__class__.__name__)
def new_f( *args, **kwargs ):
parameters = [_getstr(a) for a in args] + [ '{}={}'.format( key, _getstr(value) ) for key, value in kwargs.items() ]
writeLog( 'call: {}({})'.format(f.__name__, removeDiacritic(u', '.join(parameters))) )
return f( *args, **kwargs)
return new_f
def logException( e, exc_info ):
eType, eValue, eTraceback = exc_info
ex = traceback.format_exception( eType, eValue, eTraceback )
writeLog( '**** Begin Exception ****' )
for d in ex:
for line in d.split( '\n' ):
writeLog( line )
writeLog( '**** End Exception ****' )
def fieldToHeader( f, multi_line=False ):
if f == 'uci_id':
return _('UCI ID')
if f == 'uci_code':
return _('UCI Code')
if f.endswith('_name'):
f = f[:-5]
f = f.replace('_', u' ').replace('uci', 'UCI')
s = ' '.join( w[0].upper() + w[1:] for w in f.split() )
s = s.replace(' With ', ' with ').replace(' In ', ' in ').replace(' Of ', ' of ')
s = s.replace( 'with Bonus', '-Bonus' ).replace( 'Plus Penalty', '+Penalty' ).replace( 'Plus Second', '+Second' )
s = s.replace( 'Kom', 'KOM' )
if multi_line:
fields = s.split()
if len(fields) == 2:
s = '\n'.join( fields )
elif len(fields) > 2:
i = len(s) // 2
dFirst = s[:i].rfind(' ')
dLast = s[i:].find(' ')
if dFirst > 0 or dLast > 0:
if dLast < 0:
dLast = 100000
if i - dFirst < dLast:
j = dFirst
else:
j = i + dLast
s = s[:j] + '\n' + s[j+1:]
return s
def formatTime( secs,
highPrecision=False, extraPrecision=False,
forceHours=False, twoDigitHours=False,
forceMinutes=True, twoDigitMinutes=True,
twoDigitSeconds=False ):
if secs is None:
secs = 0
if secs < 0:
sign = '-'
secs = -secs
else:
sign = ''
f, ss = math.modf(secs)
secs = int(ss)
hours = int(secs // (60*60))
minutes = int( (secs // 60) % 60 )
secs = (secs % 60) + f
if highPrecision or extraPrecision:
if extraPrecision:
secStr = '{:06.3f}'.format( secs )
else:
secStr = '{:05.2f}'.format( secs )
else:
secStr = '{:02.0f}'.format( secs )
if secStr.startswith('60'):
secStr = '00' + secStr[2:]
minutes += 1
if minutes == 60:
minutes = 0
hours += 1
if forceHours or hours > 0:
return "{}{:0{hourWidth}d}:{:02d}:{}".format(sign, hours, minutes, secStr, hourWidth=2 if twoDigitHours else 0)
else:
if forceMinutes or minutes > 0:
return "{}{:0{minuteWidth}d}:{}".format(sign, minutes, secStr, minuteWidth=2 if twoDigitMinutes else 0)
else:
return "{}{}".format(
sign,
secStr if twoDigitSeconds else (secStr.lstrip('0') if not secStr.startswith('00') else secStr[1:])
)
def formatTimeGap( secs, highPrecision=False, separateWithQuotes=True, forceHours=False ):
if secs is None:
secs = 0
if secs < 0:
sign = '-'
secs = -secs
else:
sign = ''
f, ss = math.modf(secs)
secs = int(ss)
hours = int(secs // (60*60))
minutes = int( (secs // 60) % 60 )
secs = (secs % 60) + f
if highPrecision:
secStr = '{:05.2f}'.format( secs )
else:
secStr = '{:02d}'.format( int(secs) ) # Truncate fractional seconds.
if secStr == '60' or secStr == '60.00':
secStr = '00.00' if highPrecision else '00'
minutes += 1
if minutes == 60:
minutes = 0
hours += 1
if separateWithQuotes:
if forceHours or hours > 0:
return "{}{}h{}'{}\"".format(sign, hours, minutes, secStr)
else:
return "{}{}'{}\"".format(sign, minutes, secStr)
else:
if forceHours or hours > 0:
return "{}{}:{:02d}:{}".format(sign, hours, minutes, secStr)
else:
return "{}{:02d}:{}".format(sign, minutes, secStr)
def StrToSeconds( str = '' ):
secs = 0.0
for f in str.split(':'):
try:
n = float(f)
except ValueError:
n = 0.0
secs = secs * 60.0 + n
return secs
lang = 'en'
def ordinal( value ):
try:
value = int(value)
except ValueError:
return u'{}'.format(value)
return {
'fr': lambda v: '{}{}'.format(v, 'er' if v == 1 else 'e'),
'es': lambda v: u'{}.\u00B0'.format(v),
'en': lambda v: "{}{}".format(v, ['th','st','nd','rd','th','th','th','th','th','th'][v%10]) if (v % 100)//10 != 1 else "{}{}".format(value, "th"),
}.get( lang[:2], lambda v: '{}'.format(v) )( value )
def getHomeDir():
homedir = os.path.expanduser('~')
return homedir
def getDirName(): return dirName
def getImageFolder(): return imageFolder
def getHtmlFolder(): return htmlFolder
def getHtmlDocFolder(): return htmlDocFolder
if sys.platform == 'darwin':
webbrowser.register("chrome", None, webbrowser.MacOSXOSAScript('chrome'), -1)
def LaunchApplication( fnames ):
for fname in (fnames if isinstance(fnames, list) else [fnames]):
if os.name == 'nt':
subprocess.call(('cmd', '/C', 'start', '', fname))
elif sys.platform.startswith('darwin'):
subprocess.call(('open', fname))
else:
subprocess.call(('xdg-open', fname)) # Linux