shell.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 import argparse
00034 from python_qt_binding.QtCore import qVersion
00035 from qt_gui.plugin import Plugin
00036 from qt_gui_py_common.simple_settings_dialog import SimpleSettingsDialog
00037 
00038 from rqt_shell.shell_widget import ShellWidget
00039 
00040 try:
00041     if qVersion().startswith('5.'):
00042         raise ImportError('embedding is not support with Qt 5')
00043     from xterm_widget import XTermWidget, is_xterm_available
00044     _has_xterm = is_xterm_available()
00045 except ImportError:
00046     XTermWidget = None
00047     _has_xterm = False
00048 try:
00049     if qVersion().startswith('5.'):
00050         raise ImportError('spyderlib does not support Qt 5 yet')
00051     from spyder_shell_widget import SpyderShellWidget
00052     _has_spyderlib = True
00053 except ImportError:
00054     SpyderShellWidget = None
00055     _has_spyderlib = False
00056 
00057 
00058 class Shell(Plugin):
00059     """
00060     Plugin providing an interactive shell
00061     """
00062     # shell types in order of priority
00063     shell_types = [
00064         {
00065             'title': 'XTerm',
00066             'widget_class': XTermWidget,
00067             'description': 'Fully functional embedded XTerm (needs xterm, only works on X11 with Qt 4).',
00068             'enabled': _has_xterm,
00069         },
00070         {
00071             'title': 'SpyderShell',
00072             'widget_class': SpyderShellWidget,
00073             'description': 'Advanced shell (needs spyderlib, only works with Qt 4).',
00074             'enabled': _has_spyderlib,
00075         },
00076         {
00077             'title': 'SimpleShell',
00078             'widget_class': ShellWidget,
00079             'description': 'Simple shell for executing non-interactive finite commands.',
00080             'enabled': True,
00081         },
00082     ]
00083 
00084     def __init__(self, context):
00085         super(Shell, self).__init__(context)
00086         self._context = context
00087         self.setObjectName('Shell')
00088         self._args = self._parse_args(context.argv())
00089 
00090         self._widget = None
00091 
00092     def _parse_args(self, argv):
00093         parser = argparse.ArgumentParser(prog='rqt_shell')
00094         Shell.add_arguments(parser)
00095         return parser.parse_args(argv)
00096 
00097     @staticmethod
00098     def add_arguments(parser):
00099         group = parser.add_argument_group('Options for rqt_shell plugin')
00100         group.add_argument('-i', '--init-script',
00101                 help="Executes this script before staring a $SHELL subshell.  \
00102                 Doesn't work for SimpleShell. \
00103                 Always export any variables you intend on using in $SHELL")
00104 
00105     def _switch_shell_widget(self):
00106         # check for available shell type
00107         while not self.shell_types[self._shell_type_index]['enabled']:
00108             self._shell_type_index += 1
00109         selected_shell = self.shell_types[self._shell_type_index]
00110 
00111         if self._widget is not None:
00112             if hasattr(self._widget, 'close_signal'):
00113                 self._widget.close_signal.disconnect(self._context.close_plugin)
00114             self._context.remove_widget(self._widget)
00115             self._widget.close()
00116 
00117         self._widget = selected_shell['widget_class'](script_path=self._args.init_script)
00118         self._widget.setWindowTitle(selected_shell['title'])
00119         if self._context.serial_number() > 1:
00120             self._widget.setWindowTitle(self._widget.windowTitle() + (' (%d)' % self._context.serial_number()))
00121         self._context.add_widget(self._widget)
00122         if hasattr(self._widget, 'close_signal'):
00123             self._widget.close_signal.connect(self._context.close_plugin)
00124 
00125     def save_settings(self, plugin_settings, instance_settings):
00126         instance_settings.set_value('shell_type', self._shell_type_index)
00127 
00128     def restore_settings(self, plugin_settings, instance_settings):
00129         self._shell_type_index = int(instance_settings.value('shell_type', 0))
00130         self._switch_shell_widget()
00131 
00132     def trigger_configuration(self):
00133         dialog = SimpleSettingsDialog(title='Shell Options')
00134         dialog.add_exclusive_option_group(title='Shell Type', options=self.shell_types, selected_index=self._shell_type_index)
00135         shell_type = dialog.get_settings()[0]
00136         if shell_type is not None and self._shell_type_index != shell_type['selected_index']:
00137             self._shell_type_index = shell_type['selected_index']
00138             self._context.reload_plugin()
00139 
00140     def shutdown_plugin(self):
00141         if self._widget is not None and hasattr(self._widget, 'shutdown'):
00142             self._widget.shutdown()
00143 


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