-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUtils.py
190 lines (160 loc) · 5.43 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
#-----------------------------------------------------------------------
# Set translation locale.
#
import wx
locale = wx.Locale()
from Version import AppVerName
import gettext
initTranslationCalled = False
def initTranslation():
global initTranslationCalled
if not initTranslationCalled:
try:
gettext.install(AppVerName.split(None, 1), './locale', unicode=True)
except:
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 platform
import datetime
import traceback
import subprocess
import unicodedata
def removeDiacritic( s ):
'''
Accept a unicode string, and return a normal string
without any diacritical marks.
'''
try:
return unicodedata.normalize('NFKD', '{}'.format(s)).encode('ASCII', 'ignore').decode()
except:
return s
'''
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 )
if 'WXMAC' in wx.Platform:
try:
topdirName = os.environ['RESOURCEPATH']
except:
topdirName = os.path.dirname(os.path.realpath(__file__))
if os.path.isdir( os.path.join(topdirName, 'images') ):
dirName = topdirName
else:
dirName = os.path.normpath(topdirName + '/../Resources/')
if not os.path.isdir(dirName):
dirName = os.path.normpath(topdirName + '/../../Resources/')
if not os.path.isdir(dirName):
raise Exception("Resource Directory does not exist:" + dirName)
else:
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) in ['library.zip', 'MainWin.exe', 'CrossMgrCallUpSeedingMgr.exe']:
dirName = os.path.dirname(dirName)
if 'CrossMgrSeedingMgr?' in os.path.basename(dirName):
dirName = os.path.dirname(dirName)
if not os.path.isdir( os.path.join(dirName, 'images') ):
dirName = os.path.dirname(dirName)
if os.path.isdir( os.path.join(dirName, 'images') ):
pass
elif os.path.isdir( '/usr/local/images' ):
dirName = '/usr/local'
imageFolder = os.path.join(dirName, 'images')
htmlFolder = os.path.join(dirName, 'html')
htmlDocFolder = os.path.join(dirName, 'htmldoc')
import webbrowser
if 'WXMAC' in wx.Platform:
webbrowser.register("chrome", None, webbrowser.MacOSXOSAScript('chrome'))
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
PlatformName = platform.system()
def writeLog( message ):
try:
dt = datetime.datetime.now()
dt = dt.replace( microsecond = 0 )
sys.stdout.write( '{} ({}) {}{}'.format(dt.isoformat(), PlatformName, message, '\n' if not message or message[-1] != '\n' else '' ) )
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")
def logCall( f ):
def _getstr( x ):
return '{}'.format(x) if not isinstance(x, wx.Object) else '<<{}>>'.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(', '.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 getHomeDir():
homedir = os.path.expanduser('~')
return homedir
def getDirName(): return dirName
def getImageFolder(): return imageFolder
def getHtmlFolder(): return htmlFolder
def getHtmlDocFolder(): return htmlDocFolder