<commit_before>"""
A GUI Ipython terminal window which can interact
with Glue. Based on code from

http://stackoverflow.com/a/9796491/1332492
and
http://stackoverflow.com/a/11525205/1332492

Usage:
   new_widget = glue_terminal(**kwargs)

Implementation Note:

Since v1.0dev, IPython implements embeddable in-process terminal widgets.
This functionality doesn't exist in v0.12 and v0.13 -- this module provides
a fallback implmentation for older IPython versions
"""

import sys
import atexit
from contextlib import contextmanager

# must import these first, to set up Qt properly
from ...external.qt import QtCore
from ...external.qt.QtGui import QInputDialog, QMdiSubWindow

from zmq import ZMQError
from zmq.eventloop.zmqstream import ZMQStream
from zmq.eventloop import ioloop

from IPython.utils.traitlets import TraitError
from IPython.lib.kernel import find_connection_file

try:   # IPython 1.0
    from IPython.kernel.zmq.ipkernel import Kernel
    from IPython.kernel.zmq.kernelapp import IPKernelApp
    from IPython.kernel.zmq.iostream import OutStream
    from IPython.qt.manager import QtKernelManager
    from IPython.qt.console.rich_ipython_widget import RichIPythonWidget

    # these are only needed for v1.0
    from IPython.kernel.connect import get_connection_file
    from IPython import get_ipython
    from IPython.qt.client import QtKernelClient
    from IPython.kernel.inprocess.ipkernel import InProcessKernel
    from IPython.kernel.inprocess.ipkernel import InProcessInteractiveShell
    from IPython.qt.inprocess import \
        QtInProcessKernelManager

except ImportError:
    from IPython.zmq.ipkernel import Kernel
    from IPython.zmq.ipkernel import IPKernelApp
    from IPython.zmq.iostream import OutStream
    from IPython.frontend.qt.kernelmanager import QtKernelManager
    from IPython.frontend.qt.console.rich_ipython_widget import \
        RichIPythonWidget


def in_process_console(console_class=RichIPythonWidget, **kwargs):
    """Create a console widget, connected to an in-process Kernel

    This only works on IPython v 0.13 and above

    Parameters
    ----------
    console_class : The class of the console widget to create
    kwargs : Extra variables to put into the namespace
    """

    km = QtInProcessKernelManager()
    km.start_kernel()

    kernel = km.kernel
    kernel.gui = 'qt4'

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

    control = console_class()
    control.kernel_manager = km
    control.kernel_client = client
    control.shell = kernel.shell
    control.shell.user_ns.update(**kwargs)
    return control


def connected_console(console_class=RichIPythonWidget, **kwargs):
    """Create a console widget, connected to another kernel running in
       the current process

    This only works on IPython v1.0 and above

    Parameters
    ----------
    console_class : The class of the console widget to create
    kwargs : Extra variables to put into the namespace
    """
    shell = get_ipython()
    if shell is None:
        raise RuntimeError("There is no IPython kernel in this process")

    client = QtKernelClient(connection_file=get_connection_file())
    client.load_connection_file()
    client.start_channels()

    control = console_class()
    control.kernel_client = client
    control.shell = shell
    control.shell.user_ns.update(**kwargs)
    return control


class DragAndDropTerminal(RichIPythonWidget):

    def __init__(self, **kwargs):
        super(DragAndDropTerminal, self).__init__(**kwargs)
        self.setAcceptDrops(True)
        self.shell = None

    def mdi_wrap(self):
        sub = QMdiSubWindow()
        sub.setWidget(self)
        self.destroyed.connect(sub.close)
        sub.resize(self.size())<commit_msg>Added Glue banner to terminal<commit_after>"""
A GUI Ipython terminal window which can interact
with Glue. Based on code from

http://stackoverflow.com/a/9796491/1332492
and
http://stackoverflow.com/a/11525205/1332492

Usage:
   new_widget = glue_terminal(**kwargs)

Implementation Note:

Since v1.0dev, IPython implements embeddable in-process terminal widgets.
This functionality doesn't exist in v0.12 and v0.13 -- this module provides
a fallback implmentation for older IPython versions
"""

