##// END OF EJS Templates
Make process execution work under windows.
Make process execution work under windows.

File last commit:

r1449:fbdb22ce
r1449:fbdb22ce
Show More
wx_frontend.py
398 lines | 14.3 KiB | text/x-python | PythonLexer
Gael Varoquaux
Update wx frontend.
r1349 # encoding: utf-8 -*- test-case-name:
# FIXME: Need to add tests.
# ipython1.frontend.cocoa.tests.test_cocoa_frontend -*-
"""Classes to provide a Wx frontend to the
Gael Varoquaux
Traceback capture now working.
r1360 IPython.kernel.core.interpreter.
Gael Varoquaux
Update wx frontend.
r1349
"""
__docformat__ = "restructuredtext en"
#-------------------------------------------------------------------------------
# Copyright (C) 2008 The IPython Development Team
#
# Distributed under the terms of the BSD License. The full license is in
# the file COPYING, distributed as part of this software.
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
# Imports
#-------------------------------------------------------------------------------
import wx
Gael Varoquaux
Correct small history bug. Add busy cursor.
r1371 import re
Gael Varoquaux
Nice background color for already entered code....
r1374 from wx import stc
Gael Varoquaux
Update wx frontend.
r1349 from console_widget import ConsoleWidget
Gael Varoquaux
Calltips are now working.
r1375 import __builtin__
Gael Varoquaux
Deal with raw_input.
r1388 from time import sleep
Gael Varoquaux
First cut of subprocess execution with redirection of stdin/stdout.
r1437 import sys
gvaroquaux
Bind Ctrl-C to kill process, when in process execution.
r1447 import signal
Gael Varoquaux
Update wx frontend.
r1349
Gael Varoquaux
First cut of subprocess execution with redirection of stdin/stdout.
r1437 from threading import Lock
from IPython.frontend.piped_process import PipedProcess
Gael Varoquaux
Traceback capture now working.
r1360 from IPython.frontend.prefilterfrontend import PrefilterFrontEnd
Gael Varoquaux
Update wx frontend.
r1349
Gael Varoquaux
Nice background color for already entered code....
r1374 #_COMMAND_BG = '#FAFAF1' # Nice green
Gael Varoquaux
Calltips are now working.
r1375 _RUNNING_BUFFER_BG = '#FDFFD3' # Nice yellow
Gael Varoquaux
Tracebacks in red.
r1384 _ERROR_BG = '#FFF1F1' # Nice red
Gael Varoquaux
Nice background color for already entered code....
r1374
_RUNNING_BUFFER_MARKER = 31
Gael Varoquaux
Tracebacks in red.
r1384 _ERROR_MARKER = 30
Gael Varoquaux
Nice background color for already entered code....
r1374
Gael Varoquaux
Update wx frontend.
r1349 #-------------------------------------------------------------------------------
# Classes to implement the Wx frontend
#-------------------------------------------------------------------------------
Gael Varoquaux
Minor bug.
r1389 class WxController(PrefilterFrontEnd, ConsoleWidget):
Gael Varoquaux
Update wx frontend.
r1349
output_prompt = \
Gael Varoquaux
Synchronous stdout/stderr output.
r1383 '\x01\x1b[0;31m\x02Out[\x01\x1b[1;31m\x02%i\x01\x1b[0;31m\x02]: \x01\x1b[0m\x02'
gvaroquaux
Make the wx frontend well-behaved under windows.
r1436
gvaroquaux
Bind Ctrl-C to kill process, when in process execution.
r1447 # Print debug info on what is happening to the console.
gvaroquaux
Make the wx frontend well-behaved under windows.
r1436 debug = True
Gael Varoquaux
First cut of subprocess execution with redirection of stdin/stdout.
r1437
gvaroquaux
Bind Ctrl-C to kill process, when in process execution.
r1447 # The title of the terminal, as captured through the ANSI escape
# sequences.
def _set_title(self, title):
return self.Parent.SetTitle(title)
def _get_title(self):
return self.Parent.GetTitle()
title = property(_get_title, _set_title)
#--------------------------------------------------------------------------
# Private Attributes
#--------------------------------------------------------------------------
# A flag governing the behavior of the input. Can be:
#
# 'readline' for readline-like behavior with a prompt
# and an edit buffer.
# 'subprocess' for sending the raw input directly to a
# subprocess.
# 'buffering' for buffering of the input, that will be used
# when the input state switches back to another state.
_input_state = 'readline'
Gael Varoquaux
First cut of subprocess execution with redirection of stdin/stdout.
r1437 # Attribute to store reference to the pipes of a subprocess, if we
# are running any.
gvaroquaux
Bind Ctrl-C to kill process, when in process execution.
r1447 _running_process = False
Gael Varoquaux
First cut of subprocess execution with redirection of stdin/stdout.
r1437
# A queue for writing fast streams to the screen without flooding the
# event loop
gvaroquaux
Bind Ctrl-C to kill process, when in process execution.
r1447 _out_buffer = []
Gael Varoquaux
First cut of subprocess execution with redirection of stdin/stdout.
r1437
gvaroquaux
Bind Ctrl-C to kill process, when in process execution.
r1447 # A lock to lock the _out_buffer to make sure we don't empty it
Gael Varoquaux
First cut of subprocess execution with redirection of stdin/stdout.
r1437 # while it is being swapped
gvaroquaux
Bind Ctrl-C to kill process, when in process execution.
r1447 _out_buffer_lock = Lock()
Gael Varoquaux
First cut of subprocess execution with redirection of stdin/stdout.
r1437
Gael Varoquaux
Update wx frontend.
r1349 #--------------------------------------------------------------------------
# Public API
#--------------------------------------------------------------------------
def __init__(self, parent, id=wx.ID_ANY, pos=wx.DefaultPosition,
size=wx.DefaultSize, style=wx.CLIP_CHILDREN,
*args, **kwds):
""" Create Shell instance.
"""
ConsoleWidget.__init__(self, parent, id, pos, size, style)
Gael Varoquaux
Traceback capture now working.
r1360 PrefilterFrontEnd.__init__(self)
Gael Varoquaux
Update wx frontend.
r1349
Gael Varoquaux
Nice background color for already entered code....
r1374 # Marker for running buffer.
self.MarkerDefine(_RUNNING_BUFFER_MARKER, stc.STC_MARK_BACKGROUND,
background=_RUNNING_BUFFER_BG)
Gael Varoquaux
Tracebacks in red.
r1384 # Marker for tracebacks.
self.MarkerDefine(_ERROR_MARKER, stc.STC_MARK_BACKGROUND,
background=_ERROR_BG)
Gael Varoquaux
Nice background color for already entered code....
r1374
Gael Varoquaux
First cut of subprocess execution with redirection of stdin/stdout.
r1437 # A time for flushing the write buffer
BUFFER_FLUSH_TIMER_ID = 100
self._buffer_flush_timer = wx.Timer(self, BUFFER_FLUSH_TIMER_ID)
wx.EVT_TIMER(self, BUFFER_FLUSH_TIMER_ID, self._buffer_flush)
Gael Varoquaux
Nice background color for already entered code....
r1374
Gael Varoquaux
Usability tweaks. Better auto tab completion. Terminal size more...
r1380 def do_completion(self):
Gael Varoquaux
Improve tab-completion.
r1373 """ Do code completion.
"""
Gael Varoquaux
First cut of subprocess execution with redirection of stdin/stdout.
r1437 if self.debug:
print >>sys.__stdout__, "do_completion",
Gael Varoquaux
Correct small history bug. Add busy cursor.
r1371 line = self.get_current_edit_buffer()
Gael Varoquaux
Proper tab completion that actually completes.
r1379 new_line, completions = self.complete(line)
if len(completions)>1:
Gael Varoquaux
Usability tweaks. Better auto tab completion. Terminal size more...
r1380 self.write_completion(completions)
Gael Varoquaux
Proper tab completion that actually completes.
r1379 self.replace_current_edit_buffer(new_line)
Gael Varoquaux
First cut of subprocess execution with redirection of stdin/stdout.
r1437 if self.debug:
print >>sys.__stdout__, completions
Gael Varoquaux
Improve tab-completion.
r1373
Gael Varoquaux
Calltips are now working.
r1375 def do_calltip(self):
Gael Varoquaux
First cut of subprocess execution with redirection of stdin/stdout.
r1437 if self.debug:
print >>sys.__stdout__, "do_calltip"
Gael Varoquaux
More calltip fixing.
r1382 separators = re.compile('[\s\{\}\[\]\(\)\= ,:]')
Gael Varoquaux
Calltips are now working.
r1375 symbol = self.get_current_edit_buffer()
Gael Varoquaux
More calltip fixing.
r1382 symbol_string = separators.split(symbol)[-1]
Gael Varoquaux
Calltips are now working.
r1375 base_symbol_string = symbol_string.split('.')[0]
if base_symbol_string in self.shell.user_ns:
symbol = self.shell.user_ns[base_symbol_string]
elif base_symbol_string in self.shell.user_global_ns:
symbol = self.shell.user_global_ns[base_symbol_string]
elif base_symbol_string in __builtin__.__dict__:
symbol = __builtin__.__dict__[base_symbol_string]
else:
return False
Gael Varoquaux
Tweak calltip code.
r1381 for name in symbol_string.split('.')[1:] + ['__doc__']:
Gael Varoquaux
Calltips are now working.
r1375 symbol = getattr(symbol, name)
Gael Varoquaux
Usability tweaks. Better auto tab completion. Terminal size more...
r1380 try:
Gael Varoquaux
Tweak calltip code.
r1381 self.AutoCompCancel()
wx.Yield()
Gael Varoquaux
Usability tweaks. Better auto tab completion. Terminal size more...
r1380 self.CallTipShow(self.GetCurrentPos(), symbol)
Gael Varoquaux
More calltip fixing.
r1382 except:
Gael Varoquaux
Usability tweaks. Better auto tab completion. Terminal size more...
r1380 # The retrieve symbol couldn't be converted to a string
pass
Gael Varoquaux
Calltips are now working.
r1375
Gael Varoquaux
Proper tab completion that actually completes.
r1379
Gael Varoquaux
Usability tweaks. Better auto tab completion. Terminal size more...
r1380 def popup_completion(self, create=False):
""" Updates the popup completion menu if it exists. If create is
true, open the menu.
"""
Gael Varoquaux
First cut of subprocess execution with redirection of stdin/stdout.
r1437 if self.debug:
print >>sys.__stdout__, "popup_completion",
Gael Varoquaux
Improve tab-completion.
r1373 line = self.get_current_edit_buffer()
Gael Varoquaux
Usability tweaks. Better auto tab completion. Terminal size more...
r1380 if (self.AutoCompActive() and not line[-1] == '.') \
or create==True:
suggestion, completions = self.complete(line)
offset=0
if completions:
Gael Varoquaux
More calltip fixing.
r1382 complete_sep = re.compile('[\s\{\}\[\]\(\)\= ,:]')
Gael Varoquaux
Usability tweaks. Better auto tab completion. Terminal size more...
r1380 residual = complete_sep.split(line)[-1]
offset = len(residual)
Gael Varoquaux
Tweak calltip code.
r1381 self.pop_completion(completions, offset=offset)
Gael Varoquaux
First cut of subprocess execution with redirection of stdin/stdout.
r1437 if self.debug:
print >>sys.__stdout__, completions
Gael Varoquaux
Correct small history bug. Add busy cursor.
r1371
gvaroquaux
Bind Ctrl-C to kill process, when in process execution.
r1447 def new_prompt(self, prompt):
self._input_state = 'readline'
ConsoleWidget.new_prompt(self, prompt)
Gael Varoquaux
Deal with raw_input.
r1388 def raw_input(self, prompt):
""" A replacement from python's raw_input.
"""
self.new_prompt(prompt)
self.waiting = True
self.__old_on_enter = self._on_enter
def my_on_enter():
self.waiting = False
self._on_enter = my_on_enter
Gael Varoquaux
Minor bug.
r1389 # XXX: Busy waiting, ugly.
Gael Varoquaux
Deal with raw_input.
r1388 while self.waiting:
wx.Yield()
sleep(0.1)
self._on_enter = self.__old_on_enter
gvaroquaux
Bind Ctrl-C to kill process, when in process execution.
r1447 self._input_state = 'buffering'
Gael Varoquaux
Deal with raw_input.
r1388 return self.get_current_edit_buffer().rstrip('\n')
Gael Varoquaux
Nice background color for already entered code....
r1374 def execute(self, python_string, raw_string=None):
gvaroquaux
Bind Ctrl-C to kill process, when in process execution.
r1447 self._input_state = 'buffering'
Gael Varoquaux
Usability tweaks. Better auto tab completion. Terminal size more...
r1380 self.CallTipCancel()
Gael Varoquaux
Correct small history bug. Add busy cursor.
r1371 self._cursor = wx.BusyCursor()
Gael Varoquaux
Nice background color for already entered code....
r1374 if raw_string is None:
raw_string = python_string
end_line = self.current_prompt_line \
+ max(1, len(raw_string.split('\n'))-1)
for i in range(self.current_prompt_line, end_line):
Gael Varoquaux
Tracebacks in red.
r1384 self.MarkerAdd(i, _RUNNING_BUFFER_MARKER)
Gael Varoquaux
Tweak calltip code.
r1381 # Update the display:
wx.Yield()
Gael Varoquaux
Usability tweaks. Better auto tab completion. Terminal size more...
r1380 self.GotoPos(self.GetLength())
Gael Varoquaux
Add demo app. Add callback for exit to the ipython0 code.
r1391 PrefilterFrontEnd.execute(self, python_string, raw_string=raw_string)
def capture_output(self):
Gael Varoquaux
Deal with raw_input.
r1388 self.__old_raw_input = __builtin__.raw_input
__builtin__.raw_input = self.raw_input
Gael Varoquaux
Add demo app. Add callback for exit to the ipython0 code.
r1391 PrefilterFrontEnd.capture_output(self)
def release_output(self):
Gael Varoquaux
Deal with raw_input.
r1388 __builtin__.raw_input = self.__old_raw_input
Gael Varoquaux
Add demo app. Add callback for exit to the ipython0 code.
r1391 PrefilterFrontEnd.capture_output(self)
Gael Varoquaux
Nice background color for already entered code....
r1374
Gael Varoquaux
Correct small history bug. Add busy cursor.
r1371
def after_execute(self):
PrefilterFrontEnd.after_execute(self)
Gael Varoquaux
Improve tab-completion.
r1373 if hasattr(self, '_cursor'):
del self._cursor
Gael Varoquaux
Correct small history bug. Add busy cursor.
r1371
Gael Varoquaux
First cut of subprocess execution with redirection of stdin/stdout.
r1437
def system_call(self, command_string):
gvaroquaux
Bind Ctrl-C to kill process, when in process execution.
r1447 self._input_state = 'subprocess'
self._running_process = PipedProcess(command_string,
Gael Varoquaux
First cut of subprocess execution with redirection of stdin/stdout.
r1437 out_callback=self.buffered_write,
end_callback = self._end_system_call)
gvaroquaux
Bind Ctrl-C to kill process, when in process execution.
r1447 self._running_process.start()
Gael Varoquaux
First cut of subprocess execution with redirection of stdin/stdout.
r1437 # XXX: another one of these polling loops to have a blocking
# call
gvaroquaux
Flush the print buffer at the end of a program execution.
r1440 wx.Yield()
gvaroquaux
Bind Ctrl-C to kill process, when in process execution.
r1447 while self._running_process:
Gael Varoquaux
First cut of subprocess execution with redirection of stdin/stdout.
r1437 wx.Yield()
sleep(0.1)
gvaroquaux
Flush the print buffer at the end of a program execution.
r1440 # Be sure to flush the buffer.
self._buffer_flush(event=None)
Gael Varoquaux
First cut of subprocess execution with redirection of stdin/stdout.
r1437
def buffered_write(self, text):
""" A write method for streams, that caches the stream in order
to avoid flooding the event loop.
This can be called outside of the main loop, in separate
threads.
"""
gvaroquaux
Bind Ctrl-C to kill process, when in process execution.
r1447 self._out_buffer_lock.acquire()
self._out_buffer.append(text)
self._out_buffer_lock.release()
Gael Varoquaux
First cut of subprocess execution with redirection of stdin/stdout.
r1437 if not self._buffer_flush_timer.IsRunning():
gvaroquaux
Make process execution work under windows.
r1449 wx.CallAfter(self._buffer_flush_timer.Start, 100) # milliseconds
Gael Varoquaux
First cut of subprocess execution with redirection of stdin/stdout.
r1437
Gael Varoquaux
Tracebacks in red.
r1384 def show_traceback(self):
start_line = self.GetCurrentLine()
PrefilterFrontEnd.show_traceback(self)
wx.Yield()
for i in range(start_line, self.GetCurrentLine()):
self.MarkerAdd(i, _ERROR_MARKER)
Gael Varoquaux
Update wx frontend.
r1349 #--------------------------------------------------------------------------
# Private API
#--------------------------------------------------------------------------
def _on_key_down(self, event, skip=True):
""" Capture the character events, let the parent
widget handle them, and put our logic afterward.
"""
gvaroquaux
Bind Ctrl-C to kill process, when in process execution.
r1447 print >>sys.__stderr__, event.KeyCode
Gael Varoquaux
Update wx frontend.
r1349 current_line_number = self.GetCurrentLine()
gvaroquaux
Bind Ctrl-C to kill process, when in process execution.
r1447 if event.KeyCode in (ord('c'), ord('C')) and event.ControlDown():
# Capture Control-C
if self._input_state == 'subprocess':
if self.debug:
print >>sys.__stderr__, 'Killing running process'
self._running_process.process.kill()
elif self._input_state == 'buffering':
if self.debug:
print >>sys.__stderr__, 'Raising KeyboardException'
raise KeyboardException
# XXX: We need to make really sure we
# get back to a prompt.
elif self._input_state == 'subprocess' and event.KeyCode<256 \
Gael Varoquaux
First cut of subprocess execution with redirection of stdin/stdout.
r1437 and event.Modifiers in (wx.MOD_NONE, wx.MOD_WIN):
gvaroquaux
Bind Ctrl-C to kill process, when in process execution.
r1447 # We are running a process, we redirect keys.
Gael Varoquaux
First cut of subprocess execution with redirection of stdin/stdout.
r1437 ConsoleWidget._on_key_down(self, event, skip=skip)
if self.debug:
print >>sys.__stderr__, chr(event.KeyCode)
gvaroquaux
Bind Ctrl-C to kill process, when in process execution.
r1447 self._running_process.process.stdin.write(chr(event.KeyCode))
gvaroquaux
Make process execution work under windows.
r1449 self._running_process.process.stdin.flush()
Gael Varoquaux
First cut of subprocess execution with redirection of stdin/stdout.
r1437 elif event.KeyCode in (ord('('), 57):
# Calltips
Gael Varoquaux
Tweak calltip code.
r1381 event.Skip()
self.do_calltip()
elif self.AutoCompActive():
Gael Varoquaux
Correct small history bug. Add busy cursor.
r1371 event.Skip()
Gael Varoquaux
Improve tab-completion.
r1373 if event.KeyCode in (wx.WXK_BACK, wx.WXK_DELETE):
Gael Varoquaux
Tweak calltip code.
r1381 wx.CallAfter(self.popup_completion, create=True)
Gael Varoquaux
Improve tab-completion.
r1373 elif not event.KeyCode in (wx.WXK_UP, wx.WXK_DOWN, wx.WXK_LEFT,
wx.WXK_RIGHT):
Gael Varoquaux
Usability tweaks. Better auto tab completion. Terminal size more...
r1380 wx.CallAfter(self.popup_completion)
Gael Varoquaux
Update wx frontend.
r1349 else:
Gael Varoquaux
Correct small history bug. Add busy cursor.
r1371 # Up history
if event.KeyCode == wx.WXK_UP and (
( current_line_number == self.current_prompt_line and
event.Modifiers in (wx.MOD_NONE, wx.MOD_WIN) )
or event.ControlDown() ):
new_buffer = self.get_history_previous(
self.get_current_edit_buffer())
if new_buffer is not None:
self.replace_current_edit_buffer(new_buffer)
if self.GetCurrentLine() > self.current_prompt_line:
# Go to first line, for seemless history up.
self.GotoPos(self.current_prompt_pos)
# Down history
elif event.KeyCode == wx.WXK_DOWN and (
( current_line_number == self.LineCount -1 and
event.Modifiers in (wx.MOD_NONE, wx.MOD_WIN) )
or event.ControlDown() ):
new_buffer = self.get_history_next()
if new_buffer is not None:
self.replace_current_edit_buffer(new_buffer)
Gael Varoquaux
Make "help()" work.
r1390 # Tab-completion
Gael Varoquaux
Correct small history bug. Add busy cursor.
r1371 elif event.KeyCode == ord('\t'):
last_line = self.get_current_edit_buffer().split('\n')[-1]
if not re.match(r'^\s*$', last_line):
Gael Varoquaux
Usability tweaks. Better auto tab completion. Terminal size more...
r1380 self.do_completion()
Gael Varoquaux
Correct small history bug. Add busy cursor.
r1371 else:
event.Skip()
else:
ConsoleWidget._on_key_down(self, event, skip=skip)
Gael Varoquaux
Update wx frontend.
r1349
Gael Varoquaux
Improve tab-completion.
r1373 def _on_key_up(self, event, skip=True):
Gael Varoquaux
First cut of subprocess execution with redirection of stdin/stdout.
r1437 if event.KeyCode in (59, ord('.')):
Gael Varoquaux
Improve tab-completion.
r1373 # Intercepting '.'
event.Skip()
Gael Varoquaux
Usability tweaks. Better auto tab completion. Terminal size more...
r1380 self.popup_completion(create=True)
Gael Varoquaux
Improve tab-completion.
r1373 else:
ConsoleWidget._on_key_up(self, event, skip=skip)
gvaroquaux
Bind Ctrl-C to kill process, when in process execution.
r1447
gvaroquaux
Make the wx frontend well-behaved under windows.
r1436 def _on_enter(self):
if self.debug:
print >>sys.__stdout__, repr(self.get_current_edit_buffer())
PrefilterFrontEnd._on_enter(self)
Gael Varoquaux
Update wx frontend.
r1349
Gael Varoquaux
Clean up the title-setting code.
r1393
gvaroquaux
Bind Ctrl-C to kill process, when in process execution.
r1447 def _end_system_call(self):
""" Called at the end of a system call.
"""
print >>sys.__stderr__, 'End of system call'
self._input_state = 'buffering'
self._running_process = False
Gael Varoquaux
Clean up the title-setting code.
r1393
gvaroquaux
Bind Ctrl-C to kill process, when in process execution.
r1447
def _buffer_flush(self, event):
""" Called by the timer to flush the write buffer.
This is always called in the mainloop, by the wx timer.
"""
self._out_buffer_lock.acquire()
_out_buffer = self._out_buffer
self._out_buffer = []
self._out_buffer_lock.release()
gvaroquaux
Make process execution work under windows.
r1449 self.write(''.join(_out_buffer), refresh=False)
gvaroquaux
Bind Ctrl-C to kill process, when in process execution.
r1447 self._buffer_flush_timer.Stop()
Gael Varoquaux
Clean up the title-setting code.
r1393
Gael Varoquaux
Update wx frontend.
r1349 if __name__ == '__main__':
class MainWindow(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title, size=(300,250))
self._sizer = wx.BoxSizer(wx.VERTICAL)
Gael Varoquaux
Minor bug.
r1389 self.shell = WxController(self)
Gael Varoquaux
Update wx frontend.
r1349 self._sizer.Add(self.shell, 1, wx.EXPAND)
self.SetSizer(self._sizer)
self.SetAutoLayout(1)
self.Show(True)
app = wx.PySimpleApp()
frame = MainWindow(None, wx.ID_ANY, 'Ipython')
frame.shell.SetFocus()
Gael Varoquaux
Usability tweaks. Better auto tab completion. Terminal size more...
r1380 frame.SetSize((680, 460))
Gael Varoquaux
Update wx frontend.
r1349 self = frame.shell
app.MainLoop()