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