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 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         # capture tab key
00066         #self.shell._key_tab = self._key_tab
00067 
00068         self.shell.set_pythonshell_font(QFont('Mono'))
00069 
00070         # Additional python path list
00071         self.path = []
00072 
00073         # For compatibility with the other shells that can live in the external console
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         # Working directory
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')


rqt_shell
Author(s): Dorian Scholz
autogenerated on Sat Sep 9 2017 02:43:57