spyder_shell_widget.py
Go to the documentation of this file.
1 # Software License Agreement (BSD License)
2 #
3 # Copyright (c) 2012, Dorian Scholz
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
8 # are met:
9 #
10 # * Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 # * Redistributions in binary form must reproduce the above
13 # copyright notice, this list of conditions and the following
14 # disclaimer in the documentation and/or other materials provided
15 # with the distribution.
16 # * Neither the name of Willow Garage, Inc. nor the names of its
17 # contributors may be used to endorse or promote products derived
18 # from this software without specific prior written permission.
19 #
20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 # POSSIBILITY OF SUCH DAMAGE.
32 
33 from python_qt_binding.QtGui import QFont, QIcon
34 from python_qt_binding.QtCore import QProcess, SIGNAL, QTextCodec, Signal
35 
36 from spyderlib.widgets.externalshell.baseshell import ExternalShellBase
37 from spyderlib.widgets.shell import TerminalWidget
38 
39 import os
40 
41 
42 def is_string(s):
43  """Check if the argument is a string which works for both Python 2 and 3."""
44  try:
45  return isinstance(s, basestring)
46  except NameError:
47  return isinstance(s, str)
48 
49 
50 class SpyderShellWidget(ExternalShellBase):
51  """Spyder Shell Widget: execute a shell in a separate process using spyderlib's ExternalShellBase"""
52  SHELL_CLASS = TerminalWidget
53  close_signal = Signal()
54 
55  def __init__(self, parent=None, script_path=None):
56  ExternalShellBase.__init__(self, parent=parent, fname=None, wdir='.',
57  history_filename='.history',
58  light_background=True,
59  menu_actions=None,
60  show_buttons_inside=False,
61  show_elapsed_time=False)
62 
63  self.setObjectName('SpyderShellWidget')
64 
65  # capture tab key
66  #self.shell._key_tab = self._key_tab
67 
68  self.shell.set_pythonshell_font(QFont('Mono'))
69 
70  # Additional python path list
71  self.path = []
72 
73  # For compatibility with the other shells that can live in the external console
74  self.is_ipython_kernel = False
75  self.connection_file = None
76 
77  self.create_process(script_path=script_path)
78 
79  def get_icon(self):
80  return QIcon()
81 
82  def create_process(self, script_path=None):
83  self.shell.clear()
84 
85  self.process = QProcess(self)
86  self.process.setProcessChannelMode(QProcess.MergedChannels)
87 
88  env = []
89  for key_val_pair in self.process.systemEnvironment():
90  try:
91  value = unicode(key_val_pair)
92  except NameError:
93  value = str(key_val_pair)
94  env.append(value)
95  env.append('TERM=xterm')
96  env.append('COLORTERM=gnome-terminal')
97  self.process.setEnvironment(env)
98 
99  # Working directory
100  if self.wdir is not None:
101  self.process.setWorkingDirectory(self.wdir)
102 
103  self.process.readyReadStandardOutput.connect(self.write_output)
104  self.process.finished.connect(self.finished)
105  self.process.finished.connect(self.close_signal)
106 
107  if script_path:
108  options = [
109  "-c 'source %s; /bin/bash -i'" % os.path.abspath(script_path)]
110  else:
111  options = ['-i']
112  self.process.start('/bin/bash', options)
113 
114  running = self.process.waitForStarted()
115  self.set_running_state(running)
116  if not running:
117  self.shell.addPlainText("Process failed to start")
118  else:
119  self.shell.setFocus()
120  self.emit(SIGNAL('started()'))
121 
122  return self.process
123 
124  def shutdown(self):
125  self.process.kill()
126  self.process.waitForFinished()
127 
128  def _key_tab(self):
129  self.process.write('\t')
130  self.process.waitForBytesWritten(-1)
131  self.write_output()
132 
133  def send_to_process(self, text):
134  if not is_string(text):
135  try:
136  text = unicode(text)
137  except NameError:
138  text = str(text)
139  if not text.endswith('\n'):
140  text += '\n'
141  self.process.write(QTextCodec.codecForLocale().fromUnicode(text))
142  self.process.waitForBytesWritten(-1)
143 
145  self.send_ctrl_to_process('c')
def __init__(self, parent=None, script_path=None)


rqt_shell
Author(s): Dorian Scholz, Kunal Tyagi
autogenerated on Fri Apr 23 2021 02:35:36