def get_shell_dialog():
    global _shell_dialog

    if _shell_dialog is not None:
        return _shell_dialog

    try:
        deps = {'pip': 'ipython>=1.0',
                'linux-ubuntu': 'ipython-qtconsole',
                'linux-debian': 'ipython-qtconsole'}

        IPython = py_import('IPython.qt.console.rich_ipython_widget', deps,
                            True)
        RichIPythonWidget = \
                IPython.qt.console.rich_ipython_widget.RichIPythonWidget
        py_import('IPython.qt.inprocess', deps, True)
        QtInProcessKernelManager = \
                IPython.qt.inprocess.QtInProcessKernelManager
    except __HOLE__:
        return None

    km = QtInProcessKernelManager()
    km.start_kernel()
    kernel = km.kernel
    kernel.gui = 'qt4'

    kernel_client = km.client()
    kernel_client.start_channels()

    class IPythonDialog(RichIPythonWidget, QVistrailsPaletteInterface):
        """This class incorporates an  IPython shell into a dockable widget for use in the
        VisTrails environment"""
        def __init__(self, parent=None):
            RichIPythonWidget.__init__(self, parent)
            self.old_streams = None
            self.running_workflow = False
            self.kernel_manager = km
            self.kernel_client = kernel_client
            self.exit_requested.connect(self.stop)
            self.setWindowTitle("Console")
            self.vistrails_interpreter = get_default_interpreter()

        def visibility_changed(self, visible):
            QVistrailsPaletteInterface.visibility_changed(self, visible)
            if visible:
                self.show()
            else:
                self.hide()
        def stop(self):
            kernel_client.stop_channels()
            km.shutdown_kernel()

        def hide(self):
            """suspend() -> None
            Called when hiding the parent window in order to recover the previous
            state.

            """
            #recovering the state
            if self.old_streams is not None:
                sys.stdout, sys.stderr, sys.stdin = self.old_streams
                self.old_streams = None
            RichIPythonWidget.hide(self)

        def show(self):
            """show() -> None
            Store previous state and starts capturing all interactive input and
            output.

            """
            # capture all interactive input/output
            if self.old_streams is None:
                self.old_streams = sys.stdout, sys.stderr, sys.stdin
                sys.stdout   = self
                sys.stderr   = self
                sys.stdin    = self
            RichIPythonWidget.show(self)

        def showEvent(self, e):
            """showEvent(e) -> None
            Event handler called when the dialog acquires focus

            """
            self.show()

        def flush(self):
            """flush() -> None.
            Simulate stdin, stdout, and stderr.

            """
            pass

        def isatty(self):
            """isatty() -> int
            Simulate stdin, stdout, and stderr.

            """
            return 1

        def readline(self):
            """readline() -> str

            Simulate stdin, stdout, and stderr.

            """
            return ""

        def write(self, text):
            """write(text: str) -> None
            Simulate stdin, stdout, and stderr.

            """
            self.input_buffer = ''
            if not self.running_workflow:
                self.running_workflow = True
                # make text blue
                self._append_plain_text("\n\x1b[34m<STANDARD OUTPUT>\x1b[0m\n", True)
            self._append_plain_text(text, True)
            self._prompt_pos = self._get_end_cursor().position()
            self._control.ensureCursorVisible()
            self._control.moveCursor(QtGui.QTextCursor.End)

        def eventFilter(self, obj, event):
            """ Reimplemented to ensure a console-like behavior in the underlying
                text widgets.
            """
            etype = event.type()
            if etype == QtCore.QEvent.KeyPress:
                self.running_workflow = False
            return RichIPythonWidget.eventFilter(self, obj, event)

    _shell_dialog = IPythonDialog
    return IPythonDialog
dataset/ETHPy150Open VisTrails/VisTrails/vistrails/gui/shell.py/get_shell_dialog