I have a fix for the PasteAndRun method of the wx.py.shell.Shell. I
also propose a change to split the run functionality from the
PasteAndRun method, as I use it in SPE when a user selects code, so
that he doesn’t have first to copy and then to paste it.
Now you get a syntax error when pasting python source which contains
else, elif or except (am I forgetting one?) as it considers it from
there as a new command, while it belongs to the same previous command.
The following code solves the issue. The new Paste and Run method could
be based on this, leaving the Execute method also available please. If
you want I could write the new PasteAndRun method, but it is quite
trivial.
Stani
PS So now the code:
def Execute(self,text):
"""Replace selection with clipboard contents, run commands."""
ps1 = str(sys.ps1)
ps2 = str(sys.ps2)
endpos = self.GetTextLength()
self.SetCurrentPos(endpos)
startpos = self.promptPosEnd
self.SetSelection(startpos, endpos)
self.ReplaceSelection('')
text = text.lstrip()
text = self.fixLineEndings(text)
text = self.lstripPrompt(text)
text = text.replace(os.linesep + ps1, '\n')
text = text.replace(os.linesep + ps2, '\n')
text = text.replace(os.linesep, '\n')
lines = text.split('\n')
commands = []
command = ''
for line in lines:
if line.strip() == ps2.strip():
If we are pasting from something like a
web page that drops the trailing space
from the ps2 prompt of a blank line.
line = ''
lstrip = line.lstrip()
if line.strip() != '' and lstrip == line and \
lstrip[:4] not in [‘else’,‘elif’] and \
lstrip[:6] != ‘except’:
# New command.
if command:
Add the previous command to the list.
commands.append(command)
Start a new command, which may be multiline.
command = line
else:
Multiline command. Add to the command.
command += '\n'
command += line
commands.append(command)
for command in commands:
command = command.replace('\n', os.linesep + ps2)
self.write(command)
self.processLine()
I have a fix for the PasteAndRun method of the wx.py.shell.Shell. I also propose a change to split the run functionality from the PasteAndRun method, as I use it in SPE when a user selects code, so that he doesn't have first to copy and then to paste it.
Now you get a syntax error when pasting python source which contains else, elif or except (am I forgetting one?) as it considers it from there as a new command, while it belongs to the same previous command. The following code solves the issue. The new Paste and Run method could be based on this, leaving the Execute method also available please. If you want I could write the new PasteAndRun method, but it is quite trivial.
Is something like this what you had in mind?
def PasteAndRun(self):
"""Replace selection with clipboard contents, run commands."""
text = ''
if wx.TheClipboard.Open():
if wx.TheClipboard.IsSupported(wx.DataFormat(wx.DF_TEXT)):
data = wx.TextDataObject()
if wx.TheClipboard.GetData(data):
text = data.GetText()
wx.TheClipboard.Close()
if text:
self.Execute(text)
def Execute(self, text):
"""Replace selection with text and run commands."""
ps1 = str(sys.ps1)
ps2 = str(sys.ps2)
endpos = self.GetTextLength()
self.SetCurrentPos(endpos)
startpos = self.promptPosEnd
self.SetSelection(startpos, endpos)
self.ReplaceSelection('')
text = text.lstrip()
text = self.fixLineEndings(text)
text = self.lstripPrompt(text)
text = text.replace(os.linesep + ps1, '\n')
text = text.replace(os.linesep + ps2, '\n')
text = text.replace(os.linesep, '\n')
lines = text.split('\n')
commands =
command = ''
for line in lines:
if line.strip() == ps2.strip():
# If we are pasting from something like a
# web page that drops the trailing space
# from the ps2 prompt of a blank line.
line = ''
lstrip = line.lstrip()
if line.strip() != '' and lstrip == line and \
lstrip[:4] not in ['else','elif'] and \
lstrip[:6] != 'except':
# New command.
if command:
# Add the previous command to the list.
commands.append(command)
# Start a new command, which may be multiline.
command = line
else:
# Multiline command. Add to the command.
command += '\n'
command += line
commands.append(command)
for command in commands:
command = command.replace('\n', os.linesep + ps2)
self.write(command)
self.processLine()
···
--
Robin Dunn
Software Craftsman http://wxPython.org Java give you jitters? Relax with wxPython!