shell.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 import argparse
34 from python_qt_binding.QtCore import qVersion
35 from qt_gui.plugin import Plugin
36 from qt_gui_py_common.simple_settings_dialog import SimpleSettingsDialog
37 
38 from rqt_shell.shell_widget import ShellWidget
39 
40 try:
41  if qVersion().startswith('5.'):
42  raise ImportError('embedding is not support with Qt 5')
43  from xterm_widget import XTermWidget, is_xterm_available
44  _has_xterm = is_xterm_available()
45 except ImportError:
46  XTermWidget = None
47  _has_xterm = False
48 try:
49  if qVersion().startswith('5.'):
50  raise ImportError('spyderlib does not support Qt 5 yet')
51  from spyder_shell_widget import SpyderShellWidget
52  _has_spyderlib = True
53 except ImportError:
54  SpyderShellWidget = None
55  _has_spyderlib = False
56 
57 
58 class Shell(Plugin):
59  """
60  Plugin providing an interactive shell
61  """
62  # shell types in order of priority
63  shell_types = [
64  {
65  'title': 'XTerm',
66  'widget_class': XTermWidget,
67  'description': 'Fully functional embedded XTerm (needs xterm, only works on X11 with Qt 4).',
68  'enabled': _has_xterm,
69  },
70  {
71  'title': 'SpyderShell',
72  'widget_class': SpyderShellWidget,
73  'description': 'Advanced shell (needs spyderlib, only works with Qt 4).',
74  'enabled': _has_spyderlib,
75  },
76  {
77  'title': 'SimpleShell',
78  'widget_class': ShellWidget,
79  'description': 'Simple shell for executing non-interactive finite commands.',
80  'enabled': True,
81  },
82  ]
83 
84  def __init__(self, context):
85  super(Shell, self).__init__(context)
86  self._context = context
87  self.setObjectName('Shell')
88  self._args = self._parse_args(context.argv())
89 
90  self._widget = None
91 
92  def _parse_args(self, argv):
93  parser = argparse.ArgumentParser(prog='rqt_shell')
94  Shell.add_arguments(parser)
95  return parser.parse_args(argv)
96 
97  @staticmethod
98  def add_arguments(parser):
99  group = parser.add_argument_group('Options for rqt_shell plugin')
100  group.add_argument('-i', '--init-script',
101  help="Executes this script before staring a $SHELL subshell. \
102  Doesn't work for SimpleShell. \
103  Always export any variables you intend on using in $SHELL")
104 
106  # check for available shell type
107  while not self.shell_types[self._shell_type_index]['enabled']:
108  self._shell_type_index += 1
109  selected_shell = self.shell_types[self._shell_type_index]
110 
111  if self._widget is not None:
112  if hasattr(self._widget, 'close_signal'):
113  self._widget.close_signal.disconnect(self._context.close_plugin)
114  self._context.remove_widget(self._widget)
115  self._widget.close()
116 
117  self._widget = selected_shell['widget_class'](script_path=self._args.init_script)
118  self._widget.setWindowTitle(selected_shell['title'])
119  if self._context.serial_number() > 1:
120  self._widget.setWindowTitle(self._widget.windowTitle() + (' (%d)' % self._context.serial_number()))
121  self._context.add_widget(self._widget)
122  if hasattr(self._widget, 'close_signal'):
123  self._widget.close_signal.connect(self._context.close_plugin)
124 
125  def save_settings(self, plugin_settings, instance_settings):
126  instance_settings.set_value('shell_type', self._shell_type_index)
127 
128  def restore_settings(self, plugin_settings, instance_settings):
129  self._shell_type_index = int(instance_settings.value('shell_type', 0))
130  self._switch_shell_widget()
131 
133  dialog = SimpleSettingsDialog(title='Shell Options')
134  dialog.add_exclusive_option_group(title='Shell Type', options=self.shell_types, selected_index=self._shell_type_index)
135  shell_type = dialog.get_settings()[0]
136  if shell_type is not None and self._shell_type_index != shell_type['selected_index']:
137  self._shell_type_index = shell_type['selected_index']
138  self._context.reload_plugin()
139 
140  def shutdown_plugin(self):
141  if self._widget is not None and hasattr(self._widget, 'shutdown'):
142  self._widget.shutdown()
143 
def trigger_configuration(self)
Definition: shell.py:132
def add_arguments(parser)
Definition: shell.py:98
def shutdown_plugin(self)
Definition: shell.py:140
def save_settings(self, plugin_settings, instance_settings)
Definition: shell.py:125
def _parse_args(self, argv)
Definition: shell.py:92
def __init__(self, context)
Definition: shell.py:84
def restore_settings(self, plugin_settings, instance_settings)
Definition: shell.py:128
def _switch_shell_widget(self)
Definition: shell.py:105


rqt_shell
Author(s): Dorian Scholz
autogenerated on Fri Jun 7 2019 21:50:05