author | Laurent Bessard |
Sun, 24 Mar 2013 23:54:50 +0100 | |
changeset 998 | 2f7721dae9a0 |
parent 980 | c7ba67d01d65 |
child 1030 | d9e75a5b5f08 |
permissions | -rw-r--r-- |
814 | 1 |
#!/usr/bin/env python |
2 |
# -*- coding: utf-8 -*- |
|
3 |
||
4 |
#This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor |
|
5 |
#based on the plcopen standard. |
|
6 |
# |
|
7 |
#Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD |
|
8 |
# |
|
9 |
#See COPYING file for copyrights details. |
|
10 |
# |
|
11 |
#This library is free software; you can redistribute it and/or |
|
12 |
#modify it under the terms of the GNU General Public |
|
13 |
#License as published by the Free Software Foundation; either |
|
14 |
#version 2.1 of the License, or (at your option) any later version. |
|
15 |
# |
|
16 |
#This library is distributed in the hope that it will be useful, |
|
17 |
#but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
18 |
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
19 |
#General Public License for more details. |
|
20 |
# |
|
21 |
#You should have received a copy of the GNU General Public |
|
22 |
#License along with this library; if not, write to the Free Software |
|
23 |
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
24 |
||
25 |
import wx |
|
26 |
import os, sys, platform, time, traceback, getopt |
|
27 |
||
28 |
CWD = os.path.split(os.path.realpath(__file__))[0] |
|
29 |
||
30 |
__version__ = "$Revision: 1.130 $" |
|
31 |
||
32 |
if __name__ == '__main__': |
|
33 |
# Usage message displayed when help request or when error detected in |
|
34 |
# command line |
|
35 |
def usage(): |
|
36 |
print "\nUsage of PLCOpenEditor.py :" |
|
37 |
print "\n %s [Filepath]\n"%sys.argv[0] |
|
38 |
||
39 |
# Parse options given to PLCOpenEditor in command line |
|
40 |
try: |
|
41 |
opts, args = getopt.getopt(sys.argv[1:], "h", ["help"]) |
|
42 |
except getopt.GetoptError: |
|
43 |
# print help information and exit: |
|
44 |
usage() |
|
45 |
sys.exit(2) |
|
46 |
||
47 |
# Extract if help has been requested |
|
48 |
for o, a in opts: |
|
49 |
if o in ("-h", "--help"): |
|
50 |
usage() |
|
51 |
sys.exit() |
|
52 |
||
53 |
# Extract the optional filename to open |
|
54 |
fileOpen = None |
|
55 |
if len(args) > 1: |
|
56 |
usage() |
|
57 |
sys.exit() |
|
58 |
elif len(args) == 1: |
|
59 |
fileOpen = args[0] |
|
60 |
||
61 |
# Create wxApp (Need to create App before internationalization because of |
|
62 |
# Windows) |
|
63 |
app = wx.PySimpleApp() |
|
64 |
||
815
e4f24593a758
Adding support for extending internationalization to extensions
laurent
parents:
814
diff
changeset
|
65 |
from docutil import * |
e4f24593a758
Adding support for extending internationalization to extensions
laurent
parents:
814
diff
changeset
|
66 |
|
967
8a339cd61cb4
Added support for extension custom contextual add menu
Laurent Bessard
parents:
838
diff
changeset
|
67 |
from util.TranslationCatalogs import AddCatalog |
815
e4f24593a758
Adding support for extending internationalization to extensions
laurent
parents:
814
diff
changeset
|
68 |
from util.BitmapLibrary import AddBitmapFolder, GetBitmap |
e4f24593a758
Adding support for extending internationalization to extensions
laurent
parents:
814
diff
changeset
|
69 |
|
e4f24593a758
Adding support for extending internationalization to extensions
laurent
parents:
814
diff
changeset
|
70 |
AddCatalog(os.path.join(CWD, "locale")) |
e4f24593a758
Adding support for extending internationalization to extensions
laurent
parents:
814
diff
changeset
|
71 |
AddBitmapFolder(os.path.join(CWD, "images")) |
814 | 72 |
|
73 |
if __name__ == '__main__': |
|
815
e4f24593a758
Adding support for extending internationalization to extensions
laurent
parents:
814
diff
changeset
|
74 |
# Import module for internationalization |
e4f24593a758
Adding support for extending internationalization to extensions
laurent
parents:
814
diff
changeset
|
75 |
import gettext |
e4f24593a758
Adding support for extending internationalization to extensions
laurent
parents:
814
diff
changeset
|
76 |
import __builtin__ |
e4f24593a758
Adding support for extending internationalization to extensions
laurent
parents:
814
diff
changeset
|
77 |
|
814 | 78 |
__builtin__.__dict__['_'] = wx.GetTranslation |
79 |
||
80 |
from IDEFrame import IDEFrame, AppendMenu |
|
81 |
from IDEFrame import TITLE, EDITORTOOLBAR, FILEMENU, EDITMENU, DISPLAYMENU, PROJECTTREE, POUINSTANCEVARIABLESPANEL, LIBRARYTREE, PAGETITLES |
|
82 |
from IDEFrame import EncodeFileSystemPath, DecodeFileSystemPath |
|
83 |
from editors.Viewer import Viewer |
|
84 |
from PLCControler import PLCControler |
|
85 |
||
86 |
#------------------------------------------------------------------------------- |
|
87 |
# PLCOpenEditor Main Class |
|
88 |
#------------------------------------------------------------------------------- |
|
89 |
||
90 |
# Define PLCOpenEditor FileMenu extra items id |
|
91 |
[ID_PLCOPENEDITORFILEMENUGENERATE, |
|
92 |
] = [wx.NewId() for _init_coll_FileMenu_Items in range(1)] |
|
93 |
||
94 |
class PLCOpenEditor(IDEFrame): |
|
95 |
||
96 |
# Compatibility function for wx versions < 2.6 |
|
97 |
if wx.VERSION < (2, 6, 0): |
|
98 |
def Bind(self, event, function, id = None): |
|
99 |
if id is not None: |
|
100 |
event(self, id, function) |
|
101 |
else: |
|
102 |
event(self, function) |
|
103 |
||
104 |
def _init_coll_FileMenu_Items(self, parent): |
|
105 |
AppendMenu(parent, help='', id=wx.ID_NEW, |
|
106 |
kind=wx.ITEM_NORMAL, text=_(u'New') +'\tCTRL+N') |
|
107 |
AppendMenu(parent, help='', id=wx.ID_OPEN, |
|
108 |
kind=wx.ITEM_NORMAL, text=_(u'Open') + '\tCTRL+O') |
|
109 |
AppendMenu(parent, help='', id=wx.ID_CLOSE, |
|
110 |
kind=wx.ITEM_NORMAL, text=_(u'Close Tab') + '\tCTRL+W') |
|
111 |
AppendMenu(parent, help='', id=wx.ID_CLOSE_ALL, |
|
112 |
kind=wx.ITEM_NORMAL, text=_(u'Close Project') + '\tCTRL+SHIFT+W') |
|
113 |
parent.AppendSeparator() |
|
114 |
AppendMenu(parent, help='', id=wx.ID_SAVE, |
|
115 |
kind=wx.ITEM_NORMAL, text=_(u'Save') + '\tCTRL+S') |
|
116 |
AppendMenu(parent, help='', id=wx.ID_SAVEAS, |
|
117 |
kind=wx.ITEM_NORMAL, text=_(u'Save As...') + '\tCTRL+SHIFT+S') |
|
118 |
AppendMenu(parent, help='', id=ID_PLCOPENEDITORFILEMENUGENERATE, |
|
119 |
kind=wx.ITEM_NORMAL, text=_(u'Generate Program') + '\tCTRL+G') |
|
120 |
parent.AppendSeparator() |
|
121 |
AppendMenu(parent, help='', id=wx.ID_PAGE_SETUP, |
|
122 |
kind=wx.ITEM_NORMAL, text=_(u'Page Setup') + '\tCTRL+ALT+P') |
|
123 |
AppendMenu(parent, help='', id=wx.ID_PREVIEW, |
|
124 |
kind=wx.ITEM_NORMAL, text=_(u'Preview') + '\tCTRL+SHIFT+P') |
|
125 |
AppendMenu(parent, help='', id=wx.ID_PRINT, |
|
126 |
kind=wx.ITEM_NORMAL, text=_(u'Print') + '\tCTRL+P') |
|
127 |
parent.AppendSeparator() |
|
128 |
AppendMenu(parent, help='', id=wx.ID_PROPERTIES, |
|
129 |
kind=wx.ITEM_NORMAL, text=_(u'&Properties')) |
|
130 |
parent.AppendSeparator() |
|
131 |
AppendMenu(parent, help='', id=wx.ID_EXIT, |
|
132 |
kind=wx.ITEM_NORMAL, text=_(u'Quit') + '\tCTRL+Q') |
|
133 |
||
134 |
self.Bind(wx.EVT_MENU, self.OnNewProjectMenu, id=wx.ID_NEW) |
|
135 |
self.Bind(wx.EVT_MENU, self.OnOpenProjectMenu, id=wx.ID_OPEN) |
|
136 |
self.Bind(wx.EVT_MENU, self.OnCloseTabMenu, id=wx.ID_CLOSE) |
|
137 |
self.Bind(wx.EVT_MENU, self.OnCloseProjectMenu, id=wx.ID_CLOSE_ALL) |
|
138 |
self.Bind(wx.EVT_MENU, self.OnSaveProjectMenu, id=wx.ID_SAVE) |
|
139 |
self.Bind(wx.EVT_MENU, self.OnSaveProjectAsMenu, id=wx.ID_SAVEAS) |
|
140 |
self.Bind(wx.EVT_MENU, self.OnGenerateProgramMenu, |
|
141 |
id=ID_PLCOPENEDITORFILEMENUGENERATE) |
|
142 |
self.Bind(wx.EVT_MENU, self.OnPageSetupMenu, id=wx.ID_PAGE_SETUP) |
|
143 |
self.Bind(wx.EVT_MENU, self.OnPreviewMenu, id=wx.ID_PREVIEW) |
|
144 |
self.Bind(wx.EVT_MENU, self.OnPrintMenu, id=wx.ID_PRINT) |
|
145 |
self.Bind(wx.EVT_MENU, self.OnPropertiesMenu, id=wx.ID_PROPERTIES) |
|
146 |
self.Bind(wx.EVT_MENU, self.OnQuitMenu, id=wx.ID_EXIT) |
|
147 |
||
148 |
self.AddToMenuToolBar([(wx.ID_NEW, "new", _(u'New'), None), |
|
149 |
(wx.ID_OPEN, "open", _(u'Open'), None), |
|
150 |
(wx.ID_SAVE, "save", _(u'Save'), None), |
|
151 |
(wx.ID_SAVEAS, "saveas", _(u'Save As...'), None), |
|
152 |
(wx.ID_PRINT, "print", _(u'Print'), None)]) |
|
153 |
||
154 |
def _init_coll_HelpMenu_Items(self, parent): |
|
155 |
AppendMenu(parent, help='', id=wx.ID_HELP, |
|
156 |
kind=wx.ITEM_NORMAL, text=_(u'PLCOpenEditor') + '\tF1') |
|
157 |
#AppendMenu(parent, help='', id=wx.ID_HELP_CONTENTS, |
|
158 |
# kind=wx.ITEM_NORMAL, text=u'PLCOpen\tF2') |
|
159 |
#AppendMenu(parent, help='', id=wx.ID_HELP_CONTEXT, |
|
160 |
# kind=wx.ITEM_NORMAL, text=u'IEC 61131-3\tF3') |
|
161 |
AppendMenu(parent, help='', id=wx.ID_ABOUT, |
|
162 |
kind=wx.ITEM_NORMAL, text=_(u'About')) |
|
163 |
self.Bind(wx.EVT_MENU, self.OnPLCOpenEditorMenu, id=wx.ID_HELP) |
|
164 |
#self.Bind(wx.EVT_MENU, self.OnPLCOpenMenu, id=wx.ID_HELP_CONTENTS) |
|
165 |
self.Bind(wx.EVT_MENU, self.OnAboutMenu, id=wx.ID_ABOUT) |
|
166 |
||
167 |
## Constructor of the PLCOpenEditor class. |
|
168 |
# @param parent The parent window. |
|
169 |
# @param controler The controler been used by PLCOpenEditor (default: None). |
|
170 |
# @param fileOpen The filepath to open if no controler defined (default: None). |
|
171 |
# @param debug The filepath to open if no controler defined (default: False). |
|
172 |
def __init__(self, parent, fileOpen = None): |
|
173 |
IDEFrame.__init__(self, parent) |
|
174 |
||
175 |
result = None |
|
176 |
||
177 |
# Open the filepath if defined |
|
178 |
if fileOpen is not None: |
|
179 |
fileOpen = DecodeFileSystemPath(fileOpen, False) |
|
180 |
if os.path.isfile(fileOpen): |
|
181 |
# Create a new controller |
|
182 |
controler = PLCControler() |
|
183 |
result = controler.OpenXMLFile(fileOpen) |
|
184 |
if result is None: |
|
185 |
self.Controler = controler |
|
186 |
self.LibraryPanel.SetController(controler) |
|
187 |
self.ProjectTree.Enable(True) |
|
188 |
self.PouInstanceVariablesPanel.SetController(controler) |
|
189 |
self._Refresh(PROJECTTREE, POUINSTANCEVARIABLESPANEL, LIBRARYTREE) |
|
190 |
||
191 |
# Define PLCOpenEditor icon |
|
192 |
self.SetIcon(wx.Icon(os.path.join(CWD, "images", "poe.ico"),wx.BITMAP_TYPE_ICO)) |
|
193 |
||
194 |
self.Bind(wx.EVT_CLOSE, self.OnCloseFrame) |
|
195 |
||
196 |
self._Refresh(TITLE, EDITORTOOLBAR, FILEMENU, EDITMENU, DISPLAYMENU) |
|
197 |
||
198 |
if result is not None: |
|
199 |
self.ShowErrorMessage(result) |
|
200 |
||
201 |
def OnCloseFrame(self, event): |
|
202 |
if self.Controler is None or self.CheckSaveBeforeClosing(_("Close Application")): |
|
203 |
self.AUIManager.UnInit() |
|
204 |
||
205 |
self.SaveLastState() |
|
206 |
||
207 |
event.Skip() |
|
208 |
else: |
|
209 |
event.Veto() |
|
210 |
||
211 |
def RefreshTitle(self): |
|
212 |
name = _("PLCOpenEditor") |
|
213 |
if self.Controler is not None: |
|
214 |
self.SetTitle("%s - %s"%(name, self.Controler.GetFilename())) |
|
215 |
else: |
|
216 |
self.SetTitle(name) |
|
217 |
||
218 |
#------------------------------------------------------------------------------- |
|
219 |
# File Menu Functions |
|
220 |
#------------------------------------------------------------------------------- |
|
221 |
||
222 |
def RefreshFileMenu(self): |
|
223 |
MenuToolBar = self.Panes["MenuToolBar"] |
|
224 |
if self.Controler is not None: |
|
225 |
selected = self.TabsOpened.GetSelection() |
|
226 |
if selected >= 0: |
|
227 |
graphic_viewer = isinstance(self.TabsOpened.GetPage(selected), Viewer) |
|
228 |
else: |
|
229 |
graphic_viewer = False |
|
230 |
if self.TabsOpened.GetPageCount() > 0: |
|
231 |
self.FileMenu.Enable(wx.ID_CLOSE, True) |
|
232 |
if graphic_viewer: |
|
233 |
self.FileMenu.Enable(wx.ID_PREVIEW, True) |
|
234 |
self.FileMenu.Enable(wx.ID_PRINT, True) |
|
235 |
MenuToolBar.EnableTool(wx.ID_PRINT, True) |
|
236 |
else: |
|
237 |
self.FileMenu.Enable(wx.ID_PREVIEW, False) |
|
238 |
self.FileMenu.Enable(wx.ID_PRINT, False) |
|
239 |
MenuToolBar.EnableTool(wx.ID_PRINT, False) |
|
240 |
else: |
|
241 |
self.FileMenu.Enable(wx.ID_CLOSE, False) |
|
242 |
self.FileMenu.Enable(wx.ID_PREVIEW, False) |
|
243 |
self.FileMenu.Enable(wx.ID_PRINT, False) |
|
244 |
MenuToolBar.EnableTool(wx.ID_PRINT, False) |
|
245 |
self.FileMenu.Enable(wx.ID_PAGE_SETUP, True) |
|
246 |
project_modified = not self.Controler.ProjectIsSaved() |
|
247 |
self.FileMenu.Enable(wx.ID_SAVE, project_modified) |
|
248 |
MenuToolBar.EnableTool(wx.ID_SAVE, project_modified) |
|
249 |
self.FileMenu.Enable(wx.ID_PROPERTIES, True) |
|
250 |
self.FileMenu.Enable(wx.ID_CLOSE_ALL, True) |
|
251 |
self.FileMenu.Enable(wx.ID_SAVEAS, True) |
|
252 |
MenuToolBar.EnableTool(wx.ID_SAVEAS, True) |
|
253 |
self.FileMenu.Enable(ID_PLCOPENEDITORFILEMENUGENERATE, True) |
|
254 |
else: |
|
255 |
self.FileMenu.Enable(wx.ID_CLOSE, False) |
|
256 |
self.FileMenu.Enable(wx.ID_PAGE_SETUP, False) |
|
257 |
self.FileMenu.Enable(wx.ID_PREVIEW, False) |
|
258 |
self.FileMenu.Enable(wx.ID_PRINT, False) |
|
259 |
MenuToolBar.EnableTool(wx.ID_PRINT, False) |
|
260 |
self.FileMenu.Enable(wx.ID_SAVE, False) |
|
261 |
MenuToolBar.EnableTool(wx.ID_SAVE, False) |
|
262 |
self.FileMenu.Enable(wx.ID_PROPERTIES, False) |
|
263 |
self.FileMenu.Enable(wx.ID_CLOSE_ALL, False) |
|
264 |
self.FileMenu.Enable(wx.ID_SAVEAS, False) |
|
265 |
MenuToolBar.EnableTool(wx.ID_SAVEAS, False) |
|
266 |
self.FileMenu.Enable(ID_PLCOPENEDITORFILEMENUGENERATE, False) |
|
267 |
||
268 |
def OnNewProjectMenu(self, event): |
|
269 |
if self.Controler is not None and not self.CheckSaveBeforeClosing(): |
|
270 |
return |
|
271 |
dialog = ProjectDialog(self) |
|
272 |
if dialog.ShowModal() == wx.ID_OK: |
|
273 |
properties = dialog.GetValues() |
|
274 |
self.ResetView() |
|
275 |
self.Controler = PLCControler() |
|
276 |
self.Controler.CreateNewProject(properties) |
|
277 |
self.LibraryPanel.SetController(self.Controler) |
|
278 |
self._Refresh(TITLE, FILEMENU, EDITMENU, PROJECTTREE, POUINSTANCEVARIABLESPANEL, |
|
279 |
LIBRARYTREE) |
|
280 |
||
281 |
def OnOpenProjectMenu(self, event): |
|
282 |
if self.Controler is not None and not self.CheckSaveBeforeClosing(): |
|
283 |
return |
|
284 |
filepath = "" |
|
285 |
if self.Controler is not None: |
|
286 |
filepath = self.Controler.GetFilePath() |
|
287 |
if filepath != "": |
|
288 |
directory = os.path.dirname(filepath) |
|
289 |
else: |
|
290 |
directory = os.getcwd() |
|
291 |
||
292 |
result = None |
|
293 |
||
294 |
dialog = wx.FileDialog(self, _("Choose a file"), directory, "", _("PLCOpen files (*.xml)|*.xml|All files|*.*"), wx.OPEN) |
|
295 |
if dialog.ShowModal() == wx.ID_OK: |
|
296 |
filepath = dialog.GetPath() |
|
297 |
if os.path.isfile(filepath): |
|
298 |
self.ResetView() |
|
299 |
controler = PLCControler() |
|
300 |
result = controler.OpenXMLFile(filepath) |
|
301 |
if result is None: |
|
302 |
self.Controler = controler |
|
303 |
self.LibraryPanel.SetController(controler) |
|
304 |
self.ProjectTree.Enable(True) |
|
305 |
self.PouInstanceVariablesPanel.SetController(controler) |
|
306 |
self._Refresh(PROJECTTREE, LIBRARYTREE) |
|
307 |
self._Refresh(TITLE, EDITORTOOLBAR, FILEMENU, EDITMENU) |
|
308 |
dialog.Destroy() |
|
309 |
||
310 |
if result is not None: |
|
311 |
self.ShowErrorMessage(result) |
|
312 |
||
313 |
def OnCloseProjectMenu(self, event): |
|
314 |
if not self.CheckSaveBeforeClosing(): |
|
315 |
return |
|
316 |
self.ResetView() |
|
317 |
self._Refresh(TITLE, EDITORTOOLBAR, FILEMENU, EDITMENU) |
|
318 |
||
319 |
def OnSaveProjectMenu(self, event): |
|
320 |
self.SaveProject() |
|
321 |
||
322 |
def OnSaveProjectAsMenu(self, event): |
|
323 |
self.SaveProjectAs() |
|
324 |
||
325 |
def OnGenerateProgramMenu(self, event): |
|
326 |
dialog = wx.FileDialog(self, _("Choose a file"), os.getcwd(), self.Controler.GetProgramFilePath(), _("ST files (*.st)|*.st|All files|*.*"), wx.SAVE|wx.CHANGE_DIR) |
|
327 |
if dialog.ShowModal() == wx.ID_OK: |
|
328 |
filepath = dialog.GetPath() |
|
329 |
message_text = "" |
|
330 |
header, icon = _("Done"), wx.ICON_INFORMATION |
|
331 |
if os.path.isdir(os.path.dirname(filepath)): |
|
332 |
program, errors, warnings = self.Controler.GenerateProgram(filepath) |
|
333 |
message_text += "".join([_("warning: %s\n") for warning in warnings]) |
|
334 |
if len(errors) > 0: |
|
335 |
message_text += "".join([_("error: %s\n") for error in errors]) |
|
336 |
message_text += _("Can't generate program to file %s!")%filepath |
|
337 |
header, icon = _("Error"), wx.ICON_ERROR |
|
338 |
else: |
|
339 |
message_text += _("Program was successfully generated!") |
|
340 |
else: |
|
341 |
message_text += _("\"%s\" is not a valid folder!")%os.path.dirname(filepath) |
|
342 |
header, icon = _("Error"), wx.ICON_ERROR |
|
343 |
message = wx.MessageDialog(self, message_text, header, wx.OK|icon) |
|
344 |
message.ShowModal() |
|
345 |
message.Destroy() |
|
346 |
dialog.Destroy() |
|
347 |
||
348 |
def OnPLCOpenEditorMenu(self, event): |
|
349 |
wx.MessageBox(_("No documentation available.\nComing soon.")) |
|
350 |
||
351 |
def OnPLCOpenMenu(self, event): |
|
352 |
open_pdf(os.path.join(CWD, "plcopen", "TC6_XML_V101.pdf")) |
|
353 |
||
354 |
def OnAboutMenu(self, event): |
|
355 |
OpenHtmlFrame(self,_("About PLCOpenEditor"), os.path.join(CWD, "doc", "plcopen_about.html"), wx.Size(350, 350)) |
|
356 |
||
357 |
def SaveProject(self): |
|
358 |
result = self.Controler.SaveXMLFile() |
|
359 |
if not result: |
|
360 |
self.SaveProjectAs() |
|
361 |
else: |
|
362 |
self._Refresh(TITLE, FILEMENU, PAGETITLES) |
|
363 |
||
364 |
def SaveProjectAs(self): |
|
365 |
filepath = self.Controler.GetFilePath() |
|
366 |
if filepath != "": |
|
367 |
directory, filename = os.path.split(filepath) |
|
368 |
else: |
|
369 |
directory, filename = os.getcwd(), "%(projectName)s.xml"%self.Controler.GetProjectProperties() |
|
370 |
dialog = wx.FileDialog(self, _("Choose a file"), directory, filename, _("PLCOpen files (*.xml)|*.xml|All files|*.*"), wx.SAVE|wx.OVERWRITE_PROMPT) |
|
371 |
if dialog.ShowModal() == wx.ID_OK: |
|
372 |
filepath = dialog.GetPath() |
|
373 |
if os.path.isdir(os.path.dirname(filepath)): |
|
374 |
result = self.Controler.SaveXMLFile(filepath) |
|
375 |
if not result: |
|
376 |
self.ShowErrorMessage(_("Can't save project to file %s!")%filepath) |
|
377 |
else: |
|
378 |
self.ShowErrorMessage(_("\"%s\" is not a valid folder!")%os.path.dirname(filepath)) |
|
379 |
self._Refresh(TITLE, FILEMENU, PAGETITLES) |
|
380 |
dialog.Destroy() |
|
381 |
||
382 |
#------------------------------------------------------------------------------- |
|
383 |
# Exception Handler |
|
384 |
#------------------------------------------------------------------------------- |
|
385 |
||
386 |
Max_Traceback_List_Size = 20 |
|
387 |
||
388 |
def Display_Exception_Dialog(e_type,e_value,e_tb): |
|
389 |
trcbck_lst = [] |
|
390 |
for i,line in enumerate(traceback.extract_tb(e_tb)): |
|
391 |
trcbck = " " + str(i+1) + _(". ") |
|
392 |
if line[0].find(os.getcwd()) == -1: |
|
393 |
trcbck += _("file : ") + str(line[0]) + _(", ") |
|
394 |
else: |
|
395 |
trcbck += _("file : ") + str(line[0][len(os.getcwd()):]) + _(", ") |
|
396 |
trcbck += _("line : ") + str(line[1]) + _(", ") + _("function : ") + str(line[2]) |
|
397 |
trcbck_lst.append(trcbck) |
|
398 |
||
399 |
# Allow clicking.... |
|
400 |
cap = wx.Window_GetCapture() |
|
401 |
if cap: |
|
402 |
cap.ReleaseMouse() |
|
403 |
||
404 |
dlg = wx.SingleChoiceDialog(None, |
|
405 |
_(""" |
|
406 |
An error has occurred. |
|
407 |
||
408 |
Click OK to save an error report. |
|
409 |
||
410 |
Please be kind enough to send this file to: |
|
411 |
edouard.tisserant@gmail.com |
|
412 |
||
413 |
Error: |
|
414 |
""") + |
|
415 |
str(e_type) + _(" : ") + str(e_value), |
|
416 |
_("Error"), |
|
417 |
trcbck_lst) |
|
418 |
try: |
|
419 |
res = (dlg.ShowModal() == wx.ID_OK) |
|
420 |
finally: |
|
421 |
dlg.Destroy() |
|
422 |
||
423 |
return res |
|
424 |
||
425 |
def Display_Error_Dialog(e_value): |
|
426 |
message = wx.MessageDialog(None, str(e_value), _("Error"), wx.OK|wx.ICON_ERROR) |
|
427 |
message.ShowModal() |
|
428 |
message.Destroy() |
|
429 |
||
430 |
def get_last_traceback(tb): |
|
431 |
while tb.tb_next: |
|
432 |
tb = tb.tb_next |
|
433 |
return tb |
|
434 |
||
435 |
||
436 |
def format_namespace(d, indent=' '): |
|
437 |
return '\n'.join(['%s%s: %s' % (indent, k, repr(v)[:10000]) for k, v in d.iteritems()]) |
|
438 |
||
439 |
||
440 |
ignored_exceptions = [] # a problem with a line in a module is only reported once per session |
|
441 |
||
442 |
def AddExceptHook(path, app_version='[No version]'):#, ignored_exceptions=[]): |
|
443 |
||
444 |
def handle_exception(e_type, e_value, e_traceback): |
|
445 |
traceback.print_exception(e_type, e_value, e_traceback) # this is very helpful when there's an exception in the rest of this func |
|
446 |
last_tb = get_last_traceback(e_traceback) |
|
447 |
ex = (last_tb.tb_frame.f_code.co_filename, last_tb.tb_frame.f_lineno) |
|
448 |
if str(e_value).startswith("!!!"): |
|
449 |
Display_Error_Dialog(e_value) |
|
450 |
elif ex not in ignored_exceptions: |
|
451 |
result = Display_Exception_Dialog(e_type,e_value,e_traceback) |
|
452 |
if result: |
|
453 |
ignored_exceptions.append(ex) |
|
454 |
info = { |
|
455 |
'app-title' : wx.GetApp().GetAppName(), # app_title |
|
456 |
'app-version' : app_version, |
|
457 |
'wx-version' : wx.VERSION_STRING, |
|
458 |
'wx-platform' : wx.Platform, |
|
459 |
'python-version' : platform.python_version(), #sys.version.split()[0], |
|
460 |
'platform' : platform.platform(), |
|
461 |
'e-type' : e_type, |
|
462 |
'e-value' : e_value, |
|
463 |
'date' : time.ctime(), |
|
464 |
'cwd' : os.getcwd(), |
|
465 |
} |
|
466 |
if e_traceback: |
|
467 |
info['traceback'] = ''.join(traceback.format_tb(e_traceback)) + '%s: %s' % (e_type, e_value) |
|
468 |
last_tb = get_last_traceback(e_traceback) |
|
469 |
exception_locals = last_tb.tb_frame.f_locals # the locals at the level of the stack trace where the exception actually occurred |
|
470 |
info['locals'] = format_namespace(exception_locals) |
|
471 |
if 'self' in exception_locals: |
|
472 |
info['self'] = format_namespace(exception_locals['self'].__dict__) |
|
473 |
||
474 |
output = open(path+os.sep+"bug_report_"+info['date'].replace(':','-').replace(' ','_')+".txt",'w') |
|
475 |
lst = info.keys() |
|
476 |
lst.sort() |
|
477 |
for a in lst: |
|
478 |
output.write(a+":\n"+str(info[a])+"\n\n") |
|
479 |
||
480 |
#sys.excepthook = lambda *args: wx.CallAfter(handle_exception, *args) |
|
481 |
sys.excepthook = handle_exception |
|
482 |
||
483 |
if __name__ == '__main__': |
|
484 |
wx.InitAllImageHandlers() |
|
485 |
||
486 |
# Install a exception handle for bug reports |
|
487 |
AddExceptHook(os.getcwd(),__version__) |
|
488 |
||
489 |
frame = PLCOpenEditor(None, fileOpen=fileOpen) |
|
490 |
||
491 |
frame.Show() |
|
492 |
app.MainLoop() |
|
493 |