import sys
import atexit
from contextlib import contextmanager

# must import these first, to set up Qt properly
from ...external.qt import QtCore
from ...external.qt.QtGui import QInputDialog, QMdiSubWindow
from ...version import __version__

from zmq import ZMQError
from zmq.eventloop.zmqstream import ZMQStream
from zmq.eventloop import ioloop

from IPython.utils.traitlets import TraitError
from IPython.lib.kernel import find_connection_file
from IPython.core.usage import default_banner

try:   # IPython 1.0
    from IPython.kernel.zmq.ipkernel import Kernel
    from IPython.kernel.zmq.kernelapp import IPKernelApp
    from IPython.kernel.zmq.iostream import OutStream
    from IPython.qt.manager import QtKernelManager
    from IPython.qt.console.rich_ipython_widget import RichIPythonWidget

    # these are only needed for v1.0
    from IPython.kernel.connect import get_connection_file
    from IPython import get_ipython
    from IPython.qt.client import QtKernelClient
    from IPython.kernel.inprocess.ipkernel import InProcessKernel
    from IPython.kernel.inprocess.ipkernel import InProcessInteractiveShell
    from IPython.qt.inprocess import \
        QtInProcessKernelManager

except ImportError:
    from IPython.zmq.ipkernel import Kernel
    from IPython.zmq.ipkernel import IPKernelApp
    from IPython.zmq.iostream import OutStream
    from IPython.frontend.qt.kernelmanager import QtKernelManager
    from IPython.frontend.qt.console.rich_ipython_widget import \
        RichIPythonWidget


def in_process_console(console_class=RichIPythonWidget, **kwargs):
    """Create a console widget, connected to an in-process Kernel

    This only works on IPython v 0.13 and above

    Parameters
    ----------
    console_class : The class of the console widget to create
    kwargs : Extra variables to put into the namespace
    """

    km = QtInProcessKernelManager()
    km.start_kernel()

    kernel = km.kernel
    kernel.gui = 'qt4'

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

    control = console_class()
    control.kernel_manager = km
    control.kernel_client = client
    control.shell = kernel.shell
    control.shell.user_ns.update(**kwargs)
    return control


def connected_console(console_class=RichIPythonWidget, **kwargs):
    """Create a console widget, connected to another kernel running in
       the current process

    This only works on IPython v1.0 and above

    Parameters
    ----------
    console_class : The class of the console widget to create
    kwargs : Extra variables to put into the namespace
    """
    shell = get_ipython()
    if shell is None:
        raise RuntimeError("There is no IPython kernel in this process")

    client = QtKernelClient(connection_file=get_connection_file())
    client.load_connection_file()
    client.start_channels()

    control = console_class()
    control.kernel_client = client
    control.shell = shell
    control.shell.user_ns.update(**kwargs)
    return control


glue_banner_parts = []
glue_banner_parts.append("Glue %s " %  __version__)
glue_banner_parts.append("Predefined variables - drag additional items into "
                         "this window to use:")
glue_banner_parts.append("\t* data_collection (aliased to dc)")
glue_banner_parts.append("\t* application")
glue_banner_parts.append("\t* hub")

glue_banner = '\n'.join(glue_banner_parts)


class DragAndDropTerminal(RichIPythonWidget):
    banner = default_banner + '\n' + glue_banner

    def __init__(self, **kwargs):
        super(DragAndDropTerminal, self).__init__(**kwargs)
        self.setAcceptDrops(True)
        self.shell = None

    def mdi_wrap(self):
        sub = QMdiSubWindow()
        sub.setWidget(self)
        self.destroyed.connect(sub.close)
        sub.resize(self.size())
{'fraction_non_alphanumeric': 0.07555167684827328, 'fraction_numerical': 0.011719237002867472, 'mean_word_length': 3.615650172612198, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5495821', 'n_tokens_mistral': 2453, 'n_tokens_neox': 2351, 'n_words': 674}