author | Edouard Tisserant |
Wed, 31 Jul 2013 10:45:07 +0900 | |
branch | 1.1 Korean release |
changeset 1280 | 72a826dfcfbb |
parent 1030 | d9e75a5b5f08 |
child 1295 | 2ad168756c5e |
permissions | -rwxr-xr-x |
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 |
|
1030
d9e75a5b5f08
Fix bug when creating New project in PLCOpenEditor
Laurent Bessard
parents:
980
diff
changeset
|
85 |
from dialogs import ProjectDialog |
814 | 86 |
|
87 |
#------------------------------------------------------------------------------- |
|
88 |
# PLCOpenEditor Main Class |
|
89 |
#------------------------------------------------------------------------------- |
|
90 |
||
91 |
# Define PLCOpenEditor FileMenu extra items id |
|
92 |
[ID_PLCOPENEDITORFILEMENUGENERATE, |
|
93 |
] = [wx.NewId() for _init_coll_FileMenu_Items in range(1)] |
|
94 |
||
95 |
class PLCOpenEditor(IDEFrame): |
|
96 |
||
97 |
# Compatibility function for wx versions < 2.6 |
|
98 |
if wx.VERSION < (2, 6, 0): |
|
99 |
def Bind(self, event, function, id = None): |
|
100 |
if id is not None: |
|
101 |
event(self, id, function) |
|
102 |
else: |
|
103 |
event(self, function) |
|
104 |
||
105 |
def _init_coll_FileMenu_Items(self, parent): |
|
106 |
AppendMenu(parent, help='', id=wx.ID_NEW, |
|
107 |
kind=wx.ITEM_NORMAL, text=_(u'New') +'\tCTRL+N') |
|
108 |
AppendMenu(parent, help='', id=wx.ID_OPEN, |
|
109 |
kind=wx.ITEM_NORMAL, text=_(u'Open') + '\tCTRL+O') |
|
110 |
AppendMenu(parent, help='', id=wx.ID_CLOSE, |
|
111 |
kind=wx.ITEM_NORMAL, text=_(u'Close Tab') + '\tCTRL+W') |
|
112 |
AppendMenu(parent, help='', id=wx.ID_CLOSE_ALL, |
|
113 |
kind=wx.ITEM_NORMAL, text=_(u'Close Project') + '\tCTRL+SHIFT+W') |
|
114 |
parent.AppendSeparator() |
|
115 |
AppendMenu(parent, help='', id=wx.ID_SAVE, |
|
116 |
kind=wx.ITEM_NORMAL, text=_(u'Save') + '\tCTRL+S') |
|
117 |
AppendMenu(parent, help='', id=wx.ID_SAVEAS, |
|
118 |
kind=wx.ITEM_NORMAL, text=_(u'Save As...') + '\tCTRL+SHIFT+S') |
|
119 |
AppendMenu(parent, help='', id=ID_PLCOPENEDITORFILEMENUGENERATE, |
|
120 |
kind=wx.ITEM_NORMAL, text=_(u'Generate Program') + '\tCTRL+G') |
|
121 |
parent.AppendSeparator() |
|
122 |
AppendMenu(parent, help='', id=wx.ID_PAGE_SETUP, |
|
123 |
kind=wx.ITEM_NORMAL, text=_(u'Page Setup') + '\tCTRL+ALT+P') |
|
124 |
AppendMenu(parent, help='', id=wx.ID_PREVIEW, |
|
125 |
kind=wx.ITEM_NORMAL, text=_(u'Preview') + '\tCTRL+SHIFT+P') |
|
126 |
AppendMenu(parent, help='', id=wx.ID_PRINT, |
|
127 |
kind=wx.ITEM_NORMAL, text=_(u'Print') + '\tCTRL+P') |
|
128 |
parent.AppendSeparator() |
|
129 |
AppendMenu(parent, help='', id=wx.ID_PROPERTIES, |
|
130 |
kind=wx.ITEM_NORMAL, text=_(u'&Properties')) |
|
131 |
parent.AppendSeparator() |
|
132 |
AppendMenu(parent, help='', id=wx.ID_EXIT, |
|
133 |
kind=wx.ITEM_NORMAL, text=_(u'Quit') + '\tCTRL+Q') |
|
134 |
||
135 |
self.Bind(wx.EVT_MENU, self.OnNewProjectMenu, id=wx.ID_NEW) |
|
136 |
self.Bind(wx.EVT_MENU, self.OnOpenProjectMenu, id=wx.ID_OPEN) |
|
137 |
self.Bind(wx.EVT_MENU, self.OnCloseTabMenu, id=wx.ID_CLOSE) |
|
138 |
self.Bind(wx.EVT_MENU, self.OnCloseProjectMenu, id=wx.ID_CLOSE_ALL) |
|
139 |
self.Bind(wx.EVT_MENU, self.OnSaveProjectMenu, id=wx.ID_SAVE) |
|
140 |
self.Bind(wx.EVT_MENU, self.OnSaveProjectAsMenu, id=wx.ID_SAVEAS) |
|
141 |
self.Bind(wx.EVT_MENU, self.OnGenerateProgramMenu, |
|
142 |
id=ID_PLCOPENEDITORFILEMENUGENERATE) |
|
143 |
self.Bind(wx.EVT_MENU, self.OnPageSetupMenu, id=wx.ID_PAGE_SETUP) |
|
144 |
self.Bind(wx.EVT_MENU, self.OnPreviewMenu, id=wx.ID_PREVIEW) |
|
145 |
self.Bind(wx.EVT_MENU, self.OnPrintMenu, id=wx.ID_PRINT) |
|
146 |
self.Bind(wx.EVT_MENU, self.OnPropertiesMenu, id=wx.ID_PROPERTIES) |
|
147 |
self.Bind(wx.EVT_MENU, self.OnQuitMenu, id=wx.ID_EXIT) |
|
148 |
||
149 |
self.AddToMenuToolBar([(wx.ID_NEW, "new", _(u'New'), None), |
|
150 |
(wx.ID_OPEN, "open", _(u'Open'), None), |
|
151 |
(wx.ID_SAVE, "save", _(u'Save'), None), |
|
152 |
(wx.ID_SAVEAS, "saveas", _(u'Save As...'), None), |
|
153 |
(wx.ID_PRINT, "print", _(u'Print'), None)]) |
|
154 |
||
155 |
def _init_coll_HelpMenu_Items(self, parent): |
|
156 |
AppendMenu(parent, help='', id=wx.ID_HELP, |
|
157 |
kind=wx.ITEM_NORMAL, text=_(u'PLCOpenEditor') + '\tF1') |
|
158 |
#AppendMenu(parent, help='', id=wx.ID_HELP_CONTENTS, |
|
159 |
# kind=wx.ITEM_NORMAL, text=u'PLCOpen\tF2') |
|
160 |
#AppendMenu(parent, help='', id=wx.ID_HELP_CONTEXT, |
|
161 |
# kind=wx.ITEM_NORMAL, text=u'IEC 61131-3\tF3') |
|
162 |
AppendMenu(parent, help='', id=wx.ID_ABOUT, |
|
163 |
kind=wx.ITEM_NORMAL, text=_(u'About')) |
|
164 |
self.Bind(wx.EVT_MENU, self.OnPLCOpenEditorMenu, id=wx.ID_HELP) |
|
165 |
#self.Bind(wx.EVT_MENU, self.OnPLCOpenMenu, id=wx.ID_HELP_CONTENTS) |
|
166 |
self.Bind(wx.EVT_MENU, self.OnAboutMenu, id=wx.ID_ABOUT) |
|
167 |
||
168 |
## Constructor of the PLCOpenEditor class. |
|
169 |
# @param parent The parent window. |
|
170 |
# @param controler The controler been used by PLCOpenEditor (default: None). |
|
171 |
# @param fileOpen The filepath to open if no controler defined (default: None). |
|
172 |
# @param debug The filepath to open if no controler defined (default: False). |
|
173 |
def __init__(self, parent, fileOpen = None): |
|
174 |
IDEFrame.__init__(self, parent) |
|
175 |
||
176 |
result = None |
|
177 |
||
178 |
# Open the filepath if defined |
|
179 |
if fileOpen is not None: |
|
180 |
fileOpen = DecodeFileSystemPath(fileOpen, False) |
|
181 |
if os.path.isfile(fileOpen): |
|
182 |
# Create a new controller |
|
183 |
controler = PLCControler() |
|
184 |
result = controler.OpenXMLFile(fileOpen) |
|
185 |
if result is None: |
|
186 |
self.Controler = controler |
|
187 |
self.LibraryPanel.SetController(controler) |
|
188 |
self.ProjectTree.Enable(True) |
|
189 |
self.PouInstanceVariablesPanel.SetController(controler) |
|
190 |
self._Refresh(PROJECTTREE, POUINSTANCEVARIABLESPANEL, LIBRARYTREE) |
|
191 |
||
192 |
# Define PLCOpenEditor icon |
|
193 |
self.SetIcon(wx.Icon(os.path.join(CWD, "images", "poe.ico"),wx.BITMAP_TYPE_ICO)) |
|
194 |
||
195 |
self.Bind(wx.EVT_CLOSE, self.OnCloseFrame) |
|
196 |
||
197 |
self._Refresh(TITLE, EDITORTOOLBAR, FILEMENU, EDITMENU, DISPLAYMENU) |
|
198 |
||
199 |
if result is not None: |
|
200 |
self.ShowErrorMessage(result) |
|
201 |
||
202 |
def OnCloseFrame(self, event): |
|
203 |
if self.Controler is None or self.CheckSaveBeforeClosing(_("Close Application")): |
|
204 |
self.AUIManager.UnInit() |
|
205 |
||
206 |
self.SaveLastState() |
|
207 |
||
208 |
event.Skip() |
|
209 |
else: |
|
210 |
event.Veto() |
|
211 |
||
212 |
def RefreshTitle(self): |
|
213 |
name = _("PLCOpenEditor") |
|
214 |
if self.Controler is not None: |
|
215 |
self.SetTitle("%s - %s"%(name, self.Controler.GetFilename())) |
|
216 |
else: |
|
217 |
self.SetTitle(name) |
|
218 |
||
219 |
#------------------------------------------------------------------------------- |
|
220 |
# File Menu Functions |
|
221 |
#------------------------------------------------------------------------------- |
|
222 |
||
223 |
def RefreshFileMenu(self): |
|
224 |
MenuToolBar = self.Panes["MenuToolBar"] |
|
225 |
if self.Controler is not None: |
|
226 |
selected = self.TabsOpened.GetSelection() |
|
227 |
if selected >= 0: |
|
228 |
graphic_viewer = isinstance(self.TabsOpened.GetPage(selected), Viewer) |
|
229 |
else: |
|
230 |
graphic_viewer = False |
|
231 |
if self.TabsOpened.GetPageCount() > 0: |
|
232 |
self.FileMenu.Enable(wx.ID_CLOSE, True) |
|
233 |
if graphic_viewer: |
|
234 |
self.FileMenu.Enable(wx.ID_PREVIEW, True) |
|
235 |
self.FileMenu.Enable(wx.ID_PRINT, True) |
|
236 |
MenuToolBar.EnableTool(wx.ID_PRINT, True) |
|
237 |
else: |
|
238 |
self.FileMenu.Enable(wx.ID_PREVIEW, False) |
|
239 |
self.FileMenu.Enable(wx.ID_PRINT, False) |
|
240 |
MenuToolBar.EnableTool(wx.ID_PRINT, False) |
|
241 |
else: |
|
242 |
self.FileMenu.Enable(wx.ID_CLOSE, False) |
|
243 |
self.FileMenu.Enable(wx.ID_PREVIEW, False) |
|
244 |
self.FileMenu.Enable(wx.ID_PRINT, False) |
|
245 |
MenuToolBar.EnableTool(wx.ID_PRINT, False) |
|
246 |
self.FileMenu.Enable(wx.ID_PAGE_SETUP, True) |
|
247 |
project_modified = not self.Controler.ProjectIsSaved() |
|
248 |
self.FileMenu.Enable(wx.ID_SAVE, project_modified) |
|
249 |
MenuToolBar.EnableTool(wx.ID_SAVE, project_modified) |
|
250 |
self.FileMenu.Enable(wx.ID_PROPERTIES, True) |
|
251 |
self.FileMenu.Enable(wx.ID_CLOSE_ALL, True) |
|
252 |
self.FileMenu.Enable(wx.ID_SAVEAS, True) |
|
253 |
MenuToolBar.EnableTool(wx.ID_SAVEAS, True) |
|
254 |
self.FileMenu.Enable(ID_PLCOPENEDITORFILEMENUGENERATE, True) |
|
255 |
else: |
|
256 |
self.FileMenu.Enable(wx.ID_CLOSE, False) |
|
257 |
self.FileMenu.Enable(wx.ID_PAGE_SETUP, False) |
|
258 |
self.FileMenu.Enable(wx.ID_PREVIEW, False) |
|
259 |
self.FileMenu.Enable(wx.ID_PRINT, False) |
|
260 |
MenuToolBar.EnableTool(wx.ID_PRINT, False) |
|
261 |
self.FileMenu.Enable(wx.ID_SAVE, False) |
|
262 |
MenuToolBar.EnableTool(wx.ID_SAVE, False) |
|
263 |
self.FileMenu.Enable(wx.ID_PROPERTIES, False) |
|
264 |
self.FileMenu.Enable(wx.ID_CLOSE_ALL, False) |
|
265 |
self.FileMenu.Enable(wx.ID_SAVEAS, False) |
|
266 |
MenuToolBar.EnableTool(wx.ID_SAVEAS, False) |
|
267 |
self.FileMenu.Enable(ID_PLCOPENEDITORFILEMENUGENERATE, False) |
|
268 |
||
269 |
def OnNewProjectMenu(self, event): |
|
270 |
if self.Controler is not None and not self.CheckSaveBeforeClosing(): |
|
271 |
return |
|
272 |
dialog = ProjectDialog(self) |
|
273 |
if dialog.ShowModal() == wx.ID_OK: |
|
274 |
properties = dialog.GetValues() |
|
275 |
self.ResetView() |
|
276 |
self.Controler = PLCControler() |
|
277 |
self.Controler.CreateNewProject(properties) |
|
278 |
self.LibraryPanel.SetController(self.Controler) |
|
279 |
self._Refresh(TITLE, FILEMENU, EDITMENU, PROJECTTREE, POUINSTANCEVARIABLESPANEL, |
|
280 |
LIBRARYTREE) |
|
281 |
||
282 |
def OnOpenProjectMenu(self, event): |
|
283 |
if self.Controler is not None and not self.CheckSaveBeforeClosing(): |
|
284 |
return |
|
285 |
filepath = "" |
|
286 |
if self.Controler is not None: |
|
287 |
filepath = self.Controler.GetFilePath() |
|
288 |
if filepath != "": |
|
289 |
directory = os.path.dirname(filepath) |
|
290 |
else: |
|
291 |
directory = os.getcwd() |
|
292 |
||
293 |
result = None |
|
294 |
||
295 |
dialog = wx.FileDialog(self, _("Choose a file"), directory, "", _("PLCOpen files (*.xml)|*.xml|All files|*.*"), wx.OPEN) |
|
296 |
if dialog.ShowModal() == wx.ID_OK: |
|
297 |
filepath = dialog.GetPath() |
|
298 |
if os.path.isfile(filepath): |
|
299 |
self.ResetView() |
|
300 |
controler = PLCControler() |
|
301 |
result = controler.OpenXMLFile(filepath) |
|
302 |
if result is None: |
|
303 |
self.Controler = controler |
|
304 |
self.LibraryPanel.SetController(controler) |
|
305 |
self.ProjectTree.Enable(True) |
|
306 |
self.PouInstanceVariablesPanel.SetController(controler) |
|
307 |
self._Refresh(PROJECTTREE, LIBRARYTREE) |
|
308 |
self._Refresh(TITLE, EDITORTOOLBAR, FILEMENU, EDITMENU) |
|
309 |
dialog.Destroy() |
|
310 |
||
311 |
if result is not None: |
|
312 |
self.ShowErrorMessage(result) |
|
313 |
||
314 |
def OnCloseProjectMenu(self, event): |
|
315 |
if not self.CheckSaveBeforeClosing(): |
|
316 |
return |
|
317 |
self.ResetView() |
|
318 |
self._Refresh(TITLE, EDITORTOOLBAR, FILEMENU, EDITMENU) |
|
319 |
||
320 |
def OnSaveProjectMenu(self, event): |
|
321 |
self.SaveProject() |
|
322 |
||
323 |
def OnSaveProjectAsMenu(self, event): |
|
324 |
self.SaveProjectAs() |
|
325 |
||
326 |
def OnGenerateProgramMenu(self, event): |
|
327 |
dialog = wx.FileDialog(self, _("Choose a file"), os.getcwd(), self.Controler.GetProgramFilePath(), _("ST files (*.st)|*.st|All files|*.*"), wx.SAVE|wx.CHANGE_DIR) |
|
328 |
if dialog.ShowModal() == wx.ID_OK: |
|
329 |
filepath = dialog.GetPath() |
|
330 |
message_text = "" |
|
331 |
header, icon = _("Done"), wx.ICON_INFORMATION |
|
332 |
if os.path.isdir(os.path.dirname(filepath)): |
|
333 |
program, errors, warnings = self.Controler.GenerateProgram(filepath) |
|
334 |
message_text += "".join([_("warning: %s\n") for warning in warnings]) |
|
335 |
if len(errors) > 0: |
|
336 |
message_text += "".join([_("error: %s\n") for error in errors]) |
|
337 |
message_text += _("Can't generate program to file %s!")%filepath |
|
338 |
header, icon = _("Error"), wx.ICON_ERROR |
|
339 |
else: |
|
340 |
message_text += _("Program was successfully generated!") |
|
341 |
else: |
|
342 |
message_text += _("\"%s\" is not a valid folder!")%os.path.dirname(filepath) |
|
343 |
header, icon = _("Error"), wx.ICON_ERROR |
|
344 |
message = wx.MessageDialog(self, message_text, header, wx.OK|icon) |
|
345 |
message.ShowModal() |
|
346 |
message.Destroy() |
|
347 |
dialog.Destroy() |
|
348 |
||
349 |
def OnPLCOpenEditorMenu(self, event): |
|
350 |
wx.MessageBox(_("No documentation available.\nComing soon.")) |
|
351 |
||
352 |
def OnPLCOpenMenu(self, event): |
|
353 |
open_pdf(os.path.join(CWD, "plcopen", "TC6_XML_V101.pdf")) |
|
354 |
||
355 |
def OnAboutMenu(self, event): |
|
356 |
OpenHtmlFrame(self,_("About PLCOpenEditor"), os.path.join(CWD, "doc", "plcopen_about.html"), wx.Size(350, 350)) |
|
357 |
||
358 |
def SaveProject(self): |
|
359 |
result = self.Controler.SaveXMLFile() |
|
360 |
if not result: |
|
361 |
self.SaveProjectAs() |
|
362 |
else: |
|
363 |
self._Refresh(TITLE, FILEMENU, PAGETITLES) |
|
364 |
||
365 |
def SaveProjectAs(self): |
|
366 |
filepath = self.Controler.GetFilePath() |
|
367 |
if filepath != "": |
|
368 |
directory, filename = os.path.split(filepath) |
|
369 |
else: |
|
370 |
directory, filename = os.getcwd(), "%(projectName)s.xml"%self.Controler.GetProjectProperties() |
|
371 |
dialog = wx.FileDialog(self, _("Choose a file"), directory, filename, _("PLCOpen files (*.xml)|*.xml|All files|*.*"), wx.SAVE|wx.OVERWRITE_PROMPT) |
|
372 |
if dialog.ShowModal() == wx.ID_OK: |
|
373 |
filepath = dialog.GetPath() |
|
374 |
if os.path.isdir(os.path.dirname(filepath)): |
|
375 |
result = self.Controler.SaveXMLFile(filepath) |
|
376 |
if not result: |
|
377 |
self.ShowErrorMessage(_("Can't save project to file %s!")%filepath) |
|
378 |
else: |
|
379 |
self.ShowErrorMessage(_("\"%s\" is not a valid folder!")%os.path.dirname(filepath)) |
|
380 |
self._Refresh(TITLE, FILEMENU, PAGETITLES) |
|
381 |
dialog.Destroy() |
|
382 |
||
383 |
#------------------------------------------------------------------------------- |
|
384 |
# Exception Handler |
|
385 |
#------------------------------------------------------------------------------- |
|
386 |
||
387 |
Max_Traceback_List_Size = 20 |
|
388 |
||
389 |
def Display_Exception_Dialog(e_type,e_value,e_tb): |
|
390 |
trcbck_lst = [] |
|
391 |
for i,line in enumerate(traceback.extract_tb(e_tb)): |
|
392 |
trcbck = " " + str(i+1) + _(". ") |
|
393 |
if line[0].find(os.getcwd()) == -1: |
|
394 |
trcbck += _("file : ") + str(line[0]) + _(", ") |
|
395 |
else: |
|
396 |
trcbck += _("file : ") + str(line[0][len(os.getcwd()):]) + _(", ") |
|
397 |
trcbck += _("line : ") + str(line[1]) + _(", ") + _("function : ") + str(line[2]) |
|
398 |
trcbck_lst.append(trcbck) |
|
399 |
||
400 |
# Allow clicking.... |
|
401 |
cap = wx.Window_GetCapture() |
|
402 |
if cap: |
|
403 |
cap.ReleaseMouse() |
|
404 |
||
405 |
dlg = wx.SingleChoiceDialog(None, |
|
406 |
_(""" |
|
407 |
An error has occurred. |
|
408 |
||
409 |
Click OK to save an error report. |
|
410 |
||
411 |
Please be kind enough to send this file to: |
|
412 |
edouard.tisserant@gmail.com |
|
413 |
||
414 |
Error: |
|
415 |
""") + |
|
416 |
str(e_type) + _(" : ") + str(e_value), |
|
417 |
_("Error"), |
|
418 |
trcbck_lst) |
|
419 |
try: |
|
420 |
res = (dlg.ShowModal() == wx.ID_OK) |
|
421 |
finally: |
|
422 |
dlg.Destroy() |
|
423 |
||
424 |
return res |
|
425 |
||
426 |
def Display_Error_Dialog(e_value): |
|
427 |
message = wx.MessageDialog(None, str(e_value), _("Error"), wx.OK|wx.ICON_ERROR) |
|
428 |
message.ShowModal() |
|
429 |
message.Destroy() |
|
430 |
||
431 |
def get_last_traceback(tb): |
|
432 |
while tb.tb_next: |
|
433 |
tb = tb.tb_next |
|
434 |
return tb |
|
435 |
||
436 |
||
437 |
def format_namespace(d, indent=' '): |
|
438 |
return '\n'.join(['%s%s: %s' % (indent, k, repr(v)[:10000]) for k, v in d.iteritems()]) |
|
439 |
||
440 |
||
441 |
ignored_exceptions = [] # a problem with a line in a module is only reported once per session |
|
442 |
||
443 |
def AddExceptHook(path, app_version='[No version]'):#, ignored_exceptions=[]): |
|
444 |
||
445 |
def handle_exception(e_type, e_value, e_traceback): |
|
446 |
traceback.print_exception(e_type, e_value, e_traceback) # this is very helpful when there's an exception in the rest of this func |
|
447 |
last_tb = get_last_traceback(e_traceback) |
|
448 |
ex = (last_tb.tb_frame.f_code.co_filename, last_tb.tb_frame.f_lineno) |
|
449 |
if str(e_value).startswith("!!!"): |
|
450 |
Display_Error_Dialog(e_value) |
|
451 |
elif ex not in ignored_exceptions: |
|
452 |
result = Display_Exception_Dialog(e_type,e_value,e_traceback) |
|
453 |
if result: |
|
454 |
ignored_exceptions.append(ex) |
|
455 |
info = { |
|
456 |
'app-title' : wx.GetApp().GetAppName(), # app_title |
|
457 |
'app-version' : app_version, |
|
458 |
'wx-version' : wx.VERSION_STRING, |
|
459 |
'wx-platform' : wx.Platform, |
|
460 |
'python-version' : platform.python_version(), #sys.version.split()[0], |
|
461 |
'platform' : platform.platform(), |
|
462 |
'e-type' : e_type, |
|
463 |
'e-value' : e_value, |
|
464 |
'date' : time.ctime(), |
|
465 |
'cwd' : os.getcwd(), |
|
466 |
} |
|
467 |
if e_traceback: |
|
468 |
info['traceback'] = ''.join(traceback.format_tb(e_traceback)) + '%s: %s' % (e_type, e_value) |
|
469 |
last_tb = get_last_traceback(e_traceback) |
|
470 |
exception_locals = last_tb.tb_frame.f_locals # the locals at the level of the stack trace where the exception actually occurred |
|
471 |
info['locals'] = format_namespace(exception_locals) |
|
472 |
if 'self' in exception_locals: |
|
473 |
info['self'] = format_namespace(exception_locals['self'].__dict__) |
|
474 |
||
475 |
output = open(path+os.sep+"bug_report_"+info['date'].replace(':','-').replace(' ','_')+".txt",'w') |
|
476 |
lst = info.keys() |
|
477 |
lst.sort() |
|
478 |
for a in lst: |
|
479 |
output.write(a+":\n"+str(info[a])+"\n\n") |
|
480 |
||
481 |
#sys.excepthook = lambda *args: wx.CallAfter(handle_exception, *args) |
|
482 |
sys.excepthook = handle_exception |
|
483 |
||
484 |
if __name__ == '__main__': |
|
485 |
wx.InitAllImageHandlers() |
|
486 |
||
487 |
# Install a exception handle for bug reports |
|
488 |
AddExceptHook(os.getcwd(),__version__) |
|
489 |
||
490 |
frame = PLCOpenEditor(None, fileOpen=fileOpen) |
|
491 |
||
492 |
frame.Show() |
|
493 |
app.MainLoop() |
|
494 |