PyCrust updates
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@13341 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
@@ -49,12 +49,12 @@ else: # GTK
|
||||
class ShellFacade:
|
||||
"""Simplified interface to all shell-related functionality.
|
||||
|
||||
This is a semi-transparent facade, in that all attributes of other are
|
||||
This is a semi-transparent facade, in that all attributes of other are
|
||||
still accessible, even though only some are visible to the user."""
|
||||
|
||||
|
||||
name = 'PyCrust Shell Interface'
|
||||
revision = __version__
|
||||
|
||||
|
||||
def __init__(self, other):
|
||||
"""Create a ShellFacade instance."""
|
||||
methods = ['ask',
|
||||
@@ -73,7 +73,7 @@ class ShellFacade:
|
||||
d = self.__dict__
|
||||
d['other'] = other
|
||||
d['help'] = 'There is no help available, yet.'
|
||||
|
||||
|
||||
|
||||
def __getattr__(self, name):
|
||||
if hasattr(self.other, name):
|
||||
@@ -104,10 +104,10 @@ class ShellFacade:
|
||||
|
||||
class Shell(wxStyledTextCtrl):
|
||||
"""PyCrust Shell based on wxStyledTextCtrl."""
|
||||
|
||||
|
||||
name = 'PyCrust Shell'
|
||||
revision = __version__
|
||||
|
||||
|
||||
def __init__(self, parent, id=-1, pos=wxDefaultPosition, \
|
||||
size=wxDefaultSize, style=wxCLIP_CHILDREN, introText='', \
|
||||
locals=None, InterpClass=None, *args, **kwds):
|
||||
@@ -125,7 +125,7 @@ class Shell(wxStyledTextCtrl):
|
||||
else:
|
||||
Interpreter = InterpClass
|
||||
# Create default locals so we have something interesting.
|
||||
shellLocals = {'__name__': 'PyCrust-Shell',
|
||||
shellLocals = {'__name__': 'PyCrust-Shell',
|
||||
'__doc__': 'PyCrust-Shell, The PyCrust Python Shell.',
|
||||
'__version__': VERSION,
|
||||
}
|
||||
@@ -173,12 +173,12 @@ class Shell(wxStyledTextCtrl):
|
||||
|
||||
def destroy(self):
|
||||
del self.interp
|
||||
|
||||
|
||||
def config(self):
|
||||
"""Configure shell based on user preferences."""
|
||||
self.SetMarginType(1, wxSTC_MARGIN_NUMBER)
|
||||
self.SetMarginWidth(1, 40)
|
||||
|
||||
|
||||
self.SetLexer(wxSTC_LEX_PYTHON)
|
||||
self.SetKeyWords(0, ' '.join(keyword.kwlist))
|
||||
|
||||
@@ -193,7 +193,7 @@ class Shell(wxStyledTextCtrl):
|
||||
self.autoCompleteIncludeDouble = 1
|
||||
self.autoCompleteCaseInsensitive = 1
|
||||
self.AutoCompSetIgnoreCase(self.autoCompleteCaseInsensitive)
|
||||
# De we want to automatically pop up command argument help?
|
||||
# Do we want to automatically pop up command argument help?
|
||||
self.autoCallTip = 1
|
||||
self.CallTipSetBackground(wxColour(255, 255, 232))
|
||||
|
||||
@@ -206,11 +206,11 @@ class Shell(wxStyledTextCtrl):
|
||||
self.write(self.interp.introText)
|
||||
except AttributeError:
|
||||
pass
|
||||
|
||||
|
||||
def setBuiltinKeywords(self):
|
||||
"""Create pseudo keywords as part of builtins.
|
||||
|
||||
This is a rather clever hack that sets "close", "exit" and "quit"
|
||||
|
||||
This is a rather clever hack that sets "close", "exit" and "quit"
|
||||
to a PseudoKeyword object so that we can make them do what we want.
|
||||
In this case what we want is to call our self.quit() method.
|
||||
The user can type "close", "exit" or "quit" without the final parens.
|
||||
@@ -227,19 +227,19 @@ class Shell(wxStyledTextCtrl):
|
||||
|
||||
def quit(self):
|
||||
"""Quit the application."""
|
||||
|
||||
|
||||
# XXX Good enough for now but later we want to send a close event.
|
||||
|
||||
|
||||
# In the close event handler we can make sure they want to quit.
|
||||
# Other applications, like PythonCard, may choose to hide rather than
|
||||
# quit so we should just post the event and let the surrounding app
|
||||
# decide what it wants to do.
|
||||
self.write('Click on the close button to leave the application.')
|
||||
|
||||
|
||||
def setLocalShell(self):
|
||||
"""Add 'shell' to locals as reference to ShellFacade instance."""
|
||||
self.interp.locals['shell'] = ShellFacade(other=self)
|
||||
|
||||
|
||||
def execStartupScript(self, startupScript):
|
||||
"""Execute the user's PYTHONSTARTUP script if they have one."""
|
||||
if startupScript and os.path.isfile(startupScript):
|
||||
@@ -248,10 +248,10 @@ class Shell(wxStyledTextCtrl):
|
||||
(`startupText`, `startupScript`))
|
||||
else:
|
||||
self.push('')
|
||||
|
||||
|
||||
def setStyles(self, faces):
|
||||
"""Configure font size, typeface and color for lexer."""
|
||||
|
||||
|
||||
# Default style
|
||||
self.StyleSetSpec(wxSTC_STYLE_DEFAULT, "face:%(mono)s,size:%(size)d" % faces)
|
||||
|
||||
@@ -313,37 +313,41 @@ class Shell(wxStyledTextCtrl):
|
||||
|
||||
Prevents modification of previously submitted commands/responses."""
|
||||
key = event.KeyCode()
|
||||
controlDown = event.ControlDown()
|
||||
altDown = event.AltDown()
|
||||
shiftDown = event.ShiftDown()
|
||||
currpos = self.GetCurrentPos()
|
||||
stoppos = self.promptPos[1]
|
||||
# Return is used to submit a command to the interpreter.
|
||||
if key == WXK_RETURN:
|
||||
if self.AutoCompActive(): self.AutoCompCancel()
|
||||
if self.CallTipActive: self.CallTipCancel()
|
||||
if self.CallTipActive(): self.CallTipCancel()
|
||||
self.processLine()
|
||||
# If the auto-complete window is up let it do its thing.
|
||||
elif self.AutoCompActive():
|
||||
event.Skip()
|
||||
# Let Ctrl-Alt-* get handled normally.
|
||||
elif controlDown and altDown:
|
||||
event.Skip()
|
||||
# Cut to the clipboard.
|
||||
elif event.ControlDown() and key in (ord('X'), ord('x')):
|
||||
elif controlDown and key in (ord('X'), ord('x')):
|
||||
self.Cut()
|
||||
# Copy to the clipboard.
|
||||
elif event.ControlDown() and not event.ShiftDown() \
|
||||
and key in (ord('C'), ord('c')):
|
||||
elif controlDown and not shiftDown and key in (ord('C'), ord('c')):
|
||||
self.Copy()
|
||||
# Copy to the clipboard, including prompts.
|
||||
elif event.ControlDown() and event.ShiftDown() \
|
||||
and key in (ord('C'), ord('c')):
|
||||
elif controlDown and shiftDown and key in (ord('C'), ord('c')):
|
||||
self.CopyWithPrompts()
|
||||
# Paste from the clipboard.
|
||||
elif event.ControlDown() and key in (ord('V'), ord('v')):
|
||||
elif controlDown and key in (ord('V'), ord('v')):
|
||||
self.Paste()
|
||||
# Retrieve the previous command from the history buffer.
|
||||
elif (event.ControlDown() and key == WXK_UP) \
|
||||
or (event.AltDown() and key in (ord('P'), ord('p'))):
|
||||
elif (controlDown and key == WXK_UP) \
|
||||
or (altDown and key in (ord('P'), ord('p'))):
|
||||
self.OnHistoryRetrieve(step=+1)
|
||||
# Retrieve the next command from the history buffer.
|
||||
elif (event.ControlDown() and key == WXK_DOWN) \
|
||||
or (event.AltDown() and key in (ord('N'), ord('n'))):
|
||||
elif (controlDown and key == WXK_DOWN) \
|
||||
or (altDown and key in (ord('N'), ord('n'))):
|
||||
self.OnHistoryRetrieve(step=-1)
|
||||
# Search up the history for the text in front of the cursor.
|
||||
elif key == WXK_F8:
|
||||
@@ -374,6 +378,12 @@ class Shell(wxStyledTextCtrl):
|
||||
# Don't toggle between insert mode and overwrite mode.
|
||||
elif key == WXK_INSERT:
|
||||
pass
|
||||
# Don't allow line deletion.
|
||||
elif controlDown and key in (ord('L'), ord('l')):
|
||||
pass
|
||||
# Don't allow line transposition.
|
||||
elif controlDown and key in (ord('T'), ord('t')):
|
||||
pass
|
||||
# Protect the readonly portion of the shell.
|
||||
elif not self.CanEdit():
|
||||
pass
|
||||
@@ -432,17 +442,17 @@ class Shell(wxStyledTextCtrl):
|
||||
|
||||
def setStatusText(self, text):
|
||||
"""Display status information."""
|
||||
|
||||
|
||||
# This method will most likely be replaced by the enclosing app
|
||||
# to do something more interesting, like write to a status bar.
|
||||
print text
|
||||
|
||||
def processLine(self):
|
||||
"""Process the line of text at which the user hit Enter."""
|
||||
|
||||
|
||||
# The user hit ENTER and we need to decide what to do. They could be
|
||||
# sitting on any line in the shell.
|
||||
|
||||
|
||||
thepos = self.GetCurrentPos()
|
||||
endpos = self.GetTextLength()
|
||||
# If they hit RETURN at the very bottom, execute the command.
|
||||
@@ -482,7 +492,7 @@ class Shell(wxStyledTextCtrl):
|
||||
|
||||
def getMultilineCommand(self, rstrip=1):
|
||||
"""Extract a multi-line command from the editor.
|
||||
|
||||
|
||||
The command may not necessarily be valid Python syntax."""
|
||||
# XXX Need to extract real prompts here. Need to keep track of the
|
||||
# prompt every time a command is issued.
|
||||
@@ -516,10 +526,10 @@ class Shell(wxStyledTextCtrl):
|
||||
if rstrip:
|
||||
command = command.rstrip()
|
||||
return command
|
||||
|
||||
|
||||
def getCommand(self, text=None, rstrip=1):
|
||||
"""Extract a command from text which may include a shell prompt.
|
||||
|
||||
|
||||
The command may not necessarily be valid Python syntax."""
|
||||
if not text:
|
||||
text = self.GetCurLine()[0]
|
||||
@@ -543,7 +553,7 @@ class Shell(wxStyledTextCtrl):
|
||||
elif text[:ps2size] == ps2:
|
||||
text = text[ps2size:]
|
||||
return text
|
||||
|
||||
|
||||
def push(self, command):
|
||||
"""Send command to the interpreter for execution."""
|
||||
self.write(os.linesep)
|
||||
@@ -583,7 +593,7 @@ class Shell(wxStyledTextCtrl):
|
||||
|
||||
def prompt(self):
|
||||
"""Display appropriate prompt for the context, either ps1 or ps2.
|
||||
|
||||
|
||||
If this is a continuation line, autoindent as necessary."""
|
||||
if self.more:
|
||||
prompt = str(sys.ps2)
|
||||
@@ -634,25 +644,25 @@ class Shell(wxStyledTextCtrl):
|
||||
def ask(self, prompt='Please enter your response:'):
|
||||
"""Get response from the user."""
|
||||
return raw_input(prompt=prompt)
|
||||
|
||||
|
||||
def pause(self):
|
||||
"""Halt execution pending a response from the user."""
|
||||
self.ask('Press enter to continue:')
|
||||
|
||||
|
||||
def clear(self):
|
||||
"""Delete all text from the shell."""
|
||||
self.ClearAll()
|
||||
|
||||
|
||||
def run(self, command, prompt=1, verbose=1):
|
||||
"""Execute command within the shell as if it was typed in directly.
|
||||
>>> shell.run('print "this"')
|
||||
>>> print "this"
|
||||
this
|
||||
>>>
|
||||
>>>
|
||||
"""
|
||||
# Go to the very bottom of the text.
|
||||
endpos = self.GetTextLength()
|
||||
self.SetCurrentPos(endpos)
|
||||
self.SetCurrentPos(endpos)
|
||||
command = command.rstrip()
|
||||
if prompt: self.prompt()
|
||||
if verbose: self.write(command)
|
||||
@@ -670,7 +680,7 @@ class Shell(wxStyledTextCtrl):
|
||||
self.run(command, prompt=0, verbose=1)
|
||||
finally:
|
||||
file.close()
|
||||
|
||||
|
||||
def autoCompleteShow(self, command):
|
||||
"""Display auto-completion popup list."""
|
||||
list = self.interp.getAutoCompleteList(command, \
|
||||
@@ -693,11 +703,11 @@ class Shell(wxStyledTextCtrl):
|
||||
def writeOut(self, text):
|
||||
"""Replacement for stdout."""
|
||||
self.write(text)
|
||||
|
||||
|
||||
def writeErr(self, text):
|
||||
"""Replacement for stderr."""
|
||||
self.write(text)
|
||||
|
||||
|
||||
def redirectStdin(self, redirect=1):
|
||||
"""If redirect is true then sys.stdin will come from the shell."""
|
||||
if redirect:
|
||||
@@ -727,7 +737,7 @@ class Shell(wxStyledTextCtrl):
|
||||
return 1
|
||||
else:
|
||||
return 0
|
||||
|
||||
|
||||
def CanCopy(self):
|
||||
"""Return true if text is selected and can be copied."""
|
||||
return self.GetSelectionStart() != self.GetSelectionEnd()
|
||||
@@ -803,7 +813,7 @@ ID_CALLTIPS_SHOW = NewId()
|
||||
|
||||
class ShellMenu:
|
||||
"""Mixin class to add standard menu items."""
|
||||
|
||||
|
||||
def createMenus(self):
|
||||
m = self.fileMenu = wxMenu()
|
||||
m.AppendSeparator()
|
||||
@@ -968,14 +978,14 @@ class ShellMenu:
|
||||
event.Check(self.shell.autoCompleteIncludeDouble)
|
||||
elif id == ID_CALLTIPS_SHOW:
|
||||
event.Check(self.shell.autoCallTip)
|
||||
|
||||
|
||||
|
||||
class ShellFrame(wxFrame, ShellMenu):
|
||||
"""Frame containing the PyCrust shell component."""
|
||||
|
||||
|
||||
name = 'PyCrust Shell Frame'
|
||||
revision = __version__
|
||||
|
||||
|
||||
def __init__(self, parent=None, id=-1, title='PyShell', \
|
||||
pos=wxDefaultPosition, size=wxDefaultSize, \
|
||||
style=wxDEFAULT_FRAME_STYLE, locals=None, \
|
||||
@@ -999,5 +1009,5 @@ class ShellFrame(wxFrame, ShellMenu):
|
||||
self.createMenus()
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
@@ -6,5 +6,5 @@ __author__ = "Patrick K. O'Brien <pobrien@orbtech.com>"
|
||||
__cvsid__ = "$Id$"
|
||||
__version__ = "$Revision$"[11:-2]
|
||||
|
||||
VERSION = '0.7'
|
||||
|
||||
VERSION = '0.7.1'
|
||||
|
||||
|
Reference in New Issue
Block a user