2 """wxPython interactive shell
4 Copyright (c) 1999 SIA "ANK"
6 this module is free software. it may be used under same terms as Python itself
9 i would like to use command completion (see rlcompleter library module),
10 but i cannot load it because i don't have readline...
13 03-oct-1999 [als] created
14 04-oct-1999 [als] PyShellOutput.intro moved from __init__ parameters
15 to class attributes; html debug disabled
16 04-oct-1999 [als] fixed bug with class attributes
17 input prompts and output styles added to customized demo
19 04-oct-1999 [rpd] Changed to use the new sizers
20 05-oct-1990 [als] changes inspired by code.InteractiveInterpreter()
21 from Python Library. if i knew about this class earlier,
22 i would rather inherit from it.
23 renamed to wxPyShell.py since i've renounced the 8.3 scheme
26 __version__
="$Revision$"
29 import sys
, string
, code
, traceback
30 from wxPython
.wx
import *
31 from wxPython
.html
import *
34 class PyShellInput(wxPanel
):
35 """PyShell input window
38 PS1
=" Enter Command:"
40 def __init__(self
, parent
, shell
, id=-1):
41 """Create input window
43 shell must be a PyShell object.
44 it is used for exception handling, eval() namespaces,
45 and shell.output is used for output
46 (print's go to overridden stdout)
48 wxPanel
.__init
__(self
, parent
, id)
50 # make a private copy of class attrs
51 self
.PS1
=PyShellInput
.PS1
52 self
.PS2
=PyShellInput
.PS2
54 self
.label
=wxStaticText(self
, -1, self
.PS1
)
56 self
.entry
=wxTextCtrl(self
, tid
, style
= wxTE_MULTILINE
)
57 EVT_CHAR(self
.entry
, self
.OnChar
)
58 self
.entry
.SetFont(wxFont(9, wxMODERN
, wxNORMAL
, wxNORMAL
, false
))
59 sizer
=wxBoxSizer(wxVERTICAL
)
60 sizer
.AddMany([(self
.label
, 0, wxEXPAND
), (self
.entry
, 1, wxEXPAND
)])
62 self
.SetAutoLayout(true
)
63 EVT_SET_FOCUS(self
, self
.OnSetFocus
)
64 # when in "continuation" mode,
65 # two consecutive newlines are required
66 # to avoid execution of unfinished block
69 def OnSetFocus(self
, event
):
73 def Clear(self
, event
=None):
74 """reset input state"""
75 self
.label
.SetLabel(self
.PS1
)
77 self
.entry
.SetSelection(0, self
.entry
.GetLastPosition())
79 # self.entry.SetFocus()
81 def OnChar(self
, event
):
82 """called on CHARevent. executes input on newline"""
83 # print "On Char:", event.__dict__.keys()
84 if event
.KeyCode() !=WXK_RETURN
:
88 text
=self
.entry
.GetValue()
90 text
=string
.replace(text
, "\r\n", "\n")
91 # see if we've finished
92 if (not (self
.first_line
or text
[-1] =="\n") # in continuation mode
93 or (text
[-1] =="\\") # escaped newline
95 # XXX should escaped newline put myself i "continuation" mode?
98 # ok, we can try to execute this
99 rc
=self
.shell
.TryExec(text
)
101 # code is incomplete; continue input
103 self
.label
.SetLabel(self
.PS2
)
110 class PyShellOutput(wxPanel
):
111 """PyShell output window
113 for now, it is based on simple wxTextCtrl,
114 but i'm looking at HTML classes to provide colorized output
116 # attributes for for different (input, output, exception) display styles:
117 # begin tag, end tag, newline
118 in_style
=(" <font color=\"#000080\"><tt>>>> ",
119 "</tt></font><br>\n", "<br>\n... ")
120 out_style
=("<tt>", "</tt>\n", "<br>\n")
121 exc_style
=("<font color=\"#FF0000\"><tt>",
122 "</tt></font>\n", "<br>\n")
123 intro
="<H3>wxPython Interactive Shell</H3>\n"
126 erefs
=(("&", "&"), (">", ">"), ("<", "<"), (" ", " "))
127 def __init__(self
, parent
, id=-1):
128 wxPanel
.__init
__(self
, parent
, id)
129 # make a private copy of class attrs
130 self
.in_style
=PyShellOutput
.in_style
131 self
.out_style
=PyShellOutput
.out_style
132 self
.exc_style
=PyShellOutput
.exc_style
133 self
.intro
=PyShellOutput
.intro
134 self
.html_debug
=PyShellOutput
.html_debug
137 # this was used in html debugging,
138 # but i don't want to delete it; it's funny
139 splitter
=wxSplitterWindow(self
, -1)
140 self
.view
=wxTextCtrl(splitter
, -1,
141 style
= wxTE_MULTILINE|wxTE_READONLY|wxHSCROLL
)
142 self
.html
=wxHtmlWindow(splitter
)
143 splitter
.SplitVertically(self
.view
, self
.html
)
144 splitter
.SetSashPosition(40)
145 splitter
.SetMinimumPaneSize(3)
146 self
.client
=splitter
149 self
.html
=wxHtmlWindow(self
)
150 self
.client
=self
.html
# used in OnSize()
151 self
.text
=self
.intro
152 self
.html
.SetPage(self
.text
)
153 self
.html
.SetAutoLayout(TRUE
)
155 # refreshes are annoying
160 def OnSize(self
, event
):
161 self
.client
.SetSize(self
.GetClientSize())
163 def OnIdle(self
, event
):
164 """when there's nothing to do, we can update display"""
165 if self
.in_batch
and self
.dirty
: self
.UpdWindow()
167 def BeginBatch(self
):
168 """do not refresh display till EndBatch()"""
172 """end batch; start updating display immediately"""
174 if self
.dirty
: self
.UpdWindow()
177 """sync display with text buffer"""
179 html
.SetPage(self
.text
)
182 (x
,y
) =html
.GetVirtualSize()
185 def AddText(self
, text
, style
=None):
186 """write text to output window"""
187 # a trick needed to defer default from compile-time to execute-time
188 if style
==None: style
=self
.out_style
189 if 0 and __debug__
: sys
.__stdout
__.write(text
)
191 for (symbol
, eref
) in self
.erefs
:
192 text
=string
.replace(text
, symbol
, eref
)
194 text
=string
.replace(text
, "\n", style
[2])
196 self
.text
=self
.text
+style
[0] +text
+style
[1]
197 if not self
.in_batch
: self
.UpdWindow()
200 # html debug output needn't to be too large
201 self
.view
.SetValue(self
.text
[-4096:])
203 def write(self
, str, style
=None):
204 """stdout-like interface"""
205 if style
==None: style
=self
.out_style
206 # do not process incomplete lines
208 # hm... what was i supposed to do?
211 self
.line_buffer
=self
.line_buffer
+str
213 self
.AddText(self
.line_buffer
+str, style
)
216 def flush(self
, style
=None):
217 """write out all that was left in line buffer"""
218 if style
==None: style
=self
.out_style
219 self
.AddText(self
.line_buffer
+"\n", style
)
221 def write_in(self
, str, style
=None):
222 """write text in "input" style"""
223 if style
==None: style
=self
.in_style
224 self
.AddText(str, style
)
226 def write_exc(self
, str, style
=None):
227 """write text in "exception" style"""
228 if style
==None: style
=self
.exc_style
229 self
.AddText(str, style
)
231 class PyShell(wxPanel
):
232 """interactive Python shell with wxPython interface
235 def __init__(self
, parent
, globals=globals(), locals={},
236 id=-1, pos
=wxDefaultPosition
, size
=wxDefaultSize
,
237 style
=wxTAB_TRAVERSAL
, name
="shell"):
238 """create PyShell window"""
239 wxPanel
.__init
__(self
, parent
, id, pos
, size
, style
, name
)
240 self
.globals =globals
242 splitter
=wxSplitterWindow(self
, -1)
243 self
.output
=PyShellOutput(splitter
)
244 self
.input =PyShellInput(splitter
, self
)
245 self
.input.SetFocus()
246 splitter
.SplitHorizontally(self
.input, self
.output
)
247 splitter
.SetSashPosition(100)
248 splitter
.SetMinimumPaneSize(20)
249 self
.splitter
=splitter
250 EVT_SET_FOCUS(self
, self
.OnSetFocus
)
252 def OnSetFocus(self
, event
):
253 self
.input.SetFocus()
255 def TryExec(self
, source
, symbol
="single"):
256 """Compile and run some source in the interpreter.
258 borrowed from code.InteractiveInterpreter().runsource()
259 as i said above, i would rather like to inherit from that class
261 returns 1 if more input is required, or 0, otherwise
264 cc
= code
.compile_command(source
, symbol
=symbol
)
265 except (OverflowError, SyntaxError):
266 # [als] hm... never seen anything of that kind
267 self
.ShowSyntaxError()
270 # source is incomplete
272 # source is sucessfully compiled
274 # redirect system stdout to the output window
277 # begin printout batch (html updates are deferred until EndBatch())
281 exec cc
in self
.globals, self
.locals
283 # SystemExit is not handled and has to be re-raised
286 # all other exceptions produce traceback output
288 # switch back to saved stdout
295 def ShowException(self
):
296 """display the traceback for the latest exception"""
297 (etype
, value
, tb
) =sys
.exc_info()
298 # remove myself from traceback
299 tblist
=traceback
.extract_tb(tb
)[1:]
300 msg
=string
.join(traceback
.format_exception_only(etype
, value
)
301 +traceback
.format_list(tblist
))
302 self
.output
.write_exc(msg
)
304 def ShowSyntaxError(self
):
305 """display message about syntax error (no traceback here)"""
306 (etype
, value
, tb
) =sys
.exc_info()
307 msg
=string
.join(traceback
.format_exception_only(etype
, value
))
308 self
.output
.write_exc(msg
)
310 def OnSize(self
, event
):
311 self
.splitter
.SetSize(self
.GetClientSize())
313 #----------------------------------------------------------------------
314 if __name__
== '__main__':
315 class MyFrame(wxFrame
):
316 """Very standard Frame class. Nothing special here!"""
317 def __init__(self
, parent
=NULL
, id =-1,
318 title
="wxPython Interactive Shell"):
319 wxFrame
.__init
__(self
, parent
, id, title
)
320 self
.shell
=PyShell(self
)
323 """Demonstrates usage of both default and customized shells"""
327 self
.SetTopWindow(frame
)
328 ## PyShellInput.PS1 =" let's get some work done..."
329 ## PyShellInput.PS2 =" ok, what do you really mean?"
330 ## PyShellOutput.in_style =(
331 ## "<I><font color=\"#008000\"><tt>>>> ",
332 ## "</tt></font></I><br>\n", "<br>\n... ")
333 ## PyShellOutput.out_style =(
334 ## "<font color=\"#000080\"><tt>",
335 ## "</tt></font><br>\n", "<br>\n")
336 ## PyShellOutput.exc_style =("<B><font color=\"#FF0000\"><tt>",
337 ## "</tt></font></B>\n", "<br>\n")
338 ## PyShellOutput.intro ="<I><B>Customized wxPython Shell</B>" \
339 ## "<br><-- move this sash to see html debug output</I><br>\n"
340 ## PyShellOutput.html_debug =1
341 ## frame = MyFrame(title="Customized wxPython Shell")