Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 from python_qt_binding.QtGui import QFont, QIcon
00034 from python_qt_binding.QtCore import QProcess, SIGNAL, QTextCodec, Signal
00035
00036 from spyderlib.widgets.externalshell.baseshell import ExternalShellBase
00037 from spyderlib.widgets.shell import TerminalWidget
00038
00039 import os
00040
00041
00042 def is_string(s):
00043 """Check if the argument is a string which works for both Python 2 and 3."""
00044 try:
00045 return isinstance(s, basestring)
00046 except NameError:
00047 return isinstance(s, str)
00048
00049
00050 class SpyderShellWidget(ExternalShellBase):
00051 """Spyder Shell Widget: execute a shell in a separate process using spyderlib's ExternalShellBase"""
00052 SHELL_CLASS = TerminalWidget
00053 close_signal = Signal()
00054
00055 def __init__(self, parent=None, script_path=None):
00056 ExternalShellBase.__init__(self, parent=parent, fname=None, wdir='.',
00057 history_filename='.history',
00058 light_background=True,
00059 menu_actions=None,
00060 show_buttons_inside=False,
00061 show_elapsed_time=False)
00062
00063 self.setObjectName('SpyderShellWidget')
00064
00065
00066
00067
00068 self.shell.set_pythonshell_font(QFont('Mono'))
00069
00070
00071 self.path = []
00072
00073
00074 self.is_ipython_kernel = False
00075 self.connection_file = None
00076
00077 self.create_process(script_path=script_path)
00078
00079 def get_icon(self):
00080 return QIcon()
00081
00082 def create_process(self, script_path=None):
00083 self.shell.clear()
00084
00085 self.process = QProcess(self)
00086 self.process.setProcessChannelMode(QProcess.MergedChannels)
00087
00088 env = []
00089 for key_val_pair in self.process.systemEnvironment():
00090 try:
00091 value = unicode(key_val_pair)
00092 except NameError:
00093 value = str(key_val_pair)
00094 env.append(value)
00095 env.append('TERM=xterm')
00096 env.append('COLORTERM=gnome-terminal')
00097 self.process.setEnvironment(env)
00098
00099
00100 if self.wdir is not None:
00101 self.process.setWorkingDirectory(self.wdir)
00102
00103 self.process.readyReadStandardOutput.connect(self.write_output)
00104 self.process.finished.connect(self.finished)
00105 self.process.finished.connect(self.close_signal)
00106
00107 if script_path:
00108 options = [
00109 "-c 'source %s; /bin/bash -i'" % os.path.abspath(script_path)]
00110 else:
00111 options = ['-i']
00112 self.process.start('/bin/bash', options)
00113
00114 running = self.process.waitForStarted()
00115 self.set_running_state(running)
00116 if not running:
00117 self.shell.addPlainText("Process failed to start")
00118 else:
00119 self.shell.setFocus()
00120 self.emit(SIGNAL('started()'))
00121
00122 return self.process
00123
00124 def shutdown(self):
00125 self.process.kill()
00126 self.process.waitForFinished()
00127
00128 def _key_tab(self):
00129 self.process.write('\t')
00130 self.process.waitForBytesWritten(-1)
00131 self.write_output()
00132
00133 def send_to_process(self, text):
00134 if not is_string(text):
00135 try:
00136 text = unicode(text)
00137 except NameError:
00138 text = str(text)
00139 if not text.endswith('\n'):
00140 text += '\n'
00141 self.process.write(QTextCodec.codecForLocale().fromUnicode(text))
00142 self.process.waitForBytesWritten(-1)
00143
00144 def keyboard_interrupt(self):
00145 self.send_ctrl_to_process('c')