1 | """ |
---|
2 | Console Module display Python console |
---|
3 | """ |
---|
4 | import sys |
---|
5 | import os |
---|
6 | |
---|
7 | import numpy as np |
---|
8 | |
---|
9 | import wx |
---|
10 | from wx.lib.dialogs import ScrolledMessageDialog |
---|
11 | import wx.py.editor as editor |
---|
12 | |
---|
13 | if sys.platform.count("win32") > 0: |
---|
14 | PANEL_WIDTH = 800 |
---|
15 | PANEL_HEIGHT = 700 |
---|
16 | FONT_VARIANT = 0 |
---|
17 | else: |
---|
18 | PANEL_WIDTH = 830 |
---|
19 | PANEL_HEIGHT = 730 |
---|
20 | FONT_VARIANT = 1 |
---|
21 | ID_CHECK_MODEL = wx.NewId() |
---|
22 | ID_RUN = wx.NewId() |
---|
23 | |
---|
24 | def check_model(path): |
---|
25 | """ |
---|
26 | Check that the model on the path can run. |
---|
27 | """ |
---|
28 | # try running the model |
---|
29 | from sasmodels.sasview_model import load_custom_model |
---|
30 | Model = load_custom_model(path) |
---|
31 | model = Model() |
---|
32 | q = np.array([0.01, 0.1]) |
---|
33 | Iq = model.evalDistribution(q) |
---|
34 | qx, qy = np.array([0.01, 0.01]), np.array([0.1, 0.1]) |
---|
35 | Iqxy = model.evalDistribution([qx, qy]) |
---|
36 | |
---|
37 | result = """ |
---|
38 | Iq(%s) = %s |
---|
39 | Iqxy(%s, %s) = %s |
---|
40 | """%(q, Iq, qx, qy, Iqxy) |
---|
41 | |
---|
42 | return result |
---|
43 | |
---|
44 | def show_model_output(parent, fname): |
---|
45 | # Make sure we have a python file; not sure why we care though... |
---|
46 | if not (fname and os.path.exists(fname) and fname.endswith('.py')): |
---|
47 | mssg = "\n This is not a python file." |
---|
48 | wx.MessageBox(str(mssg), 'Error', style=wx.ICON_ERROR) |
---|
49 | return False |
---|
50 | |
---|
51 | try: |
---|
52 | result, errmsg = check_model(fname), None |
---|
53 | except Exception: |
---|
54 | import traceback |
---|
55 | result, errmsg = None, traceback.format_exc() |
---|
56 | |
---|
57 | parts = ["Running model '%s'..." % os.path.basename(fname)] |
---|
58 | if errmsg is not None: |
---|
59 | parts.extend(["", "Error occurred:", errmsg, ""]) |
---|
60 | title, icon = "Error", wx.ICON_ERROR |
---|
61 | else: |
---|
62 | parts.extend(["", "Success:", result, ""]) |
---|
63 | title, icon = "Info", wx.ICON_INFORMATION |
---|
64 | text = "\n".join(parts) |
---|
65 | dlg = ScrolledMessageDialog(parent, text, title, size=((550, 250))) |
---|
66 | fnt = wx.Font(10, wx.TELETYPE, wx.NORMAL, wx.NORMAL) |
---|
67 | dlg.GetChildren()[0].SetFont(fnt) |
---|
68 | dlg.GetChildren()[0].SetInsertionPoint(0) |
---|
69 | dlg.ShowModal() |
---|
70 | dlg.Destroy() |
---|
71 | return errmsg is None |
---|
72 | |
---|
73 | class PyConsole(editor.EditorNotebookFrame): |
---|
74 | ## Internal nickname for the window, used by the AUI manager |
---|
75 | window_name = "Custom Model Editor" |
---|
76 | ## Name to appear on the window title bar |
---|
77 | window_caption = "Custom Model Editor" |
---|
78 | ## Flag to tell the AUI manager to put this panel in the center pane |
---|
79 | CENTER_PANE = False |
---|
80 | def __init__(self, parent=None, base=None, manager=None, panel=None, |
---|
81 | title='Python Shell/Editor', filename=None, |
---|
82 | size=(PANEL_WIDTH, PANEL_HEIGHT)): |
---|
83 | self.config = None |
---|
84 | editor.EditorNotebookFrame.__init__(self, parent=parent, |
---|
85 | title=title, size=size, |
---|
86 | filename=filename) |
---|
87 | self.parent = parent |
---|
88 | self._manager = manager |
---|
89 | self.base = base |
---|
90 | self.panel = panel |
---|
91 | self._add_menu() |
---|
92 | if filename != None: |
---|
93 | dataDir = os.path.dirname(filename) |
---|
94 | elif self.parent != None: |
---|
95 | dataDir = self.parent._default_save_location |
---|
96 | else: |
---|
97 | dataDir = None |
---|
98 | self.dataDir = dataDir |
---|
99 | self.Centre() |
---|
100 | |
---|
101 | self.Bind(wx.EVT_MENU, self.OnNewFile, id=wx.ID_NEW) |
---|
102 | self.Bind(wx.EVT_MENU, self.OnOpenFile, id=wx.ID_OPEN) |
---|
103 | self.Bind(wx.EVT_MENU, self.OnSaveFile, id=wx.ID_SAVE) |
---|
104 | self.Bind(wx.EVT_MENU, self.OnSaveAsFile, id=wx.ID_SAVEAS) |
---|
105 | self.Bind(wx.EVT_MENU, self.OnCheckModel, id=ID_CHECK_MODEL) |
---|
106 | self.Bind(wx.EVT_MENU, self.OnRun, id=ID_RUN) |
---|
107 | self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateCompileMenu, id=ID_CHECK_MODEL) |
---|
108 | self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateCompileMenu, id=ID_RUN) |
---|
109 | self.Bind(wx.EVT_CLOSE, self.on_close) |
---|
110 | if not title.count('Python Shell'): |
---|
111 | # Delete menu item (open and new) if not python shell |
---|
112 | #self.fileMenu.Delete(wx.ID_NEW) |
---|
113 | self.fileMenu.Delete(wx.ID_OPEN) |
---|
114 | |
---|
115 | |
---|
116 | def _add_menu(self): |
---|
117 | """ |
---|
118 | Add menu |
---|
119 | """ |
---|
120 | self.compileMenu = wx.Menu() |
---|
121 | self.compileMenu.Append(ID_CHECK_MODEL, 'Check model', |
---|
122 | 'Loading and run the model') |
---|
123 | self.compileMenu.AppendSeparator() |
---|
124 | self.compileMenu.Append(ID_RUN, 'Run in Shell', |
---|
125 | 'Run the file in the Python Shell') |
---|
126 | self.MenuBar.Insert(3, self.compileMenu, '&Run') |
---|
127 | |
---|
128 | def OnHelp(self, event): |
---|
129 | """ |
---|
130 | Show a help dialog. |
---|
131 | """ |
---|
132 | import wx.lib.dialogs |
---|
133 | title = 'Help on key bindings' |
---|
134 | text = wx.py.shell.HELP_TEXT |
---|
135 | dlg = wx.lib.dialogs.ScrolledMessageDialog(self, text, title, |
---|
136 | size=((700, 540))) |
---|
137 | fnt = wx.Font(10, wx.TELETYPE, wx.NORMAL, wx.NORMAL) |
---|
138 | dlg.GetChildren()[0].SetFont(fnt) |
---|
139 | dlg.GetChildren()[0].SetInsertionPoint(0) |
---|
140 | dlg.ShowModal() |
---|
141 | dlg.Destroy() |
---|
142 | |
---|
143 | def set_manager(self, manager): |
---|
144 | """ |
---|
145 | Set the manager of this window |
---|
146 | """ |
---|
147 | self._manager = manager |
---|
148 | |
---|
149 | def OnAbout(self, event): |
---|
150 | """ |
---|
151 | On About |
---|
152 | """ |
---|
153 | message = ABOUT |
---|
154 | dial = wx.MessageDialog(self, message, 'About', |
---|
155 | wx.OK | wx.ICON_INFORMATION) |
---|
156 | dial.ShowModal() |
---|
157 | |
---|
158 | def OnNewFile(self, event): |
---|
159 | """ |
---|
160 | OnFileOpen |
---|
161 | """ |
---|
162 | self.OnFileNew(event) |
---|
163 | |
---|
164 | def OnOpenFile(self, event): |
---|
165 | """ |
---|
166 | OnFileOpen |
---|
167 | """ |
---|
168 | self.OnFileOpen(event) |
---|
169 | self.Show(False) |
---|
170 | self.Show(True) |
---|
171 | |
---|
172 | def OnSaveFile(self, event): |
---|
173 | """ |
---|
174 | OnFileSave overwrite |
---|
175 | """ |
---|
176 | self.OnFileSave(event) |
---|
177 | self.Show(False) |
---|
178 | self.Show(True) |
---|
179 | |
---|
180 | def OnSaveAsFile(self, event): |
---|
181 | """ |
---|
182 | OnFileSaveAs overwrite |
---|
183 | """ |
---|
184 | self.OnFileSaveAs(event) |
---|
185 | self.Show(False) |
---|
186 | self.Show(True) |
---|
187 | |
---|
188 | def bufferOpen(self): |
---|
189 | """ |
---|
190 | Open file in buffer, bypassing editor bufferOpen |
---|
191 | """ |
---|
192 | if self.bufferHasChanged(): |
---|
193 | cancel = self.bufferSuggestSave() |
---|
194 | if cancel: |
---|
195 | return cancel |
---|
196 | filedir = '' |
---|
197 | if self.buffer and self.buffer.doc.filedir: |
---|
198 | filedir = self.buffer.doc.filedir |
---|
199 | if not filedir: |
---|
200 | filedir = self.dataDir |
---|
201 | result = editor.openSingle(directory=filedir, |
---|
202 | wildcard='Python Files (*.py)|*.py') |
---|
203 | if result.path: |
---|
204 | self.bufferCreate(result.path) |
---|
205 | cancel = False |
---|
206 | return cancel |
---|
207 | |
---|
208 | def bufferSaveAs(self): |
---|
209 | """ |
---|
210 | Save buffer to a new filename: Bypassing editor bufferSaveAs |
---|
211 | """ |
---|
212 | filedir = '' |
---|
213 | if self.buffer and self.buffer.doc.filedir: |
---|
214 | filedir = self.buffer.doc.filedir |
---|
215 | if not filedir: |
---|
216 | filedir = self.dataDir |
---|
217 | result = editor.saveSingle(directory=filedir, |
---|
218 | filename='untitled.py', |
---|
219 | wildcard='Python Files (*.py)|*.py') |
---|
220 | if result.path: |
---|
221 | self.buffer.confirmed = True |
---|
222 | self.buffer.saveAs(result.path) |
---|
223 | cancel = False |
---|
224 | else: |
---|
225 | cancel = True |
---|
226 | return cancel |
---|
227 | |
---|
228 | def OnRun(self, event): |
---|
229 | """ |
---|
230 | Run |
---|
231 | """ |
---|
232 | if not self._check_saved(): |
---|
233 | return True |
---|
234 | if self.buffer and self.buffer.doc.filepath: |
---|
235 | self.editor.setFocus() |
---|
236 | # Why we have to do this (Otherwise problems on Windows)? |
---|
237 | forward_path = self.buffer.doc.filepath.replace('\\', '/') |
---|
238 | self.shell.Execute("execfile('%s')" % forward_path) |
---|
239 | self.shell.Hide() |
---|
240 | self.shell.Show(True) |
---|
241 | return self.shell.GetText().split(">>>")[-2] |
---|
242 | else: |
---|
243 | mssg = "\n This is not a python file." |
---|
244 | title = 'Error' |
---|
245 | icon = wx.ICON_ERROR |
---|
246 | wx.MessageBox(str(mssg), title, style=icon) |
---|
247 | return False |
---|
248 | |
---|
249 | def OnCheckModel(self, event): |
---|
250 | """ |
---|
251 | Compile |
---|
252 | """ |
---|
253 | if not self._check_saved(): |
---|
254 | return True |
---|
255 | fname = self.editor.getStatus()[0] |
---|
256 | success = show_model_output(self, fname) |
---|
257 | |
---|
258 | # Update custom model list in fitpage combobox |
---|
259 | if success and self._manager != None and self.panel != None: |
---|
260 | self._manager.set_edit_menu_helper(self.parent) |
---|
261 | wx.CallAfter(self._manager.update_custom_combo) |
---|
262 | |
---|
263 | def _check_saved(self): |
---|
264 | """ |
---|
265 | If content was changed, suggest to save it first |
---|
266 | """ |
---|
267 | if self.bufferHasChanged() and self.buffer.doc.filepath: |
---|
268 | cancel = self.bufferSuggestSave() |
---|
269 | return not cancel |
---|
270 | return True |
---|
271 | |
---|
272 | def OnUpdateCompileMenu(self, event): |
---|
273 | """ |
---|
274 | Update Compile menu items based on current tap. |
---|
275 | """ |
---|
276 | win = wx.Window.FindFocus() |
---|
277 | id = event.GetId() |
---|
278 | event.Enable(True) |
---|
279 | try: |
---|
280 | if id == ID_CHECK_MODEL or id == ID_RUN: |
---|
281 | menu_on = False |
---|
282 | if self.buffer and self.buffer.doc.filepath: |
---|
283 | menu_on = True |
---|
284 | event.Enable(menu_on) |
---|
285 | except AttributeError: |
---|
286 | # This menu option is not supported in the current context. |
---|
287 | event.Enable(False) |
---|
288 | |
---|
289 | def on_close(self, event): |
---|
290 | """ |
---|
291 | Close event |
---|
292 | """ |
---|
293 | if self.base != None: |
---|
294 | self.base.py_frame = None |
---|
295 | self.Destroy() |
---|
296 | |
---|
297 | ABOUT = "Welcome to Python %s! \n\n" % sys.version.split()[0] |
---|
298 | ABOUT += "This uses Py Shell/Editor in wx (developed by Patrick K. O'Brien).\n" |
---|
299 | ABOUT += "If this is your first time using Python, \n" |
---|
300 | ABOUT += "you should definitely check out the tutorial " |
---|
301 | ABOUT += "on the Internet at http://www.python.org/doc/tut/." |
---|
302 | |
---|
303 | |
---|
304 | if __name__ == "__main__": |
---|
305 | |
---|
306 | app = wx.App() |
---|
307 | dlg = PyConsole() |
---|
308 | dlg.Show() |
---|
309 | app.MainLoop() |
---|