Attachment 'errorDiagTestApp.py'
Download 1 #!/usr/bin/env python
2 #Boa:App:BoaApp
3
4 import wx
5
6 import sys
7
8 # people say one should leave this alone and use decode/encode, or define this
9 # in sitecustomize.py
10 # either of them don't really work for me, so as long as the following does
11 # this is what I will do
12 if hasattr(sys, "frozen"): #Py2Exe does not run Site.py
13 sys.setdefaultencoding('iso-8859-1')
14 del sys.setdefaultencoding
15 else: #The Python interpreter needs to reload the function
16 reload(sys)
17 sys.setdefaultencoding('iso-8859-1')
18 del sys.setdefaultencoding
19
20 import os
21
22 import time as myTime
23 import traceback
24
25 import locale
26
27 import gettext
28 gettext.install('myapp', os.path.join(os.getcwd(), 'locale'), unicode=0)
29 localeDir = os.path.join(os.getcwd(), 'locale')
30 langDe = gettext.translation('myapp', localeDir, languages=['de'])
31 langEn = gettext.translation('myapp', localeDir, languages=['en'])
32
33 locale.setlocale(locale.LC_ALL, '')
34
35 import dialogerror
36
37 provider = wx.SimpleHelpProvider()
38 wx.HelpProvider_Set(provider)
39
40 import __version__
41
42 # some application folder and file settings used by dialogerror
43 cfgFileName = 'errDiag.cfg'
44 logFolderName = 'ErrorDiagTest'
45 stdOutFileName = 'stdoutlog.txt'
46 stdErrFileName = 'stderrlog.txt'
47 sqlLogFileName = 'sqllog.txt'
48
49 import errorDiagTestF
50
51 modules ={u'dialogerror': [0, '', u'dialogerror.py'],
52 u'errorDiagTestF': [1, 'Main frame of Application', u'errorDiagTestF.py']}
53
54 class BoaApp(wx.App):
55 def OnInit(self):
56 self.SetupStdFolders()
57 self.RedirectLogFiles()
58 sys.excepthook = self.MyExceptionHandler
59
60 self.DoConfig()
61 self.ChangeLang(self.appConfig.Read(key='Language'))
62
63 self.main = errorDiagTestF.create(None)
64 self.main.Show()
65 self.SetTopWindow(self.main)
66
67 self.ReportException()
68
69 return True
70
71 def DoConfig(self):
72 configFile = os.path.join(self.progLoc, cfgFileName)
73
74 self.appConfig = wx.FileConfig(appName='MyErrorTestApp', vendorName='whoEver', localFilename=configFile)
75
76 if not self.appConfig.HasEntry('ProgramLocation'):
77 self.appConfig.Write(key='ProgramLocation', value=progLoc)
78 if not self.appConfig.HasEntry('DataLocation'):
79 self.appConfig.Write(key='DataLocation', value=dataLoc)
80 if not self.appConfig.HasEntry('Language'):
81 self.appConfig.Write(key='Language', value='en')
82 if not self.appConfig.HasEntry('SQLLog'):
83 self.appConfig.Write(key='SQLLog', value='False')
84 if not self.appConfig.HasEntry('EMail'):
85 self.appConfig.Write(key='EMail', value='')
86 if not self.appConfig.HasEntry('SMTPServer'):
87 self.appConfig.Write(key='SMTPServer', value='')
88
89 def SetLang(self, lang):
90 if lang in ['de']:
91 langDe.install()
92 else:
93 langEn.install()
94
95 def ChangeLang(self, lang):
96 """Change the configured language, currently requires app restart
97 """
98 if lang in ['en', 'de']:
99 self.appConfig.Write(key='Language', value=lang)
100 self.SetLang(lang)
101
102 def SetupStdFolders(self):
103 sp = wx.StandardPaths.Get()
104 self.userdir, self.userdoc = os.path.split(sp.GetDocumentsDir())
105
106 self.appLoc, self.curDir = os.path.split(os.getcwd())
107 self.progLoc = os.getcwd()
108 self.dataLoc = os.path.join(self.appLoc, '')
109
110 self.logFolder = os.path.join(self.userdir, logFolderName)
111 if not os.path.exists(self.logFolder):
112 os.mkdir(self.logFolder)
113
114 def RedirectLogFiles(self):
115 self.sqllog = file(os.path.join(self.logFolder, sqlLogFileName), 'a+')
116 self.stdoutlog = file(os.path.join(self.logFolder, stdOutFileName), 'a+')
117 self.stderrlog = file(os.path.join(self.logFolder, stdErrFileName), 'a+')
118
119 sys.stdout = self.stdoutlog
120 sys.stderr = self.stderrlog
121
122 def MyExceptionHandler(self, type, value, trace_back):
123 """Catch exceptions, log them to file and show error dialog
124 """
125 timestamp = myTime.asctime(myTime.localtime(myTime.time()))
126 self.stderrlog.write('**** %s ****\n' % timestamp)
127 traceback.print_exception(type, value, trace_back, file=self.stderrlog)
128 self.stderrlog.write('\n')
129 # to ensure that errorDialog is shown immediately
130 self.stderrlog.flush()
131
132 self.ReportException(msg=2)
133
134 def ReportException(self, msg = 1):
135 self.SetErrDiagMsg()
136 if msg == 1:
137 self.msg = self.msg1
138 else:
139 self.msg = self.msg2
140
141 self.msg += '\n'
142 self.msg += ('Language = %s' % self.appConfig.Read(key='Language'))
143
144 self.eMail = self.appConfig.Read(key='EMail')
145 self.smtpServer = self.appConfig.Read(key='SMTPServer')
146 myErr = False
147 self.stdoutlog.seek(0)
148 self.stderrlog.seek(0)
149 self.sqllog.seek(0)
150
151 if len(self.stdoutlog.read()) >0:
152 myErr = True
153 if len(self.stderrlog.read()) >0:
154 myErr = True
155 if len(self.sqllog.read()) >0:
156 myErr = True
157
158 if myErr:
159 self.stdoutlog.seek(0)
160 self.stderrlog.seek(0)
161 self.sqllog.seek(0)
162
163 dlg = dialogerror.DialogError(self)
164 dlg.CenterOnScreen()
165 try:
166 dlg.ShowModal()
167 finally:
168 dlg.Destroy()
169
170 self.stdoutlog.seek(0, 2)
171 self.stderrlog.seek(0, 2)
172 self.sqllog.seek(0, 2)
173
174 def SetErrDiagMsg(self):
175 # need this to get translations to work
176 self.msg1 = _('''During the last session with "myApp" at least one programming problem occurred.
177
178 If you can provide some details on how to recreate the problem that would be very helpful in correcting it.
179
180
181 Yours sincerely,
182 whoEver
183 ''')
184
185 self.msg2 = _('''During this session with "myApp" at least one programming problem occurred.
186
187 If you can provide some details on how to recreate the problem that would be very helpful in correcting it.
188
189
190 Yours sincerely,
191 whoEver
192 ''')
193
194
195 def main():
196 application = BoaApp(redirect=True)
197 application.MainLoop()
198
199 if __name__ == '__main__':
200 main()
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.