##// END OF EJS Templates
Cleaned up version with initial WX callback (callafter) support
ldufrechou -
Show More
@@ -0,0 +1,420 b''
1 #!/usr/bin/python
2 # -*- coding: iso-8859-15 -*-
3 '''
4 Provides IPython remote instance.
5
6 @author: Laurent Dufrechou
7 laurent.dufrechou _at_ gmail.com
8 @license: BSD
9
10 All rights reserved. This program and the accompanying materials are made
11 available under the terms of the BSD which accompanies this distribution, and
12 is available at U{http://www.opensource.org/licenses/bsd-license.php}
13 '''
14
15 __version__ = 0.9
16 __author__ = "Laurent Dufrechou"
17 __email__ = "laurent.dufrechou _at_ gmail.com"
18 __license__ = "BSD"
19
20 import re
21 import sys
22 import os
23 import locale
24 import time
25 from ThreadEx import Thread
26 from StringIO import StringIO
27
28 try:
29 import IPython
30 except Exception,e:
31 raise "Error importing IPython (%s)" % str(e)
32
33 class IterableIPShell(Thread):
34 '''
35 Create an IPython instance inside a dedicated thread.
36 Does not start a blocking event loop, instead allow single iterations.
37 This allows embedding in any GUI without blockage.
38 The thread is a slave one, in that it doesn't interact directly with the GUI.
39 Note Thread class comes from ThreadEx that supports asynchroneous function call
40 via raise_exc()
41 '''
42
43 def __init__(self,argv=[],user_ns={},user_global_ns=None,
44 cin=None, cout=None, cerr=None,
45 exit_handler=None,time_loop = 0.1):
46 '''
47 @param argv: Command line options for IPython
48 @type argv: list
49 @param user_ns: User namespace.
50 @type user_ns: dictionary
51 @param user_global_ns: User global namespace.
52 @type user_global_ns: dictionary.
53 @param cin: Console standard input.
54 @type cin: IO stream
55 @param cout: Console standard output.
56 @type cout: IO stream
57 @param cerr: Console standard error.
58 @type cerr: IO stream
59 @param exit_handler: Replacement for builtin exit() function
60 @type exit_handler: function
61 @param time_loop: Define the sleep time between two thread's loop
62 @type int
63 '''
64 Thread.__init__(self)
65
66 #first we redefine in/out/error functions of IPython
67 if cin:
68 IPython.Shell.Term.cin = cin
69 if cout:
70 IPython.Shell.Term.cout = cout
71 if cerr:
72 IPython.Shell.Term.cerr = cerr
73
74 # This is to get rid of the blockage that accurs during
75 # IPython.Shell.InteractiveShell.user_setup()
76 IPython.iplib.raw_input = lambda x: None
77
78 self._term = IPython.genutils.IOTerm(cin=cin, cout=cout, cerr=cerr)
79
80 excepthook = sys.excepthook
81
82 self._IP = IPython.Shell.make_IPython(
83 argv,user_ns=user_ns,
84 user_global_ns=user_global_ns,
85 embedded=True,
86 shell_class=IPython.Shell.InteractiveShell)
87
88 #we replace IPython default encoding by wx locale encoding
89 loc = locale.getpreferredencoding()
90 if loc:
91 self._IP.stdin_encoding = loc
92 #we replace the ipython default pager by our pager
93 self._IP.set_hook('show_in_pager',self._pager)
94
95 #we replace the ipython default shell command caller by our shell handler
96 self._IP.set_hook('shell_hook',self._shell)
97
98 #we replace the ipython default input command caller by our method
99 IPython.iplib.raw_input_original = self._raw_input
100 #we replace the ipython default exit command by our method
101 self._IP.exit = self._setAskExit
102
103 sys.excepthook = excepthook
104
105 self._iter_more = 0
106 self._history_level = 0
107 self._complete_sep = re.compile('[\s\{\}\[\]\(\)]')
108 self._prompt = str(self._IP.outputcache.prompt1).strip()
109
110 #thread working vars
111 self._terminate = False
112 self._time_loop = time_loop
113 self._has_doc = False
114 self._do_execute = False
115 self._line_to_execute = ''
116
117 #vars that will be checked by GUI loop to handle thread states...
118 #will be replaced later by PostEvent GUI funtions...
119 self._doc_text = None
120 self._ask_exit = False
121 self._add_button = None
122
123 #----------------------- Thread management section ----------------------
124 def run (self):
125 """
126 Thread main loop
127 The thread will run until self._terminate will be set to True via shutdown() function
128 Command processing can be interrupted with Instance.raise_exc(KeyboardInterrupt) call in the
129 GUI thread.
130 """
131 while(not self._terminate):
132 try:
133 if self._do_execute:
134 self._doc_text = None
135 self._execute()
136 self._do_execute = False
137 self._afterExecute() #used for uper class to generate event after execution
138
139 except KeyboardInterrupt:
140 pass
141
142 time.sleep(self._time_loop)
143
144 def shutdown(self):
145 """
146 Shutdown the tread
147 """
148 self._terminate = True
149
150 def doExecute(self,line):
151 """
152 Tell the thread to process the 'line' command
153 """
154 self._do_execute = True
155 self._line_to_execute = line
156
157 def isExecuteDone(self):
158 """
159 Returns the processing state
160 """
161 return not self._do_execute
162
163 #----------------------- IPython management section ----------------------
164 def getAskExit(self):
165 '''
166 returns the _ask_exit variable that can be checked by GUI to see if
167 IPython request an exit handling
168 '''
169 return self._ask_exit
170
171 def clearAskExit(self):
172 '''
173 clear the _ask_exit var when GUI as handled the request.
174 '''
175 self._ask_exit = False
176
177 def getDocText(self):
178 """
179 Returns the output of the processing that need to be paged (if any)
180
181 @return: The std output string.
182 @rtype: string
183 """
184 return self._doc_text
185
186 def getBanner(self):
187 """
188 Returns the IPython banner for useful info on IPython instance
189
190 @return: The banner string.
191 @rtype: string
192 """
193 return self._IP.BANNER
194
195 def getPromptCount(self):
196 """
197 Returns the prompt number.
198 Each time a user execute a line in the IPython shell the prompt count is increased
199
200 @return: The prompt number
201 @rtype: int
202 """
203 return self._IP.outputcache.prompt_count
204
205 def getPrompt(self):
206 """
207 Returns current prompt inside IPython instance
208 (Can be In [...]: ot ...:)
209
210 @return: The current prompt.
211 @rtype: string
212 """
213 return self._prompt
214
215 def getIndentation(self):
216 """
217 Returns the current indentation level
218 Usefull to put the caret at the good start position if we want to do autoindentation.
219
220 @return: The indentation level.
221 @rtype: int
222 """
223 return self._IP.indent_current_nsp
224
225 def updateNamespace(self, ns_dict):
226 '''
227 Add the current dictionary to the shell namespace.
228
229 @param ns_dict: A dictionary of symbol-values.
230 @type ns_dict: dictionary
231 '''
232 self._IP.user_ns.update(ns_dict)
233
234 def complete(self, line):
235 '''
236 Returns an auto completed line and/or posibilities for completion.
237
238 @param line: Given line so far.
239 @type line: string
240
241 @return: Line completed as for as possible,
242 and possible further completions.
243 @rtype: tuple
244 '''
245 split_line = self._complete_sep.split(line)
246 possibilities = self._IP.complete(split_line[-1])
247 if possibilities:
248
249 def _commonPrefix(str1, str2):
250 '''
251 Reduction function. returns common prefix of two given strings.
252
253 @param str1: First string.
254 @type str1: string
255 @param str2: Second string
256 @type str2: string
257
258 @return: Common prefix to both strings.
259 @rtype: string
260 '''
261 for i in range(len(str1)):
262 if not str2.startswith(str1[:i+1]):
263 return str1[:i]
264 return str1
265 common_prefix = reduce(_commonPrefix, possibilities)
266 completed = line[:-len(split_line[-1])]+common_prefix
267 else:
268 completed = line
269 return completed, possibilities
270
271 def historyBack(self):
272 '''
273 Provides one history command back.
274
275 @return: The command string.
276 @rtype: string
277 '''
278 history = ''
279 #the below while loop is used to suppress empty history lines
280 while((history == '' or history == '\n') and self._history_level >0):
281 if self._history_level>=1:
282 self._history_level -= 1
283 history = self._getHistory()
284 return history
285
286 def historyForward(self):
287 '''
288 Provides one history command forward.
289
290 @return: The command string.
291 @rtype: string
292 '''
293 history = ''
294 #the below while loop is used to suppress empty history lines
295 while((history == '' or history == '\n') and self._history_level <= self._getHistoryMaxIndex()):
296 if self._history_level < self._getHistoryMaxIndex():
297 self._history_level += 1
298 history = self._getHistory()
299 else:
300 if self._history_level == self._getHistoryMaxIndex():
301 history = self._getHistory()
302 self._history_level += 1
303 else:
304 history = ''
305 return history
306
307 def initHistoryIndex(self):
308 '''
309 set history to last command entered
310 '''
311 self._history_level = self._getHistoryMaxIndex()+1
312
313 #----------------------- IPython PRIVATE management section ----------------------
314 def _afterExecute(self):
315 '''
316 Can be redefined to generate post event after excution is done
317 '''
318 pass
319
320 def _setAskExit(self):
321 '''
322 set the _ask_exit variable that can be cjhecked by GUI to see if
323 IPython request an exit handling
324 '''
325 self._ask_exit = True
326
327 def _getHistoryMaxIndex(self):
328 '''
329 returns the max length of the history buffer
330
331 @return: history length
332 @rtype: int
333 '''
334 return len(self._IP.input_hist_raw)-1
335
336 def _getHistory(self):
337 '''
338 Get's the command string of the current history level.
339
340 @return: Historic command string.
341 @rtype: string
342 '''
343 rv = self._IP.input_hist_raw[self._history_level].strip('\n')
344 return rv
345
346 def _pager(self,IP,text):
347 '''
348 This function is used as a callback replacment to IPython pager function
349
350 It puts the 'text' value inside the self._doc_text string that can be retrived via getDocText
351 function.
352 '''
353 self._doc_text = text
354
355 def _raw_input(self, prompt=''):
356 '''
357 Custom raw_input() replacement. Get's current line from console buffer.
358
359 @param prompt: Prompt to print. Here for compatability as replacement.
360 @type prompt: string
361
362 @return: The current command line text.
363 @rtype: string
364 '''
365 return self._line_to_execute
366
367 def _execute(self):
368 '''
369 Executes the current line provided by the shell object.
370 '''
371 orig_stdout = sys.stdout
372 sys.stdout = IPython.Shell.Term.cout
373
374 try:
375 line = self._IP.raw_input(None, self._iter_more)
376 if self._IP.autoindent:
377 self._IP.readline_startup_hook(None)
378
379 except KeyboardInterrupt:
380 self._IP.write('\nKeyboardInterrupt\n')
381 self._IP.resetbuffer()
382 # keep cache in sync with the prompt counter:
383 self._IP.outputcache.prompt_count -= 1
384
385 if self._IP.autoindent:
386 self._IP.indent_current_nsp = 0
387 self._iter_more = 0
388 except:
389 self._IP.showtraceback()
390 else:
391 self._iter_more = self._IP.push(line)
392 if (self._IP.SyntaxTB.last_syntax_error and
393 self._IP.rc.autoedit_syntax):
394 self._IP.edit_syntax_error()
395 if self._iter_more:
396 self._prompt = str(self._IP.outputcache.prompt2).strip()
397 if self._IP.autoindent:
398 self._IP.readline_startup_hook(self._IP.pre_readline)
399 else:
400 self._prompt = str(self._IP.outputcache.prompt1).strip()
401 self._IP.indent_current_nsp = 0 #we set indentation to 0
402 sys.stdout = orig_stdout
403
404 def _shell(self, ip, cmd):
405 '''
406 Replacement method to allow shell commands without them blocking.
407
408 @param ip: Ipython instance, same as self._IP
409 @type cmd: Ipython instance
410 @param cmd: Shell command to execute.
411 @type cmd: string
412 '''
413 stdin, stdout = os.popen4(cmd)
414 result = stdout.read().decode('cp437').encode(locale.getpreferredencoding())
415 #we use print command because the shell command is called inside IPython instance and thus is
416 #redirected to thread cout
417 #"\x01\x1b[1;36m\x02" <-- add colour to the text...
418 print "\x01\x1b[1;36m\x02"+result
419 stdout.close()
420 stdin.close()
1 NO CONTENT: modified file
NO CONTENT: modified file
@@ -1,1046 +1,762 b''
1 #!/usr/bin/python
1 #!/usr/bin/python
2 # -*- coding: iso-8859-15 -*-
2 # -*- coding: iso-8859-15 -*-
3 '''
3 '''
4 Provides IPython WX console widget.
4 Provides IPython WX console widget.
5
5
6 @author: Laurent Dufrechou
6 @author: Laurent Dufrechou
7 laurent.dufrechou _at_ gmail.com
7 laurent.dufrechou _at_ gmail.com
8 This WX widget is based on the original work of Eitan Isaacson
8 This WX widget is based on the original work of Eitan Isaacson
9 that provided the console for the GTK toolkit.
9 that provided the console for the GTK toolkit.
10
10
11 Original work from:
11 Original work from:
12 @author: Eitan Isaacson
12 @author: Eitan Isaacson
13 @organization: IBM Corporation
13 @organization: IBM Corporation
14 @copyright: Copyright (c) 2007 IBM Corporation
14 @copyright: Copyright (c) 2007 IBM Corporation
15 @license: BSD
15 @license: BSD
16
16
17 All rights reserved. This program and the accompanying materials are made
17 All rights reserved. This program and the accompanying materials are made
18 available under the terms of the BSD which accompanies this distribution, and
18 available under the terms of the BSD which accompanies this distribution, and
19 is available at U{http://www.opensource.org/licenses/bsd-license.php}
19 is available at U{http://www.opensource.org/licenses/bsd-license.php}
20 '''
20 '''
21
21
22 __version__ = 0.8
22 __version__ = 0.8
23 __author__ = "Laurent Dufrechou"
23 __author__ = "Laurent Dufrechou"
24 __email__ = "laurent.dufrechou _at_ gmail.com"
24 __email__ = "laurent.dufrechou _at_ gmail.com"
25 __license__ = "BSD"
25 __license__ = "BSD"
26
26
27 import wx
27 import wx
28 import wx.stc as stc
28 import wx.stc as stc
29 import wx.lib.newevent
29 import wx.lib.newevent
30
30
31 import re
31 import re
32 import sys
32 import sys
33 import os
33 import os
34 import locale
34 import locale
35 import time
35 import time
36 from ThreadEx import Thread
36 #from ThreadEx import Thread
37 from StringIO import StringIO
37 from StringIO import StringIO
38
39 try:
38 try:
40 import IPython
39 import IPython
41 except Exception,e:
40 except Exception,e:
42 raise "Error importing IPython (%s)" % str(e)
41 raise "Error importing IPython (%s)" % str(e)
43
42
44 class IterableIPShell(Thread):
45 '''
46 Create an IPython instance inside a dedicated thread.
47 Does not start a blocking event loop, instead allow single iterations.
48 This allows embedding in any GUI without blockage.
49 The thread is a slave one, in that it doesn't interact directly with the GUI.
50 Note Thread class comes from ThreadEx that supports asynchroneous function call
51 via raise_exc()
52 '''
53
43
54 def __init__(self,argv=[],user_ns=None,user_global_ns=None,
44 from ipython_interactive_shell import *
55 cin=None, cout=None, cerr=None,
56 exit_handler=None,time_loop = 0.1):
57 '''
58 @param argv: Command line options for IPython
59 @type argv: list
60 @param user_ns: User namespace.
61 @type user_ns: dictionary
62 @param user_global_ns: User global namespace.
63 @type user_global_ns: dictionary.
64 @param cin: Console standard input.
65 @type cin: IO stream
66 @param cout: Console standard output.
67 @type cout: IO stream
68 @param cerr: Console standard error.
69 @type cerr: IO stream
70 @param exit_handler: Replacement for builtin exit() function
71 @type exit_handler: function
72 @param time_loop: Define the sleep time between two thread's loop
73 @type int
74 '''
75 Thread.__init__(self)
76
77 #first we redefine in/out/error functions of IPython
78 if cin:
79 IPython.Shell.Term.cin = cin
80 if cout:
81 IPython.Shell.Term.cout = cout
82 if cerr:
83 IPython.Shell.Term.cerr = cerr
84
85 # This is to get rid of the blockage that accurs during
86 # IPython.Shell.InteractiveShell.user_setup()
87 IPython.iplib.raw_input = lambda x: None
88
89 self._term = IPython.genutils.IOTerm(cin=cin, cout=cout, cerr=cerr)
90
91 excepthook = sys.excepthook
92 self._IP = IPython.Shell.make_IPython(
93 argv,user_ns=user_ns,
94 user_global_ns=user_global_ns,
95 embedded=True,
96 shell_class=IPython.Shell.InteractiveShell)
97
98 #we replace IPython default encoding by wx locale encoding
99 loc = locale.getpreferredencoding()
100 if loc:
101 self._IP.stdin_encoding = loc
102 #we replace the ipython default pager by our pager
103 self._IP.set_hook('show_in_pager',self._pager)
104
105 #we replace the ipython default shell command caller by our shell handler
106 self._IP.set_hook('shell_hook',self._shell)
107
108 #we replace the ipython default input command caller by our method
109 IPython.iplib.raw_input_original = self._raw_input
110 #we replace the ipython default exit command by our method
111 self._IP.exit = self._setAskExit
112
113 sys.excepthook = excepthook
114
115 self._iter_more = 0
116 self._history_level = 0
117 self._complete_sep = re.compile('[\s\{\}\[\]\(\)]')
118 self._prompt = str(self._IP.outputcache.prompt1).strip()
119
120 #thread working vars
121 self._terminate = False
122 self._time_loop = time_loop
123 self._has_doc = False
124 self._do_execute = False
125 self._line_to_execute = ''
126 self._doc_text = None
127 self._ask_exit = False
128
129 #----------------------- Thread management section ----------------------
130 def run (self):
131 """
132 Thread main loop
133 The thread will run until self._terminate will be set to True via shutdown() function
134 Command processing can be interrupted with Instance.raise_exc(KeyboardInterrupt) call in the
135 GUI thread.
136 """
137 while(not self._terminate):
138 try:
139 if self._do_execute:
140 self._doc_text = None
141 self._execute()
142 self._do_execute = False
143
144 except KeyboardInterrupt:
145 pass
146
147 time.sleep(self._time_loop)
148
149 def shutdown(self):
150 """
151 Shutdown the tread
152 """
153 self._terminate = True
154
155 def doExecute(self,line):
156 """
157 Tell the thread to process the 'line' command
158 """
159 self._do_execute = True
160 self._line_to_execute = line
161
162 def isExecuteDone(self):
163 """
164 Returns the processing state
165 """
166 return not self._do_execute
167
168 #----------------------- IPython management section ----------------------
169 def getAskExit(self):
170 '''
171 returns the _ask_exit variable that can be checked by GUI to see if
172 IPython request an exit handling
173 '''
174 return self._ask_exit
175
45
176 def clearAskExit(self):
46 class WxIterableIPShell(IterableIPShell):
177 '''
47 '''
178 clear the _ask_exit var when GUI as handled the request.
48 An IterableIPShell Thread that is WX dependent.
179 '''
49 Thus it permits direct interaction with a WX GUI without OnIdle event state machine trick...
180 self._ask_exit = False
181
182 def getDocText(self):
183 """
184 Returns the output of the processing that need to be paged (if any)
185
186 @return: The std output string.
187 @rtype: string
188 """
189 return self._doc_text
190
191 def getBanner(self):
192 """
193 Returns the IPython banner for useful info on IPython instance
194
195 @return: The banner string.
196 @rtype: string
197 """
198 return self._IP.BANNER
199
200 def getPromptCount(self):
201 """
202 Returns the prompt number.
203 Each time a user execute a line in the IPython shell the prompt count is increased
204
205 @return: The prompt number
206 @rtype: int
207 """
208 return self._IP.outputcache.prompt_count
209
210 def getPrompt(self):
211 """
212 Returns current prompt inside IPython instance
213 (Can be In [...]: ot ...:)
214
215 @return: The current prompt.
216 @rtype: string
217 """
218 return self._prompt
219
220 def getIndentation(self):
221 """
222 Returns the current indentation level
223 Usefull to put the caret at the good start position if we want to do autoindentation.
224
225 @return: The indentation level.
226 @rtype: int
227 """
228 return self._IP.indent_current_nsp
229
230 def updateNamespace(self, ns_dict):
231 '''
232 Add the current dictionary to the shell namespace.
233
234 @param ns_dict: A dictionary of symbol-values.
235 @type ns_dict: dictionary
236 '''
237 self._IP.user_ns.update(ns_dict)
238
239 def complete(self, line):
240 '''
241 Returns an auto completed line and/or posibilities for completion.
242
243 @param line: Given line so far.
244 @type line: string
245
246 @return: Line completed as for as possible,
247 and possible further completions.
248 @rtype: tuple
249 '''
250 split_line = self._complete_sep.split(line)
251 possibilities = self._IP.complete(split_line[-1])
252 if possibilities:
253
254 def _commonPrefix(str1, str2):
255 '''
256 Reduction function. returns common prefix of two given strings.
257
258 @param str1: First string.
259 @type str1: string
260 @param str2: Second string
261 @type str2: string
262
263 @return: Common prefix to both strings.
264 @rtype: string
265 '''
266 for i in range(len(str1)):
267 if not str2.startswith(str1[:i+1]):
268 return str1[:i]
269 return str1
270 common_prefix = reduce(_commonPrefix, possibilities)
271 completed = line[:-len(split_line[-1])]+common_prefix
272 else:
273 completed = line
274 return completed, possibilities
275
276 def historyBack(self):
277 '''
278 Provides one history command back.
279
280 @return: The command string.
281 @rtype: string
282 '''
283 history = ''
284 #the below while loop is used to suppress empty history lines
285 while((history == '' or history == '\n') and self._history_level >0):
286 if self._history_level>=1:
287 self._history_level -= 1
288 history = self._getHistory()
289 return history
290
291 def historyForward(self):
292 '''
293 Provides one history command forward.
294
295 @return: The command string.
296 @rtype: string
297 '''
50 '''
298 history = ''
51 def __init__(self,wx_instance,
299 #the below while loop is used to suppress empty history lines
52 argv=[],user_ns={},user_global_ns=None,
300 while((history == '' or history == '\n') and self._history_level <= self._getHistoryMaxIndex()):
53 cin=None, cout=None, cerr=None,
301 if self._history_level < self._getHistoryMaxIndex():
54 exit_handler=None,time_loop = 0.1):
302 self._history_level += 1
303 history = self._getHistory()
304 else:
305 if self._history_level == self._getHistoryMaxIndex():
306 history = self._getHistory()
307 self._history_level += 1
308 else:
309 history = ''
310 return history
311
312 def initHistoryIndex(self):
313 '''
314 set history to last command entered
315 '''
316 self._history_level = self._getHistoryMaxIndex()+1
317
318 #----------------------- IPython PRIVATE management section ----------------------
319 def _setAskExit(self):
320 '''
321 set the _ask_exit variable that can be cjhecked by GUI to see if
322 IPython request an exit handling
323 '''
324 self._ask_exit = True
325
326 def _getHistoryMaxIndex(self):
327 '''
328 returns the max length of the history buffer
329
330 @return: history length
331 @rtype: int
332 '''
333 return len(self._IP.input_hist_raw)-1
334
335 def _getHistory(self):
336 '''
337 Get's the command string of the current history level.
338
339 @return: Historic command string.
340 @rtype: string
341 '''
342 rv = self._IP.input_hist_raw[self._history_level].strip('\n')
343 return rv
344
345 def _pager(self,IP,text):
346 '''
347 This function is used as a callback replacment to IPython pager function
348
55
349 It puts the 'text' value inside the self._doc_text string that can be retrived via getDocText
56 user_ns['addGUIShortcut'] = self.addGUIShortcut
350 function.
57 IterableIPShell.__init__(self,argv,user_ns,user_global_ns,
351 '''
58 cin, cout, cerr,
352 self._doc_text = text
59 exit_handler,time_loop)
353
60
354 def _raw_input(self, prompt=''):
61 # This creates a new Event class and a EVT binder function
355 '''
62 (self.IPythonAskExitEvent, EVT_IP_ASK_EXIT) = wx.lib.newevent.NewEvent()
356 Custom raw_input() replacement. Get's current line from console buffer.
63 (self.IPythonAddButtonEvent, EVT_IP_ADD_BUTTON_EXIT) = wx.lib.newevent.NewEvent()
64 (self.IPythonExecuteDoneEvent, EVT_IP_EXECUTE_DONE) = wx.lib.newevent.NewEvent()
357
65
358 @param prompt: Prompt to print. Here for compatability as replacement.
66 wx_instance.Bind(EVT_IP_ASK_EXIT, wx_instance.exit_handler)
359 @type prompt: string
67 wx_instance.Bind(EVT_IP_ADD_BUTTON_EXIT, wx_instance.add_button_handler)
68 wx_instance.Bind(EVT_IP_EXECUTE_DONE, wx_instance.evtStateExecuteDone)
360
69
361 @return: The current command line text.
70 self.wx_instance = wx_instance
362 @rtype: string
71 self._IP.exit = self._AskExit
363 '''
364 return self._line_to_execute
365
72
366 def _execute(self):
73 def addGUIShortcut(self,text,func):
367 '''
74 evt = self.IPythonAddButtonEvent(button_info={'text':text,'func':self.wx_instance.doExecuteLine(func)})
368 Executes the current line provided by the shell object.
75 wx.PostEvent(self.wx_instance, evt)
369 '''
370 orig_stdout = sys.stdout
371 sys.stdout = IPython.Shell.Term.cout
372
76
373 try:
77 def _AskExit(self):
374 line = self._IP.raw_input(None, self._iter_more)
78 evt = self.IPythonAskExitEvent()
375 if self._IP.autoindent:
79 wx.PostEvent(self.wx_instance, evt)
376 self._IP.readline_startup_hook(None)
377
378 except KeyboardInterrupt:
379 self._IP.write('\nKeyboardInterrupt\n')
380 self._IP.resetbuffer()
381 # keep cache in sync with the prompt counter:
382 self._IP.outputcache.prompt_count -= 1
383
384 if self._IP.autoindent:
385 self._IP.indent_current_nsp = 0
386 self._iter_more = 0
387 except:
388 self._IP.showtraceback()
389 else:
390 self._iter_more = self._IP.push(line)
391 if (self._IP.SyntaxTB.last_syntax_error and
392 self._IP.rc.autoedit_syntax):
393 self._IP.edit_syntax_error()
394 if self._iter_more:
395 self._prompt = str(self._IP.outputcache.prompt2).strip()
396 if self._IP.autoindent:
397 self._IP.readline_startup_hook(self._IP.pre_readline)
398 else:
399 self._prompt = str(self._IP.outputcache.prompt1).strip()
400 self._IP.indent_current_nsp = 0 #we set indentation to 0
401 sys.stdout = orig_stdout
402
80
403 def _shell(self, ip, cmd):
81 def _afterExecute(self):
404 '''
82 evt = self.IPythonExecuteDoneEvent()
405 Replacement method to allow shell commands without them blocking.
83 wx.PostEvent(self.wx_instance, evt)
406
84
407 @param ip: Ipython instance, same as self._IP
408 @type cmd: Ipython instance
409 @param cmd: Shell command to execute.
410 @type cmd: string
411 '''
412 stdin, stdout = os.popen4(cmd)
413 result = stdout.read().decode('cp437').encode(locale.getpreferredencoding())
414 #we use print command because the shell command is called inside IPython instance and thus is
415 #redirected to thread cout
416 #"\x01\x1b[1;36m\x02" <-- add colour to the text...
417 print "\x01\x1b[1;36m\x02"+result
418 stdout.close()
419 stdin.close()
420
85
421 class WxConsoleView(stc.StyledTextCtrl):
86 class WxConsoleView(stc.StyledTextCtrl):
422 '''
87 '''
423 Specialized styled text control view for console-like workflow.
88 Specialized styled text control view for console-like workflow.
424 We use here a scintilla frontend thus it can be reused in any GUI taht supports
89 We use here a scintilla frontend thus it can be reused in any GUI taht supports
425 scintilla with less work.
90 scintilla with less work.
426
91
427 @cvar ANSI_COLORS_BLACK: Mapping of terminal colors to X11 names.(with Black background)
92 @cvar ANSI_COLORS_BLACK: Mapping of terminal colors to X11 names.(with Black background)
428 @type ANSI_COLORS_BLACK: dictionary
93 @type ANSI_COLORS_BLACK: dictionary
429
94
430 @cvar ANSI_COLORS_WHITE: Mapping of terminal colors to X11 names.(with White background)
95 @cvar ANSI_COLORS_WHITE: Mapping of terminal colors to X11 names.(with White background)
431 @type ANSI_COLORS_WHITE: dictionary
96 @type ANSI_COLORS_WHITE: dictionary
432
97
433 @ivar color_pat: Regex of terminal color pattern
98 @ivar color_pat: Regex of terminal color pattern
434 @type color_pat: _sre.SRE_Pattern
99 @type color_pat: _sre.SRE_Pattern
435 '''
100 '''
436 ANSI_STYLES_BLACK ={'0;30': [0,'WHITE'], '0;31': [1,'RED'],
101 ANSI_STYLES_BLACK ={'0;30': [0,'WHITE'], '0;31': [1,'RED'],
437 '0;32': [2,'GREEN'], '0;33': [3,'BROWN'],
102 '0;32': [2,'GREEN'], '0;33': [3,'BROWN'],
438 '0;34': [4,'BLUE'], '0;35': [5,'PURPLE'],
103 '0;34': [4,'BLUE'], '0;35': [5,'PURPLE'],
439 '0;36': [6,'CYAN'], '0;37': [7,'LIGHT GREY'],
104 '0;36': [6,'CYAN'], '0;37': [7,'LIGHT GREY'],
440 '1;30': [8,'DARK GREY'], '1;31': [9,'RED'],
105 '1;30': [8,'DARK GREY'], '1;31': [9,'RED'],
441 '1;32': [10,'SEA GREEN'], '1;33': [11,'YELLOW'],
106 '1;32': [10,'SEA GREEN'], '1;33': [11,'YELLOW'],
442 '1;34': [12,'LIGHT BLUE'], '1;35': [13,'MEDIUM VIOLET RED'],
107 '1;34': [12,'LIGHT BLUE'], '1;35': [13,'MEDIUM VIOLET RED'],
443 '1;36': [14,'LIGHT STEEL BLUE'], '1;37': [15,'YELLOW']}
108 '1;36': [14,'LIGHT STEEL BLUE'], '1;37': [15,'YELLOW']}
444
109
445 ANSI_STYLES_WHITE ={'0;30': [0,'BLACK'], '0;31': [1,'RED'],
110 ANSI_STYLES_WHITE ={'0;30': [0,'BLACK'], '0;31': [1,'RED'],
446 '0;32': [2,'GREEN'], '0;33': [3,'BROWN'],
111 '0;32': [2,'GREEN'], '0;33': [3,'BROWN'],
447 '0;34': [4,'BLUE'], '0;35': [5,'PURPLE'],
112 '0;34': [4,'BLUE'], '0;35': [5,'PURPLE'],
448 '0;36': [6,'CYAN'], '0;37': [7,'LIGHT GREY'],
113 '0;36': [6,'CYAN'], '0;37': [7,'LIGHT GREY'],
449 '1;30': [8,'DARK GREY'], '1;31': [9,'RED'],
114 '1;30': [8,'DARK GREY'], '1;31': [9,'RED'],
450 '1;32': [10,'SEA GREEN'], '1;33': [11,'YELLOW'],
115 '1;32': [10,'SEA GREEN'], '1;33': [11,'YELLOW'],
451 '1;34': [12,'LIGHT BLUE'], '1;35': [13,'MEDIUM VIOLET RED'],
116 '1;34': [12,'LIGHT BLUE'], '1;35': [13,'MEDIUM VIOLET RED'],
452 '1;36': [14,'LIGHT STEEL BLUE'], '1;37': [15,'YELLOW']}
117 '1;36': [14,'LIGHT STEEL BLUE'], '1;37': [15,'YELLOW']}
453
118
454 def __init__(self,parent,prompt,intro="",background_color="BLACK",pos=wx.DefaultPosition, ID = -1, size=wx.DefaultSize,
119 def __init__(self,parent,prompt,intro="",background_color="BLACK",pos=wx.DefaultPosition, ID = -1, size=wx.DefaultSize,
455 style=0):
120 style=0):
456 '''
121 '''
457 Initialize console view.
122 Initialize console view.
458
123
459 @param parent: Parent widget
124 @param parent: Parent widget
460 @param prompt: User specified prompt
125 @param prompt: User specified prompt
461 @type intro: string
126 @type intro: string
462 @param intro: User specified startup introduction string
127 @param intro: User specified startup introduction string
463 @type intro: string
128 @type intro: string
464 @param background_color: Can be BLACK or WHITE
129 @param background_color: Can be BLACK or WHITE
465 @type background_color: string
130 @type background_color: string
466 @param other: init param of styledTextControl (can be used as-is)
131 @param other: init param of styledTextControl (can be used as-is)
467 '''
132 '''
468 stc.StyledTextCtrl.__init__(self, parent, ID, pos, size, style)
133 stc.StyledTextCtrl.__init__(self, parent, ID, pos, size, style)
469
134
470 ####### Scintilla configuration ##################################################
135 ####### Scintilla configuration ##################################################
471
136
472 # Ctrl + B or Ctrl + N can be used to zoomin/zoomout the text inside the widget
137 # Ctrl + B or Ctrl + N can be used to zoomin/zoomout the text inside the widget
473 self.CmdKeyAssign(ord('B'), stc.STC_SCMOD_CTRL, stc.STC_CMD_ZOOMIN)
138 self.CmdKeyAssign(ord('B'), stc.STC_SCMOD_CTRL, stc.STC_CMD_ZOOMIN)
474 self.CmdKeyAssign(ord('N'), stc.STC_SCMOD_CTRL, stc.STC_CMD_ZOOMOUT)
139 self.CmdKeyAssign(ord('N'), stc.STC_SCMOD_CTRL, stc.STC_CMD_ZOOMOUT)
475
140
476 #we define platform specific fonts
141 #we define platform specific fonts
477 if wx.Platform == '__WXMSW__':
142 if wx.Platform == '__WXMSW__':
478 faces = { 'times': 'Times New Roman',
143 faces = { 'times': 'Times New Roman',
479 'mono' : 'Courier New',
144 'mono' : 'Courier New',
480 'helv' : 'Arial',
145 'helv' : 'Arial',
481 'other': 'Comic Sans MS',
146 'other': 'Comic Sans MS',
482 'size' : 10,
147 'size' : 10,
483 'size2': 8,
148 'size2': 8,
484 }
149 }
485 elif wx.Platform == '__WXMAC__':
150 elif wx.Platform == '__WXMAC__':
486 faces = { 'times': 'Times New Roman',
151 faces = { 'times': 'Times New Roman',
487 'mono' : 'Monaco',
152 'mono' : 'Monaco',
488 'helv' : 'Arial',
153 'helv' : 'Arial',
489 'other': 'Comic Sans MS',
154 'other': 'Comic Sans MS',
490 'size' : 10,
155 'size' : 10,
491 'size2': 8,
156 'size2': 8,
492 }
157 }
493 else:
158 else:
494 faces = { 'times': 'Times',
159 faces = { 'times': 'Times',
495 'mono' : 'Courier',
160 'mono' : 'Courier',
496 'helv' : 'Helvetica',
161 'helv' : 'Helvetica',
497 'other': 'new century schoolbook',
162 'other': 'new century schoolbook',
498 'size' : 10,
163 'size' : 10,
499 'size2': 8,
164 'size2': 8,
500 }
165 }
501
166
502 #We draw a line at position 80
167 #We draw a line at position 80
503 self.SetEdgeMode(stc.STC_EDGE_LINE)
168 self.SetEdgeMode(stc.STC_EDGE_LINE)
504 self.SetEdgeColumn(80)
169 self.SetEdgeColumn(80)
505 self.SetEdgeColour(wx.LIGHT_GREY)
170 self.SetEdgeColour(wx.LIGHT_GREY)
506
171
507 #self.SetViewWhiteSpace(True)
172 #self.SetViewWhiteSpace(True)
508 #self.SetViewEOL(True)
173 #self.SetViewEOL(True)
509 self.SetEOLMode(stc.STC_EOL_CRLF)
174 self.SetEOLMode(stc.STC_EOL_CRLF)
510 #self.SetWrapMode(stc.STC_WRAP_CHAR)
175 #self.SetWrapMode(stc.STC_WRAP_CHAR)
511 #self.SetWrapMode(stc.STC_WRAP_WORD)
176 #self.SetWrapMode(stc.STC_WRAP_WORD)
512 self.SetBufferedDraw(True)
177 self.SetBufferedDraw(True)
513 #self.SetUseAntiAliasing(True)
178 #self.SetUseAntiAliasing(True)
514 self.SetLayoutCache(stc.STC_CACHE_PAGE)
179 self.SetLayoutCache(stc.STC_CACHE_PAGE)
515
180
516 self.EnsureCaretVisible()
181 self.EnsureCaretVisible()
517
182
518 self.SetMargins(3,3) #text is moved away from border with 3px
183 self.SetMargins(3,3) #text is moved away from border with 3px
519 # Suppressing Scintilla margins
184 # Suppressing Scintilla margins
520 self.SetMarginWidth(0,0)
185 self.SetMarginWidth(0,0)
521 self.SetMarginWidth(1,0)
186 self.SetMarginWidth(1,0)
522 self.SetMarginWidth(2,0)
187 self.SetMarginWidth(2,0)
523
188
524 # make some styles
189 # make some styles
525 if background_color != "BLACK":
190 if background_color != "BLACK":
526 self.background_color = "WHITE"
191 self.background_color = "WHITE"
527 self.SetCaretForeground("BLACK")
192 self.SetCaretForeground("BLACK")
528 self.ANSI_STYLES = self.ANSI_STYLES_WHITE
193 self.ANSI_STYLES = self.ANSI_STYLES_WHITE
529 else:
194 else:
530 self.background_color = background_color
195 self.background_color = background_color
531 self.SetCaretForeground("WHITE")
196 self.SetCaretForeground("WHITE")
532 self.ANSI_STYLES = self.ANSI_STYLES_BLACK
197 self.ANSI_STYLES = self.ANSI_STYLES_BLACK
533
198
534 self.StyleSetSpec(stc.STC_STYLE_DEFAULT, "fore:%s,back:%s,size:%d,face:%s" % (self.ANSI_STYLES['0;30'][1],
199 self.StyleSetSpec(stc.STC_STYLE_DEFAULT, "fore:%s,back:%s,size:%d,face:%s" % (self.ANSI_STYLES['0;30'][1],
535 self.background_color,
200 self.background_color,
536 faces['size'], faces['mono']))
201 faces['size'], faces['mono']))
537 self.StyleClearAll()
202 self.StyleClearAll()
538 self.StyleSetSpec(stc.STC_STYLE_BRACELIGHT, "fore:#FF0000,back:#0000FF,bold")
203 self.StyleSetSpec(stc.STC_STYLE_BRACELIGHT, "fore:#FF0000,back:#0000FF,bold")
539 self.StyleSetSpec(stc.STC_STYLE_BRACEBAD, "fore:#000000,back:#FF0000,bold")
204 self.StyleSetSpec(stc.STC_STYLE_BRACEBAD, "fore:#000000,back:#FF0000,bold")
540
205
541 for style in self.ANSI_STYLES.values():
206 for style in self.ANSI_STYLES.values():
542 self.StyleSetSpec(style[0], "bold,fore:%s" % style[1])
207 self.StyleSetSpec(style[0], "bold,fore:%s" % style[1])
543
208
544 #######################################################################
209 #######################################################################
545
210
546 self.indent = 0
211 self.indent = 0
547 self.prompt_count = 0
212 self.prompt_count = 0
548 self.color_pat = re.compile('\x01?\x1b\[(.*?)m\x02?')
213 self.color_pat = re.compile('\x01?\x1b\[(.*?)m\x02?')
549
214
550 self.write(intro)
215 self.write(intro)
551 self.setPrompt(prompt)
216 self.setPrompt(prompt)
552 self.showPrompt()
217 self.showPrompt()
553
218
554 self.Bind(wx.EVT_KEY_DOWN, self._onKeypress, self)
219 self.Bind(wx.EVT_KEY_DOWN, self._onKeypress, self)
555 #self.Bind(stc.EVT_STC_UPDATEUI, self.OnUpdateUI)
220 #self.Bind(stc.EVT_STC_UPDATEUI, self.OnUpdateUI)
556
221
557 def write(self, text):
222 def write(self, text):
558 '''
223 '''
559 Write given text to buffer.
224 Write given text to buffer.
560
225
561 @param text: Text to append.
226 @param text: Text to append.
562 @type text: string
227 @type text: string
563 '''
228 '''
564 segments = self.color_pat.split(text)
229 segments = self.color_pat.split(text)
565 segment = segments.pop(0)
230 segment = segments.pop(0)
566 self.StartStyling(self.getCurrentLineEnd(),0xFF)
231 self.StartStyling(self.getCurrentLineEnd(),0xFF)
567 self.AppendText(segment)
232 self.AppendText(segment)
568
233
569 if segments:
234 if segments:
570 ansi_tags = self.color_pat.findall(text)
235 ansi_tags = self.color_pat.findall(text)
571
236
572 for tag in ansi_tags:
237 for tag in ansi_tags:
573 i = segments.index(tag)
238 i = segments.index(tag)
574 self.StartStyling(self.getCurrentLineEnd(),0xFF)
239 self.StartStyling(self.getCurrentLineEnd(),0xFF)
575 self.AppendText(segments[i+1])
240 self.AppendText(segments[i+1])
576
241
577 if tag != '0':
242 if tag != '0':
578 self.SetStyling(len(segments[i+1]),self.ANSI_STYLES[tag][0])
243 self.SetStyling(len(segments[i+1]),self.ANSI_STYLES[tag][0])
579
244
580 segments.pop(i)
245 segments.pop(i)
581
246
582 self.moveCursor(self.getCurrentLineEnd())
247 self.moveCursor(self.getCurrentLineEnd())
583
248
584 def getPromptLen(self):
249 def getPromptLen(self):
585 '''
250 '''
586 Return the length of current prompt
251 Return the length of current prompt
587 '''
252 '''
588 return len(str(self.prompt_count)) + 7
253 return len(str(self.prompt_count)) + 7
589
254
590 def setPrompt(self,prompt):
255 def setPrompt(self,prompt):
591 self.prompt = prompt
256 self.prompt = prompt
592
257
593 def setIndentation(self,indentation):
258 def setIndentation(self,indentation):
594 self.indent = indentation
259 self.indent = indentation
595
260
596 def setPromptCount(self,count):
261 def setPromptCount(self,count):
597 self.prompt_count = count
262 self.prompt_count = count
598
263
599 def showPrompt(self):
264 def showPrompt(self):
600 '''
265 '''
601 Prints prompt at start of line.
266 Prints prompt at start of line.
602
267
603 @param prompt: Prompt to print.
268 @param prompt: Prompt to print.
604 @type prompt: string
269 @type prompt: string
605 '''
270 '''
606 self.write(self.prompt)
271 self.write(self.prompt)
607 #now we update the position of end of prompt
272 #now we update the position of end of prompt
608 self.current_start = self.getCurrentLineEnd()
273 self.current_start = self.getCurrentLineEnd()
609
274
610 autoindent = self.indent*' '
275 autoindent = self.indent*' '
611 autoindent = autoindent.replace(' ','\t')
276 autoindent = autoindent.replace(' ','\t')
612 self.write(autoindent)
277 self.write(autoindent)
613
278
614 def changeLine(self, text):
279 def changeLine(self, text):
615 '''
280 '''
616 Replace currently entered command line with given text.
281 Replace currently entered command line with given text.
617
282
618 @param text: Text to use as replacement.
283 @param text: Text to use as replacement.
619 @type text: string
284 @type text: string
620 '''
285 '''
621 self.SetSelection(self.getCurrentPromptStart(),self.getCurrentLineEnd())
286 self.SetSelection(self.getCurrentPromptStart(),self.getCurrentLineEnd())
622 self.ReplaceSelection(text)
287 self.ReplaceSelection(text)
623 self.moveCursor(self.getCurrentLineEnd())
288 self.moveCursor(self.getCurrentLineEnd())
624
289
625 def getCurrentPromptStart(self):
290 def getCurrentPromptStart(self):
626 return self.current_start
291 return self.current_start
627
292
628 def getCurrentLineStart(self):
293 def getCurrentLineStart(self):
629 return self.GotoLine(self.LineFromPosition(self.GetCurrentPos()))
294 return self.GotoLine(self.LineFromPosition(self.GetCurrentPos()))
630
295
631 def getCurrentLineEnd(self):
296 def getCurrentLineEnd(self):
632 return self.GetLength()
297 return self.GetLength()
633
298
634 def getCurrentLine(self):
299 def getCurrentLine(self):
635 '''
300 '''
636 Get text in current command line.
301 Get text in current command line.
637
302
638 @return: Text of current command line.
303 @return: Text of current command line.
639 @rtype: string
304 @rtype: string
640 '''
305 '''
641 return self.GetTextRange(self.getCurrentPromptStart(),
306 return self.GetTextRange(self.getCurrentPromptStart(),
642 self.getCurrentLineEnd())
307 self.getCurrentLineEnd())
643
308
644 def showReturned(self, text):
309 def showReturned(self, text):
645 '''
310 '''
646 Show returned text from last command and print new prompt.
311 Show returned text from last command and print new prompt.
647
312
648 @param text: Text to show.
313 @param text: Text to show.
649 @type text: string
314 @type text: string
650 '''
315 '''
651 self.write('\n'+text)
316 self.write('\n'+text)
652 if text:
317 if text:
653 self.write('\n')
318 self.write('\n')
654 self.showPrompt()
319 self.showPrompt()
655
320
656 def moveCursorOnNewValidKey(self):
321 def moveCursorOnNewValidKey(self):
657 #If cursor is at wrong position put it at last line...
322 #If cursor is at wrong position put it at last line...
658 if self.GetCurrentPos() < self.getCurrentPromptStart():
323 if self.GetCurrentPos() < self.getCurrentPromptStart():
659 self.GotoPos(self.getCurrentPromptStart())
324 self.GotoPos(self.getCurrentPromptStart())
660
325
661 def removeFromTo(self,from_pos,to_pos):
326 def removeFromTo(self,from_pos,to_pos):
662 if from_pos < to_pos:
327 if from_pos < to_pos:
663 self.SetSelection(from_pos,to_pos)
328 self.SetSelection(from_pos,to_pos)
664 self.DeleteBack()
329 self.DeleteBack()
665
330
666 def removeCurrentLine(self):
331 def removeCurrentLine(self):
667 self.LineDelete()
332 self.LineDelete()
668
333
669 def moveCursor(self,position):
334 def moveCursor(self,position):
670 self.GotoPos(position)
335 self.GotoPos(position)
671
336
672 def getCursorPos(self):
337 def getCursorPos(self):
673 return self.GetCurrentPos()
338 return self.GetCurrentPos()
674
339
675 def selectFromTo(self,from_pos,to_pos):
340 def selectFromTo(self,from_pos,to_pos):
676 self.SetSelectionStart(from_pos)
341 self.SetSelectionStart(from_pos)
677 self.SetSelectionEnd(to_pos)
342 self.SetSelectionEnd(to_pos)
678
343
679 def writeHistory(self,history):
344 def writeHistory(self,history):
680 self.removeFromTo(self.getCurrentPromptStart(),self.getCurrentLineEnd())
345 self.removeFromTo(self.getCurrentPromptStart(),self.getCurrentLineEnd())
681 self.changeLine(history)
346 self.changeLine(history)
682
347
683 def writeCompletion(self, possibilities):
348 def writeCompletion(self, possibilities):
684 max_len = len(max(possibilities,key=len))
349 max_len = len(max(possibilities,key=len))
685 max_symbol =' '*max_len
350 max_symbol =' '*max_len
686
351
687 #now we check how much symbol we can put on a line...
352 #now we check how much symbol we can put on a line...
688 cursor_pos = self.getCursorPos()
353 cursor_pos = self.getCursorPos()
689 test_buffer = max_symbol + ' '*4
354 test_buffer = max_symbol + ' '*4
690 current_lines = self.GetLineCount()
355 current_lines = self.GetLineCount()
691
356
692 allowed_symbols = 80/len(test_buffer)
357 allowed_symbols = 80/len(test_buffer)
693 if allowed_symbols == 0:
358 if allowed_symbols == 0:
694 allowed_symbols = 1
359 allowed_symbols = 1
695
360
696 pos = 1
361 pos = 1
697 buf = ''
362 buf = ''
698 for symbol in possibilities:
363 for symbol in possibilities:
699 #buf += symbol+'\n'#*spaces)
364 #buf += symbol+'\n'#*spaces)
700 if pos<allowed_symbols:
365 if pos<allowed_symbols:
701 spaces = max_len - len(symbol) + 4
366 spaces = max_len - len(symbol) + 4
702 buf += symbol+' '*spaces
367 buf += symbol+' '*spaces
703 pos += 1
368 pos += 1
704 else:
369 else:
705 buf+=symbol+'\n'
370 buf+=symbol+'\n'
706 pos = 1
371 pos = 1
707 self.write(buf)
372 self.write(buf)
708
373
709 def _onKeypress(self, event, skip=True):
374 def _onKeypress(self, event, skip=True):
710 '''
375 '''
711 Key press callback used for correcting behavior for console-like
376 Key press callback used for correcting behavior for console-like
712 interfaces. For example 'home' should go to prompt, not to begining of
377 interfaces. For example 'home' should go to prompt, not to begining of
713 line.
378 line.
714
379
715 @param widget: Widget that key press accored in.
380 @param widget: Widget that key press accored in.
716 @type widget: gtk.Widget
381 @type widget: gtk.Widget
717 @param event: Event object
382 @param event: Event object
718 @type event: gtk.gdk.Event
383 @type event: gtk.gdk.Event
719
384
720 @return: Return True if event as been catched.
385 @return: Return True if event as been catched.
721 @rtype: boolean
386 @rtype: boolean
722 '''
387 '''
723
388
724 if event.GetKeyCode() == wx.WXK_HOME:
389 if event.GetKeyCode() == wx.WXK_HOME:
725 if event.Modifiers == wx.MOD_NONE:
390 if event.Modifiers == wx.MOD_NONE:
726 self.moveCursorOnNewValidKey()
391 self.moveCursorOnNewValidKey()
727 self.moveCursor(self.getCurrentPromptStart())
392 self.moveCursor(self.getCurrentPromptStart())
728 return True
393 return True
729 elif event.Modifiers == wx.MOD_SHIFT:
394 elif event.Modifiers == wx.MOD_SHIFT:
730 self.moveCursorOnNewValidKey()
395 self.moveCursorOnNewValidKey()
731 self.selectFromTo(self.getCurrentPromptStart(),self.getCursorPos())
396 self.selectFromTo(self.getCurrentPromptStart(),self.getCursorPos())
732 return True
397 return True
733 else:
398 else:
734 return False
399 return False
735
400
736 elif event.GetKeyCode() == wx.WXK_LEFT:
401 elif event.GetKeyCode() == wx.WXK_LEFT:
737 if event.Modifiers == wx.MOD_NONE:
402 if event.Modifiers == wx.MOD_NONE:
738 self.moveCursorOnNewValidKey()
403 self.moveCursorOnNewValidKey()
739
404
740 self.moveCursor(self.getCursorPos()-1)
405 self.moveCursor(self.getCursorPos()-1)
741 if self.getCursorPos() < self.getCurrentPromptStart():
406 if self.getCursorPos() < self.getCurrentPromptStart():
742 self.moveCursor(self.getCurrentPromptStart())
407 self.moveCursor(self.getCurrentPromptStart())
743 return True
408 return True
744
409
745 elif event.GetKeyCode() == wx.WXK_BACK:
410 elif event.GetKeyCode() == wx.WXK_BACK:
746 self.moveCursorOnNewValidKey()
411 self.moveCursorOnNewValidKey()
747 if self.getCursorPos() > self.getCurrentPromptStart():
412 if self.getCursorPos() > self.getCurrentPromptStart():
748 self.removeFromTo(self.getCursorPos()-1,self.getCursorPos())
413 self.removeFromTo(self.getCursorPos()-1,self.getCursorPos())
749 return True
414 return True
750
415
751 if skip:
416 if skip:
752 if event.GetKeyCode() not in [wx.WXK_PAGEUP,wx.WXK_PAGEDOWN] and event.Modifiers == wx.MOD_NONE:
417 if event.GetKeyCode() not in [wx.WXK_PAGEUP,wx.WXK_PAGEDOWN] and event.Modifiers == wx.MOD_NONE:
753 self.moveCursorOnNewValidKey()
418 self.moveCursorOnNewValidKey()
754
419
755 event.Skip()
420 event.Skip()
756 return True
421 return True
757 return False
422 return False
758
423
759 def OnUpdateUI(self, evt):
424 def OnUpdateUI(self, evt):
760 # check for matching braces
425 # check for matching braces
761 braceAtCaret = -1
426 braceAtCaret = -1
762 braceOpposite = -1
427 braceOpposite = -1
763 charBefore = None
428 charBefore = None
764 caretPos = self.GetCurrentPos()
429 caretPos = self.GetCurrentPos()
765
430
766 if caretPos > 0:
431 if caretPos > 0:
767 charBefore = self.GetCharAt(caretPos - 1)
432 charBefore = self.GetCharAt(caretPos - 1)
768 styleBefore = self.GetStyleAt(caretPos - 1)
433 styleBefore = self.GetStyleAt(caretPos - 1)
769
434
770 # check before
435 # check before
771 if charBefore and chr(charBefore) in "[]{}()" and styleBefore == stc.STC_P_OPERATOR:
436 if charBefore and chr(charBefore) in "[]{}()" and styleBefore == stc.STC_P_OPERATOR:
772 braceAtCaret = caretPos - 1
437 braceAtCaret = caretPos - 1
773
438
774 # check after
439 # check after
775 if braceAtCaret < 0:
440 if braceAtCaret < 0:
776 charAfter = self.GetCharAt(caretPos)
441 charAfter = self.GetCharAt(caretPos)
777 styleAfter = self.GetStyleAt(caretPos)
442 styleAfter = self.GetStyleAt(caretPos)
778
443
779 if charAfter and chr(charAfter) in "[]{}()" and styleAfter == stc.STC_P_OPERATOR:
444 if charAfter and chr(charAfter) in "[]{}()" and styleAfter == stc.STC_P_OPERATOR:
780 braceAtCaret = caretPos
445 braceAtCaret = caretPos
781
446
782 if braceAtCaret >= 0:
447 if braceAtCaret >= 0:
783 braceOpposite = self.BraceMatch(braceAtCaret)
448 braceOpposite = self.BraceMatch(braceAtCaret)
784
449
785 if braceAtCaret != -1 and braceOpposite == -1:
450 if braceAtCaret != -1 and braceOpposite == -1:
786 self.BraceBadLight(braceAtCaret)
451 self.BraceBadLight(braceAtCaret)
787 else:
452 else:
788 self.BraceHighlight(braceAtCaret, braceOpposite)
453 self.BraceHighlight(braceAtCaret, braceOpposite)
789 #pt = self.PointFromPosition(braceOpposite)
454 #pt = self.PointFromPosition(braceOpposite)
790 #self.Refresh(True, wxRect(pt.x, pt.y, 5,5))
455 #self.Refresh(True, wxRect(pt.x, pt.y, 5,5))
791 #print pt
456 #print pt
792 #self.Refresh(False)
457 #self.Refresh(False)
793
458
794 class WxIPythonViewPanel(wx.Panel):
459 class WxIPythonViewPanel(wx.Panel):
795 '''
460 '''
796 This is wx.Panel that embbed the IPython Thread and the wx.StyledTextControl
461 This is wx.Panel that embbed the IPython Thread and the wx.StyledTextControl
797 If you want to port this to any other GUI toolkit, just replace the WxConsoleView
462 If you want to port this to any other GUI toolkit, just replace the WxConsoleView
798 by YOURGUIConsoleView and make YOURGUIIPythonView derivate from whatever container you want.
463 by YOURGUIConsoleView and make YOURGUIIPythonView derivate from whatever container you want.
799 I've choosed to derivate from a wx.Panel because it seems to be ore usefull
464 I've choosed to derivate from a wx.Panel because it seems to be ore usefull
800 Any idea to make it more 'genric' welcomed.
465 Any idea to make it more 'genric' welcomed.
801 '''
466 '''
802 def __init__(self,parent,exit_handler=None,intro=None,background_color="BLACK"):
467 def __init__(self,parent,exit_handler=None,intro=None,
468 background_color="BLACK",add_button_handler=None):
803 '''
469 '''
804 Initialize.
470 Initialize.
805 Instanciate an IPython thread.
471 Instanciate an IPython thread.
806 Instanciate a WxConsoleView.
472 Instanciate a WxConsoleView.
807 Redirect I/O to console.
473 Redirect I/O to console.
808 '''
474 '''
809 wx.Panel.__init__(self,parent,-1)
475 wx.Panel.__init__(self,parent,-1)
810
476
811 ### IPython thread instanciation ###
477 ### IPython thread instanciation ###
812 self.cout = StringIO()
478 self.cout = StringIO()
813 self.IP = IterableIPShell(cout=self.cout,cerr=self.cout,
479
480 self.add_button_handler = add_button_handler
481 self.exit_handler = exit_handler
482
483 self.IP = WxIterableIPShell(self,
484 cout=self.cout,cerr=self.cout,
814 exit_handler = exit_handler,
485 exit_handler = exit_handler,
815 time_loop = 0.1)
486 time_loop = 0.1)
816 self.IP.start()
487 self.IP.start()
817
488
818 ### IPython wx console view instanciation ###
489 ### IPython wx console view instanciation ###
819 #If user didn't defined an intro text, we create one for him
490 #If user didn't defined an intro text, we create one for him
820 #If you really wnat an empty intrp just call wxIPythonViewPanel with intro=''
491 #If you really wnat an empty intrp just call wxIPythonViewPanel with intro=''
821 if intro == None:
492 if intro == None:
822 welcome_text = "Welcome to WxIPython Shell.\n\n"
493 welcome_text = "Welcome to WxIPython Shell.\n\n"
823 welcome_text+= self.IP.getBanner()
494 welcome_text+= self.IP.getBanner()
824 welcome_text+= "!command -> Execute command in shell\n"
495 welcome_text+= "!command -> Execute command in shell\n"
825 welcome_text+= "TAB -> Autocompletion\n"
496 welcome_text+= "TAB -> Autocompletion\n"
826
497
827 self.text_ctrl = WxConsoleView(self,
498 self.text_ctrl = WxConsoleView(self,
828 self.IP.getPrompt(),
499 self.IP.getPrompt(),
829 intro=welcome_text,
500 intro=welcome_text,
830 background_color=background_color)
501 background_color=background_color)
831
502
832 self.text_ctrl.Bind(wx.EVT_KEY_DOWN, self.keyPress, self.text_ctrl)
503 self.text_ctrl.Bind(wx.EVT_KEY_DOWN, self.keyPress, self.text_ctrl)
833
504
834 ### making the layout of the panel ###
505 ### making the layout of the panel ###
835 sizer = wx.BoxSizer(wx.VERTICAL)
506 sizer = wx.BoxSizer(wx.VERTICAL)
836 sizer.Add(self.text_ctrl, 1, wx.EXPAND)
507 sizer.Add(self.text_ctrl, 1, wx.EXPAND)
837 self.SetAutoLayout(True)
508 self.SetAutoLayout(True)
838 sizer.Fit(self)
509 sizer.Fit(self)
839 sizer.SetSizeHints(self)
510 sizer.SetSizeHints(self)
840 self.SetSizer(sizer)
511 self.SetSizer(sizer)
841 #and we focus on the widget :)
512 #and we focus on the widget :)
842 self.SetFocus()
513 self.SetFocus()
843
514
844 ### below are the thread communication variable ###
515 ### below are the thread communication variable ###
845 # the IPython thread is managed via unidirectional communication.
516 # the IPython thread is managed via unidirectional communication.
846 # It's a thread slave that can't interact by itself with the GUI.
517 # It's a thread slave that can't interact by itself with the GUI.
847 # When the GUI event loop is done runStateMachine() is called and the thread sate is then
518 # When the GUI event loop is done runStateMachine() is called and the thread sate is then
848 # managed.
519 # managed.
849
520
850 #Initialize the state machine #kept for information
521 #Initialize the state machine #kept for information
851 #self.states = ['IDLE',
522 #self.states = ['IDLE',
852 # 'DO_EXECUTE_LINE',
523 # 'DO_EXECUTE_LINE',
853 # 'WAIT_END_OF_EXECUTION',
524 # 'WAIT_END_OF_EXECUTION',
854 # 'SHOW_DOC',
525 # 'SHOW_DOC',
855 # 'SHOW_PROMPT']
526 # 'SHOW_PROMPT']
856
527
857 self.cur_state = 'IDLE'
528 self.cur_state = 'IDLE'
858 self.pager_state = 'DONE'
529 self.pager_state = 'DONE'
859 #wx.CallAfter(self.runStateMachine)
530 #wx.CallAfter(self.runStateMachine)
860
531
861 # This creates a new Event class and a EVT binder function
532 # This creates a new Event class and a EVT binder function
862 (self.AskExitEvent, EVT_ASK_EXIT) = wx.lib.newevent.NewEvent()
533 #(self.AskExitEvent, EVT_ASK_EXIT) = wx.lib.newevent.NewEvent()
534 #(self.AddButtonEvent, EVT_ADDBUTTON_EXIT) = wx.lib.newevent.NewEvent()
535
863
536
864 self.Bind(wx.EVT_IDLE, self.runStateMachine)
537 #self.Bind(wx.EVT_IDLE, self.runStateMachine)
865 self.Bind(EVT_ASK_EXIT, exit_handler)
866
538
867 def __del__(self):
539 def __del__(self):
868 self.IP.shutdown()
540 self.IP.shutdown()
869 self.IP.join()
541 self.IP.join()
870 WxConsoleView.__del__()
542 WxConsoleView.__del__()
871
543
872 #---------------------------- IPython Thread Management ---------------------------------------
544 #---------------------------- IPython Thread Management ---------------------------------------
873 def runStateMachine(self,event):
545 def stateDoExecuteLine(self):
874 #print >>self.sys_stdout,"state:",self.cur_state
875 self.updateStatusTracker(self.cur_state)
876
877 if self.cur_state == 'DO_EXECUTE_LINE':
878 #print >>self.sys_stdout,"command:",self.getCurrentLine()
546 #print >>self.sys_stdout,"command:",self.getCurrentLine()
879 self.IP.doExecute(self.text_ctrl.getCurrentLine().replace('\t',' '*4))
547 self.doExecuteLine(self.text_ctrl.getCurrentLine())
548
549 def doExecuteLine(self,line):
550 print >>sys.__stdout__,"command:",line
551 self.IP.doExecute(line.replace('\t',' '*4))
880 self.updateHistoryTracker(self.text_ctrl.getCurrentLine())
552 self.updateHistoryTracker(self.text_ctrl.getCurrentLine())
881 self.cur_state = 'WAIT_END_OF_EXECUTION'
553 self.cur_state = 'WAIT_END_OF_EXECUTION'
882
554
883 if self.cur_state == 'WAIT_END_OF_EXECUTION':
555
884 if self.IP.isExecuteDone():
556 def evtStateExecuteDone(self,evt):
885 self.doc = self.IP.getDocText()
557 self.doc = self.IP.getDocText()
886 if self.IP.getAskExit():
887 evt = self.AskExitEvent()
888 wx.PostEvent(self, evt)
889 self.IP.clearAskExit()
890 if self.doc:
558 if self.doc:
891 self.pager_state = 'INIT'
559 self.pager_state = 'INIT'
892 self.cur_state = 'SHOW_DOC'
560 self.cur_state = 'SHOW_DOC'
561 self.pager(self.doc)
562 #if self.pager_state == 'DONE':
563
893 else:
564 else:
894 self.cur_state = 'SHOW_PROMPT'
565 self.stateShowPrompt()
895
566
896 if self.cur_state == 'SHOW_PROMPT':
567 def stateShowPrompt(self):
568 self.cur_state = 'SHOW_PROMPT'
897 self.text_ctrl.setPrompt(self.IP.getPrompt())
569 self.text_ctrl.setPrompt(self.IP.getPrompt())
898 self.text_ctrl.setIndentation(self.IP.getIndentation())
570 self.text_ctrl.setIndentation(self.IP.getIndentation())
899 self.text_ctrl.setPromptCount(self.IP.getPromptCount())
571 self.text_ctrl.setPromptCount(self.IP.getPromptCount())
900 rv = self.cout.getvalue()
572 rv = self.cout.getvalue()
901 if rv: rv = rv.strip('\n')
573 if rv: rv = rv.strip('\n')
902 self.text_ctrl.showReturned(rv)
574 self.text_ctrl.showReturned(rv)
903 self.cout.truncate(0)
575 self.cout.truncate(0)
904 self.IP.initHistoryIndex()
576 self.IP.initHistoryIndex()
905 self.cur_state = 'IDLE'
577 self.cur_state = 'IDLE'
906
578
907 if self.cur_state == 'SHOW_DOC':
579 ## def runStateMachine(self,event):
908 self.pager(self.doc)
580 ## #print >>self.sys_stdout,"state:",self.cur_state
909 if self.pager_state == 'DONE':
581 ## self.updateStatusTracker(self.cur_state)
910 self.cur_state = 'SHOW_PROMPT'
582 ##
911
583 ## #if self.cur_state == 'DO_EXECUTE_LINE':
912 event.Skip()
584 ## # self.doExecuteLine()
585 ##
586 ## if self.cur_state == 'WAIT_END_OF_EXECUTION':
587 ## if self.IP.isExecuteDone():
588 ## #self.button = self.IP.getAddButton()
589 ## #if self.IP.getAskExit():
590 ## # evt = self.AskExitEvent()
591 ## # wx.PostEvent(self, evt)
592 ## # self.IP.clearAskExit()
593 ## self.doc = self.IP.getDocText()
594 ## if self.doc:
595 ## self.pager_state = 'INIT'
596 ## self.cur_state = 'SHOW_DOC'
597 ## #if self.button:
598 ## #self.IP.doExecute('print "cool"')#self.button['func'])
599 ## #self.updateHistoryTracker(self.text_ctrl.getCurrentLine())
600 ##
601 ## # self.button['func']='print "cool!"'
602 ## # self.add_button_handler(self.button)
603 ## # self.IP.shortcutProcessed()
604 ##
605 ## else:
606 ## self.cur_state = 'SHOW_PROMPT'
607 ##
608 ## if self.cur_state == 'SHOW_PROMPT':
609 ## self.text_ctrl.setPrompt(self.IP.getPrompt())
610 ## self.text_ctrl.setIndentation(self.IP.getIndentation())
611 ## self.text_ctrl.setPromptCount(self.IP.getPromptCount())
612 ## rv = self.cout.getvalue()
613 ## if rv: rv = rv.strip('\n')
614 ## self.text_ctrl.showReturned(rv)
615 ## self.cout.truncate(0)
616 ## self.IP.initHistoryIndex()
617 ## self.cur_state = 'IDLE'
618 ##
619 ## if self.cur_state == 'SHOW_DOC':
620 ## self.pager(self.doc)
621 ## if self.pager_state == 'DONE':
622 ## self.cur_state = 'SHOW_PROMPT'
623 ##
624 ## event.Skip()
913
625
914 #---------------------------- IPython pager ---------------------------------------
626 #---------------------------- IPython pager ---------------------------------------
915 def pager(self,text):#,start=0,screen_lines=0,pager_cmd = None):
627 def pager(self,text):#,start=0,screen_lines=0,pager_cmd = None):
916 if self.pager_state == 'WAITING':
628 if self.pager_state == 'WAITING':
917 #print >>self.sys_stdout,"PAGER waiting"
629 #print >>self.sys_stdout,"PAGER waiting"
918 return
630 return
919
631
920 if self.pager_state == 'INIT':
632 if self.pager_state == 'INIT':
921 #print >>self.sys_stdout,"PAGER state:",self.pager_state
633 #print >>self.sys_stdout,"PAGER state:",self.pager_state
922 self.pager_lines = text[7:].split('\n')
634 self.pager_lines = text[7:].split('\n')
923 self.pager_nb_lines = len(self.pager_lines)
635 self.pager_nb_lines = len(self.pager_lines)
924 self.pager_index = 0
636 self.pager_index = 0
925 self.pager_do_remove = False
637 self.pager_do_remove = False
926 self.text_ctrl.write('\n')
638 self.text_ctrl.write('\n')
927 self.pager_state = 'PROCESS_LINES'
639 self.pager_state = 'PROCESS_LINES'
928
640
929 if self.pager_state == 'PROCESS_LINES':
641 if self.pager_state == 'PROCESS_LINES':
930 #print >>self.sys_stdout,"PAGER state:",self.pager_state
642 #print >>self.sys_stdout,"PAGER state:",self.pager_state
931 if self.pager_do_remove == True:
643 if self.pager_do_remove == True:
932 self.text_ctrl.removeCurrentLine()
644 self.text_ctrl.removeCurrentLine()
933 self.pager_do_remove = False
645 self.pager_do_remove = False
934
646
935 if self.pager_nb_lines > 10:
647 if self.pager_nb_lines > 10:
936 #print >>self.sys_stdout,"PAGER processing 10 lines"
648 #print >>self.sys_stdout,"PAGER processing 10 lines"
937 if self.pager_index > 0:
649 if self.pager_index > 0:
938 self.text_ctrl.write(">\x01\x1b[1;36m\x02"+self.pager_lines[self.pager_index]+'\n')
650 self.text_ctrl.write(">\x01\x1b[1;36m\x02"+self.pager_lines[self.pager_index]+'\n')
939 else:
651 else:
940 self.text_ctrl.write("\x01\x1b[1;36m\x02 "+self.pager_lines[self.pager_index]+'\n')
652 self.text_ctrl.write("\x01\x1b[1;36m\x02 "+self.pager_lines[self.pager_index]+'\n')
941
653
942 for line in self.pager_lines[self.pager_index+1:self.pager_index+9]:
654 for line in self.pager_lines[self.pager_index+1:self.pager_index+9]:
943 self.text_ctrl.write("\x01\x1b[1;36m\x02 "+line+'\n')
655 self.text_ctrl.write("\x01\x1b[1;36m\x02 "+line+'\n')
944 self.pager_index += 10
656 self.pager_index += 10
945 self.pager_nb_lines -= 10
657 self.pager_nb_lines -= 10
946 self.text_ctrl.write("--- Push Enter to continue or 'Q' to quit---")
658 self.text_ctrl.write("--- Push Enter to continue or 'Q' to quit---")
947 self.pager_do_remove = True
659 self.pager_do_remove = True
948 self.pager_state = 'WAITING'
660 self.pager_state = 'WAITING'
949 return
661 return
950 else:
662 else:
951 #print >>self.sys_stdout,"PAGER processing last lines"
663 #print >>self.sys_stdout,"PAGER processing last lines"
952 if self.pager_nb_lines > 0:
664 if self.pager_nb_lines > 0:
953 if self.pager_index > 0:
665 if self.pager_index > 0:
954 self.text_ctrl.write(">\x01\x1b[1;36m\x02"+self.pager_lines[self.pager_index]+'\n')
666 self.text_ctrl.write(">\x01\x1b[1;36m\x02"+self.pager_lines[self.pager_index]+'\n')
955 else:
667 else:
956 self.text_ctrl.write("\x01\x1b[1;36m\x02 "+self.pager_lines[self.pager_index]+'\n')
668 self.text_ctrl.write("\x01\x1b[1;36m\x02 "+self.pager_lines[self.pager_index]+'\n')
957
669
958 self.pager_index += 1
670 self.pager_index += 1
959 self.pager_nb_lines -= 1
671 self.pager_nb_lines -= 1
960 if self.pager_nb_lines > 0:
672 if self.pager_nb_lines > 0:
961 for line in self.pager_lines[self.pager_index:]:
673 for line in self.pager_lines[self.pager_index:]:
962 self.text_ctrl.write("\x01\x1b[1;36m\x02 "+line+'\n')
674 self.text_ctrl.write("\x01\x1b[1;36m\x02 "+line+'\n')
963 self.pager_nb_lines = 0
675 self.pager_nb_lines = 0
964 self.pager_state = 'DONE'
676 self.pager_state = 'DONE'
677 self.stateShowPrompt()
965
678
966 #---------------------------- Key Handler --------------------------------------------
679 #---------------------------- Key Handler --------------------------------------------
967 def keyPress(self, event):
680 def keyPress(self, event):
968 '''
681 '''
969 Key press callback with plenty of shell goodness, like history,
682 Key press callback with plenty of shell goodness, like history,
970 autocompletions, etc.
683 autocompletions, etc.
971 '''
684 '''
972
685
973 if event.GetKeyCode() == ord('C'):
686 if event.GetKeyCode() == ord('C'):
974 if event.Modifiers == wx.MOD_CONTROL:
687 if event.Modifiers == wx.MOD_CONTROL:
975 if self.cur_state == 'WAIT_END_OF_EXECUTION':
688 if self.cur_state == 'WAIT_END_OF_EXECUTION':
976 #we raise an exception inside the IPython thread container
689 #we raise an exception inside the IPython thread container
977 self.IP.raise_exc(KeyboardInterrupt)
690 self.IP.raise_exc(KeyboardInterrupt)
978 return
691 return
979
692
980 if event.KeyCode == wx.WXK_RETURN:
693 if event.KeyCode == wx.WXK_RETURN:
981 if self.cur_state == 'IDLE':
694 if self.cur_state == 'IDLE':
982 #we change the state ot the state machine
695 #we change the state ot the state machine
983 self.cur_state = 'DO_EXECUTE_LINE'
696 self.cur_state = 'DO_EXECUTE_LINE'
697 self.stateDoExecuteLine()
984 return
698 return
985 if self.pager_state == 'WAITING':
699 if self.pager_state == 'WAITING':
986 self.pager_state = 'PROCESS_LINES'
700 self.pager_state = 'PROCESS_LINES'
701 self.pager(self.doc)
987 return
702 return
988
703
989 if event.GetKeyCode() in [ord('q'),ord('Q')]:
704 if event.GetKeyCode() in [ord('q'),ord('Q')]:
990 if self.pager_state == 'WAITING':
705 if self.pager_state == 'WAITING':
991 self.pager_state = 'DONE'
706 self.pager_state = 'DONE'
707 self.stateShowPrompt()
992 return
708 return
993
709
994 #scroll_position = self.text_ctrl.GetScrollPos(wx.VERTICAL)
710 #scroll_position = self.text_ctrl.GetScrollPos(wx.VERTICAL)
995 if self.cur_state == 'IDLE':
711 if self.cur_state == 'IDLE':
996 if event.KeyCode == wx.WXK_UP:
712 if event.KeyCode == wx.WXK_UP:
997 history = self.IP.historyBack()
713 history = self.IP.historyBack()
998 self.text_ctrl.writeHistory(history)
714 self.text_ctrl.writeHistory(history)
999 return
715 return
1000 if event.KeyCode == wx.WXK_DOWN:
716 if event.KeyCode == wx.WXK_DOWN:
1001 history = self.IP.historyForward()
717 history = self.IP.historyForward()
1002 self.text_ctrl.writeHistory(history)
718 self.text_ctrl.writeHistory(history)
1003 return
719 return
1004 if event.KeyCode == wx.WXK_TAB:
720 if event.KeyCode == wx.WXK_TAB:
1005 #if line empty we disable tab completion
721 #if line empty we disable tab completion
1006 if not self.text_ctrl.getCurrentLine().strip():
722 if not self.text_ctrl.getCurrentLine().strip():
1007 self.text_ctrl.write('\t')
723 self.text_ctrl.write('\t')
1008 return
724 return
1009 completed, possibilities = self.IP.complete(self.text_ctrl.getCurrentLine())
725 completed, possibilities = self.IP.complete(self.text_ctrl.getCurrentLine())
1010 if len(possibilities) > 1:
726 if len(possibilities) > 1:
1011 cur_slice = self.text_ctrl.getCurrentLine()
727 cur_slice = self.text_ctrl.getCurrentLine()
1012 self.text_ctrl.write('\n')
728 self.text_ctrl.write('\n')
1013 self.text_ctrl.writeCompletion(possibilities)
729 self.text_ctrl.writeCompletion(possibilities)
1014 self.text_ctrl.write('\n')
730 self.text_ctrl.write('\n')
1015
731
1016 self.text_ctrl.showPrompt()
732 self.text_ctrl.showPrompt()
1017 self.text_ctrl.write(cur_slice)
733 self.text_ctrl.write(cur_slice)
1018 self.text_ctrl.changeLine(completed or cur_slice)
734 self.text_ctrl.changeLine(completed or cur_slice)
1019
735
1020 return
736 return
1021 event.Skip()
737 event.Skip()
1022
738
1023 #---------------------------- Hook Section --------------------------------------------
739 #---------------------------- Hook Section --------------------------------------------
1024 def updateHistoryTracker(self,command_line):
740 def updateHistoryTracker(self,command_line):
1025 '''
741 '''
1026 Default history tracker (does nothing)
742 Default history tracker (does nothing)
1027 '''
743 '''
1028 pass
744 pass
1029
745
1030 def setHistoryTrackerHook(self,func):
746 def setHistoryTrackerHook(self,func):
1031 '''
747 '''
1032 Define a new history tracker
748 Define a new history tracker
1033 '''
749 '''
1034 self.updateHistoryTracker = func
750 self.updateHistoryTracker = func
1035 def updateStatusTracker(self,status):
751 def updateStatusTracker(self,status):
1036 '''
752 '''
1037 Default status tracker (does nothing)
753 Default status tracker (does nothing)
1038 '''
754 '''
1039 pass
755 pass
1040
756
1041 def setStatusTrackerHook(self,func):
757 def setStatusTrackerHook(self,func):
1042 '''
758 '''
1043 Define a new status tracker
759 Define a new status tracker
1044 '''
760 '''
1045 self.updateStatusTracker = func
761 self.updateStatusTracker = func
1046
762
@@ -1,201 +1,199 b''
1 #!/usr/bin/python
1 #!/usr/bin/python
2 # -*- coding: iso-8859-15 -*-
2 # -*- coding: iso-8859-15 -*-
3
3
4 import wx.aui
4 import wx.aui
5 import wx.py
5 import wx.py
6 from wx.lib.wordwrap import wordwrap
6 from wx.lib.wordwrap import wordwrap
7
7
8 from ipython_view import *
8 from ipython_view import *
9 from ipython_history import *
9 from ipython_history import *
10
10
11 __version__ = 0.8
11 __version__ = 0.8
12 __author__ = "Laurent Dufrechou"
12 __author__ = "Laurent Dufrechou"
13 __email__ = "laurent.dufrechou _at_ gmail.com"
13 __email__ = "laurent.dufrechou _at_ gmail.com"
14 __license__ = "BSD"
14 __license__ = "BSD"
15
15
16 #-----------------------------------------
16 #-----------------------------------------
17 # Creating one main frame for our
17 # Creating one main frame for our
18 # application with movables windows
18 # application with movables windows
19 #-----------------------------------------
19 #-----------------------------------------
20 class MyFrame(wx.Frame):
20 class MyFrame(wx.Frame):
21 """Creating one main frame for our
21 """Creating one main frame for our
22 application with movables windows"""
22 application with movables windows"""
23 def __init__(self, parent=None, id=-1, title="WxIPython", pos=wx.DefaultPosition,
23 def __init__(self, parent=None, id=-1, title="WxIPython", pos=wx.DefaultPosition,
24 size=(800, 600), style=wx.DEFAULT_FRAME_STYLE):
24 size=(800, 600), style=wx.DEFAULT_FRAME_STYLE):
25 wx.Frame.__init__(self, parent, id, title, pos, size, style)
25 wx.Frame.__init__(self, parent, id, title, pos, size, style)
26 self._mgr = wx.aui.AuiManager()
26 self._mgr = wx.aui.AuiManager()
27
27
28 # notify PyAUI which frame to use
28 # notify PyAUI which frame to use
29 self._mgr.SetManagedWindow(self)
29 self._mgr.SetManagedWindow(self)
30
30
31 #create differents panels and make them persistant
31 #create differents panels and make them persistant
32 self.history_panel = IPythonHistoryPanel(self)
32 self.history_panel = IPythonHistoryPanel(self)
33
33
34 self.ipython_panel = WxIPythonViewPanel(self,self.OnExitDlg,
34 self.ipython_panel = WxIPythonViewPanel(self,self.OnExitDlg,
35 background_color = "BLACK")
35 background_color = "BLACK")
36
36
37 #self.ipython_panel = WxIPythonViewPanel(self,self.OnExitDlg,
37 #self.ipython_panel = WxIPythonViewPanel(self,self.OnExitDlg,
38 # background_color = "WHITE")
38 # background_color = "WHITE")
39
39
40 self.ipython_panel.setHistoryTrackerHook(self.history_panel.write)
40 self.ipython_panel.setHistoryTrackerHook(self.history_panel.write)
41 self.ipython_panel.setStatusTrackerHook(self.updateStatus)
41 self.ipython_panel.setStatusTrackerHook(self.updateStatus)
42
42
43 self.statusbar = self.createStatus()
43 self.statusbar = self.createStatus()
44 self.createMenu()
44 self.createMenu()
45
45
46 ########################################################################
46 ########################################################################
47 ### add the panes to the manager
47 ### add the panes to the manager
48 # main panels
48 # main panels
49 self._mgr.AddPane(self.ipython_panel , wx.CENTER, "IPython Shell")
49 self._mgr.AddPane(self.ipython_panel , wx.CENTER, "IPython Shell")
50 self._mgr.AddPane(self.history_panel , wx.RIGHT, "IPython history")
50 self._mgr.AddPane(self.history_panel , wx.RIGHT, "IPython history")
51
51
52 # now we specify some panel characteristics
52 # now we specify some panel characteristics
53 self._mgr.GetPane(self.ipython_panel).CaptionVisible(True);
53 self._mgr.GetPane(self.ipython_panel).CaptionVisible(True);
54 self._mgr.GetPane(self.history_panel).CaptionVisible(True);
54 self._mgr.GetPane(self.history_panel).CaptionVisible(True);
55 self._mgr.GetPane(self.history_panel).MinSize((200,400));
55 self._mgr.GetPane(self.history_panel).MinSize((200,400));
56
56
57
58
59 # tell the manager to "commit" all the changes just made
57 # tell the manager to "commit" all the changes just made
60 self._mgr.Update()
58 self._mgr.Update()
61
59
62 #global event handling
60 #global event handling
63 self.Bind(wx.EVT_CLOSE, self.OnClose)
61 self.Bind(wx.EVT_CLOSE, self.OnClose)
64 self.Bind(wx.EVT_MENU, self.OnClose,id=wx.ID_EXIT)
62 self.Bind(wx.EVT_MENU, self.OnClose,id=wx.ID_EXIT)
65 self.Bind(wx.EVT_MENU, self.OnShowIPythonPanel,id=wx.ID_HIGHEST+1)
63 self.Bind(wx.EVT_MENU, self.OnShowIPythonPanel,id=wx.ID_HIGHEST+1)
66 self.Bind(wx.EVT_MENU, self.OnShowHistoryPanel,id=wx.ID_HIGHEST+2)
64 self.Bind(wx.EVT_MENU, self.OnShowHistoryPanel,id=wx.ID_HIGHEST+2)
67 self.Bind(wx.EVT_MENU, self.OnShowAbout, id=wx.ID_HIGHEST+3)
65 self.Bind(wx.EVT_MENU, self.OnShowAbout, id=wx.ID_HIGHEST+3)
68 self.Bind(wx.EVT_MENU, self.OnShowAllPanel,id=wx.ID_HIGHEST+6)
66 self.Bind(wx.EVT_MENU, self.OnShowAllPanel,id=wx.ID_HIGHEST+6)
69
67
70 warn_text = 'Hello from IPython and wxPython.\n'
68 warn_text = 'Hello from IPython and wxPython.\n'
71 warn_text +='Please Note that this work is still EXPERIMENTAL\n'
69 warn_text +='Please Note that this work is still EXPERIMENTAL\n'
72 warn_text +='It does NOT emulate currently all the IPython functions.\n'
70 warn_text +='It does NOT emulate currently all the IPython functions.\n'
73
71
74 dlg = wx.MessageDialog(self,
72 dlg = wx.MessageDialog(self,
75 warn_text,
73 warn_text,
76 'Warning Box',
74 'Warning Box',
77 wx.OK | wx.ICON_INFORMATION
75 wx.OK | wx.ICON_INFORMATION
78 )
76 )
79 dlg.ShowModal()
77 dlg.ShowModal()
80 dlg.Destroy()
78 dlg.Destroy()
81
79
82 def createMenu(self):
80 def createMenu(self):
83 """local method used to create one menu bar"""
81 """local method used to create one menu bar"""
84
82
85 mb = wx.MenuBar()
83 mb = wx.MenuBar()
86
84
87 file_menu = wx.Menu()
85 file_menu = wx.Menu()
88 file_menu.Append(wx.ID_EXIT, "Exit")
86 file_menu.Append(wx.ID_EXIT, "Exit")
89
87
90 view_menu = wx.Menu()
88 view_menu = wx.Menu()
91 view_menu.Append(wx.ID_HIGHEST+1, "Show IPython Panel")
89 view_menu.Append(wx.ID_HIGHEST+1, "Show IPython Panel")
92 view_menu.Append(wx.ID_HIGHEST+2, "Show History Panel")
90 view_menu.Append(wx.ID_HIGHEST+2, "Show History Panel")
93 view_menu.AppendSeparator()
91 view_menu.AppendSeparator()
94 view_menu.Append(wx.ID_HIGHEST+6, "Show All")
92 view_menu.Append(wx.ID_HIGHEST+6, "Show All")
95
93
96 about_menu = wx.Menu()
94 about_menu = wx.Menu()
97 about_menu.Append(wx.ID_HIGHEST+3, "About")
95 about_menu.Append(wx.ID_HIGHEST+3, "About")
98
96
99 #view_menu.AppendSeparator()
97 #view_menu.AppendSeparator()
100 #options_menu = wx.Menu()
98 #options_menu = wx.Menu()
101 #options_menu.AppendCheckItem(wx.ID_HIGHEST+7, "Allow Floating")
99 #options_menu.AppendCheckItem(wx.ID_HIGHEST+7, "Allow Floating")
102 #options_menu.AppendCheckItem(wx.ID_HIGHEST+8, "Transparent Hint")
100 #options_menu.AppendCheckItem(wx.ID_HIGHEST+8, "Transparent Hint")
103 #options_menu.AppendCheckItem(wx.ID_HIGHEST+9, "Transparent Hint Fade-in")
101 #options_menu.AppendCheckItem(wx.ID_HIGHEST+9, "Transparent Hint Fade-in")
104
102
105
103
106 mb.Append(file_menu, "File")
104 mb.Append(file_menu, "File")
107 mb.Append(view_menu, "View")
105 mb.Append(view_menu, "View")
108 mb.Append(about_menu, "About")
106 mb.Append(about_menu, "About")
109 #mb.Append(options_menu, "Options")
107 #mb.Append(options_menu, "Options")
110
108
111 self.SetMenuBar(mb)
109 self.SetMenuBar(mb)
112
110
113 def createStatus(self):
111 def createStatus(self):
114 statusbar = self.CreateStatusBar(2, wx.ST_SIZEGRIP)
112 statusbar = self.CreateStatusBar(2, wx.ST_SIZEGRIP)
115 statusbar.SetStatusWidths([-2, -3])
113 statusbar.SetStatusWidths([-2, -3])
116 statusbar.SetStatusText("Ready", 0)
114 statusbar.SetStatusText("Ready", 0)
117 statusbar.SetStatusText("WxIPython "+str(__version__), 1)
115 statusbar.SetStatusText("WxIPython "+str(__version__), 1)
118 return statusbar
116 return statusbar
119
117
120 def updateStatus(self,text):
118 def updateStatus(self,text):
121 states = {'IDLE':'Idle',
119 states = {'IDLE':'Idle',
122 'DO_EXECUTE_LINE':'Send command',
120 'DO_EXECUTE_LINE':'Send command',
123 'WAIT_END_OF_EXECUTION':'Running command',
121 'WAIT_END_OF_EXECUTION':'Running command',
124 'SHOW_DOC':'Showing doc',
122 'SHOW_DOC':'Showing doc',
125 'SHOW_PROMPT':'Showing prompt'}
123 'SHOW_PROMPT':'Showing prompt'}
126 self.statusbar.SetStatusText(states[text], 0)
124 self.statusbar.SetStatusText(states[text], 0)
127
125
128 def OnClose(self, event):
126 def OnClose(self, event):
129 """#event used to close program """
127 """#event used to close program """
130 # deinitialize the frame manager
128 # deinitialize the frame manager
131 self._mgr.UnInit()
129 self._mgr.UnInit()
132 self.Destroy()
130 self.Destroy()
133 event.Skip()
131 event.Skip()
134
132
135 def OnExitDlg(self, event):
133 def OnExitDlg(self, event):
136 dlg = wx.MessageDialog(self, 'Are you sure you want to quit WxIPython',
134 dlg = wx.MessageDialog(self, 'Are you sure you want to quit WxIPython',
137 'WxIPython exit',
135 'WxIPython exit',
138 wx.ICON_QUESTION |
136 wx.ICON_QUESTION |
139 wx.YES_NO | wx.NO_DEFAULT
137 wx.YES_NO | wx.NO_DEFAULT
140 )
138 )
141 if dlg.ShowModal() == wx.ID_YES:
139 if dlg.ShowModal() == wx.ID_YES:
142 dlg.Destroy()
140 dlg.Destroy()
143 self._mgr.UnInit()
141 self._mgr.UnInit()
144 self.Destroy()
142 self.Destroy()
145 dlg.Destroy()
143 dlg.Destroy()
146
144
147 #event to display IPython pannel
145 #event to display IPython pannel
148 def OnShowIPythonPanel(self,event):
146 def OnShowIPythonPanel(self,event):
149 """ #event to display Boxpannel """
147 """ #event to display Boxpannel """
150 self._mgr.GetPane(self.ipython_panel).Show(True)
148 self._mgr.GetPane(self.ipython_panel).Show(True)
151 self._mgr.Update()
149 self._mgr.Update()
152 #event to display History pannel
150 #event to display History pannel
153 def OnShowHistoryPanel(self,event):
151 def OnShowHistoryPanel(self,event):
154 self._mgr.GetPane(self.history_panel).Show(True)
152 self._mgr.GetPane(self.history_panel).Show(True)
155 self._mgr.Update()
153 self._mgr.Update()
156
154
157 def OnShowAllPanel(self,event):
155 def OnShowAllPanel(self,event):
158 """#event to display all Pannels"""
156 """#event to display all Pannels"""
159 self._mgr.GetPane(self.ipython_panel).Show(True)
157 self._mgr.GetPane(self.ipython_panel).Show(True)
160 self._mgr.GetPane(self.history_panel).Show(True)
158 self._mgr.GetPane(self.history_panel).Show(True)
161 self._mgr.Update()
159 self._mgr.Update()
162
160
163 def OnShowAbout(self, event):
161 def OnShowAbout(self, event):
164 # First we create and fill the info object
162 # First we create and fill the info object
165 info = wx.AboutDialogInfo()
163 info = wx.AboutDialogInfo()
166 info.Name = "WxIPython"
164 info.Name = "WxIPython"
167 info.Version = str(__version__)
165 info.Version = str(__version__)
168 info.Copyright = "(C) 2007 Laurent Dufrechou"
166 info.Copyright = "(C) 2007 Laurent Dufrechou"
169 info.Description = wordwrap(
167 info.Description = wordwrap(
170 "A Gui that embbed a multithreaded IPython Shell",
168 "A Gui that embbed a multithreaded IPython Shell",
171 350, wx.ClientDC(self))
169 350, wx.ClientDC(self))
172 info.WebSite = ("http://ipython.scipy.org/", "IPython home page")
170 info.WebSite = ("http://ipython.scipy.org/", "IPython home page")
173 info.Developers = [ "Laurent Dufrechou" ]
171 info.Developers = [ "Laurent Dufrechou" ]
174 licenseText="BSD License.\nAll rights reserved. This program and the accompanying materials are made available under the terms of the BSD which accompanies this distribution, and is available at http://www.opensource.org/licenses/bsd-license.php"
172 licenseText="BSD License.\nAll rights reserved. This program and the accompanying materials are made available under the terms of the BSD which accompanies this distribution, and is available at http://www.opensource.org/licenses/bsd-license.php"
175 info.License = wordwrap(licenseText, 500, wx.ClientDC(self))
173 info.License = wordwrap(licenseText, 500, wx.ClientDC(self))
176
174
177 # Then we call wx.AboutBox giving it that info object
175 # Then we call wx.AboutBox giving it that info object
178 wx.AboutBox(info)
176 wx.AboutBox(info)
179
177
180 #-----------------------------------------
178 #-----------------------------------------
181 #Creating our application
179 #Creating our application
182 #-----------------------------------------
180 #-----------------------------------------
183 class MyApp(wx.PySimpleApp):
181 class MyApp(wx.PySimpleApp):
184 """Creating our application"""
182 """Creating our application"""
185 def __init__(self):
183 def __init__(self):
186 wx.PySimpleApp.__init__(self)
184 wx.PySimpleApp.__init__(self)
187
185
188 self.frame = MyFrame()
186 self.frame = MyFrame()
189 self.frame.Show()
187 self.frame.Show()
190
188
191 #-----------------------------------------
189 #-----------------------------------------
192 #Main loop
190 #Main loop
193 #-----------------------------------------
191 #-----------------------------------------
194 def main():
192 def main():
195 app = MyApp()
193 app = MyApp()
196 app.SetTopWindow(app.frame)
194 app.SetTopWindow(app.frame)
197 app.MainLoop()
195 app.MainLoop()
198
196
199 #if launched as main program run this
197 #if launched as main program run this
200 if __name__ == '__main__':
198 if __name__ == '__main__':
201 main()
199 main()
General Comments 0
You need to be logged in to leave comments. Login now