spyder_shell_widget.py
Go to the documentation of this file.
00001 # Software License Agreement (BSD License)
00002 #
00003 # Copyright (c) 2012, Dorian Scholz
00004 # All rights reserved.
00005 #
00006 # Redistribution and use in source and binary forms, with or without
00007 # modification, are permitted provided that the following conditions
00008 # are met:
00009 #
00010 #  * Redistributions of source code must retain the above copyright
00011 #    notice, this list of conditions and the following disclaimer.
00012 #  * Redistributions in binary form must reproduce the above
00013 #    copyright notice, this list of conditions and the following
00014 #    disclaimer in the documentation and/or other materials provided
00015 #    with the distribution.
00016 #  * Neither the name of Willow Garage, Inc. nor the names of its
00017 #    contributors may be used to endorse or promote products derived
00018 #    from this software without specific prior written permission.
00019 #
00020 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00021 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00022 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00023 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00024 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00025 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00026 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00027 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00028 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00029 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00030 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00031 # POSSIBILITY OF SUCH DAMAGE.
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 
00040 class SpyderShellWidget(ExternalShellBase):
00041     """Spyder Shell Widget: execute a shell in a separate process using spyderlib's ExternalShellBase"""
00042     SHELL_CLASS = TerminalWidget
00043     close_signal = Signal()
00044 
00045     def __init__(self, parent=None):
00046         ExternalShellBase.__init__(self, parent=parent, fname=None, wdir='.',
00047                                    history_filename='.history',
00048                                    light_background=True,
00049                                    menu_actions=None,
00050                                    show_buttons_inside=False,
00051                                    show_elapsed_time=False)
00052 
00053         self.setObjectName('SpyderShellWidget')
00054 
00055         # capture tab key
00056         #self.shell._key_tab = self._key_tab
00057 
00058         self.shell.set_pythonshell_font(QFont('Mono'))
00059 
00060         # Additional python path list
00061         self.path = []
00062 
00063         # For compatibility with the other shells that can live in the external console
00064         self.is_ipython_kernel = False
00065         self.connection_file = None
00066 
00067         self.create_process()
00068 
00069     def get_icon(self):
00070         return QIcon()
00071 
00072     def create_process(self):
00073         self.shell.clear()
00074 
00075         self.process = QProcess(self)
00076         self.process.setProcessChannelMode(QProcess.MergedChannels)
00077 
00078         env = [unicode(key_val_pair) for key_val_pair in self.process.systemEnvironment()]
00079         env.append('TERM=xterm')
00080         env.append('COLORTERM=gnome-terminal')
00081         self.process.setEnvironment(env)
00082 
00083         # Working directory
00084         if self.wdir is not None:
00085             self.process.setWorkingDirectory(self.wdir)
00086 
00087         self.process.readyReadStandardOutput.connect(self.write_output)
00088         self.process.finished.connect(self.finished)
00089         self.process.finished.connect(self.close_signal)
00090 
00091         self.process.start('/bin/bash', ['-i'])
00092 
00093         running = self.process.waitForStarted()
00094         self.set_running_state(running)
00095         if not running:
00096             self.shell.addPlainText("Process failed to start")
00097         else:
00098             self.shell.setFocus()
00099             self.emit(SIGNAL('started()'))
00100 
00101         return self.process
00102 
00103     def shutdown(self):
00104         self.process.kill()
00105         self.process.waitForFinished()
00106 
00107     def _key_tab(self):
00108         self.process.write('\t')
00109         self.process.waitForBytesWritten(-1)
00110         self.write_output()
00111 
00112     def send_to_process(self, text):
00113         if not isinstance(text, basestring):
00114             text = unicode(text)
00115         if not text.endswith('\n'):
00116             text += '\n'
00117         self.process.write(QTextCodec.codecForLocale().fromUnicode(text))
00118         self.process.waitForBytesWritten(-1)
00119 
00120     def keyboard_interrupt(self):
00121         self.send_ctrl_to_process('c')


rqt_shell
Author(s): Dorian Scholz
autogenerated on Wed Sep 16 2015 06:58:18