Uploaded Test files

This commit is contained in:
Batuhan Berk Başoğlu 2020-11-12 11:05:57 -05:00
parent f584ad9d97
commit 2e81cb7d99
16627 changed files with 2065359 additions and 102444 deletions

View file

@ -0,0 +1,3 @@
"""Jupyter terminal console"""
from ._version import version_info, __version__

View file

@ -0,0 +1,4 @@
from jupyter_console import app
if __name__ == '__main__':
app.launch_new_instance()

View file

@ -0,0 +1,10 @@
""" For beta/alpha/rc releases, the version number for a beta is X.Y.ZbN
**without dots between the last 'micro' number and b**. N is the number of
the beta released i.e. 1, 2, 3 ...
See PEP 440 https://www.python.org/dev/peps/pep-0440/
"""
version_info = (6, 2, 0)
__version__ = '.'.join(map(str, version_info[:3])) + ''.join(version_info[3:])

View file

@ -0,0 +1,159 @@
""" A minimal application using the ZMQ-based terminal IPython frontend.
This is not a complete console app, as subprocess will not be able to receive
input, there is no real readline support, among other limitations.
"""
# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.
from __future__ import print_function
import logging
import signal
import sys
from traitlets import (
Dict, Any
)
from traitlets.config import catch_config_error, boolean_flag
from jupyter_core.application import JupyterApp, base_aliases, base_flags, NoStart
from jupyter_client.consoleapp import (
JupyterConsoleApp, app_aliases, app_flags,
)
from jupyter_console.ptshell import ZMQTerminalInteractiveShell
from jupyter_console import __version__
#-----------------------------------------------------------------------------
# Globals
#-----------------------------------------------------------------------------
_examples = """
jupyter console # start the ZMQ-based console
jupyter console --existing # connect to an existing ipython session
"""
#-----------------------------------------------------------------------------
# Flags and Aliases
#-----------------------------------------------------------------------------
# copy flags from mixin:
flags = dict(base_flags)
# start with mixin frontend flags:
# update full dict with frontend flags:
flags.update(app_flags)
flags.update(boolean_flag(
'simple-prompt', 'ZMQTerminalInteractiveShell.simple_prompt',
"Force simple minimal prompt using `raw_input`",
"Use a rich interactive prompt with prompt_toolkit"
))
# copy flags from mixin
aliases = dict(base_aliases)
aliases.update(app_aliases)
frontend_aliases = set(app_aliases.keys())
frontend_flags = set(app_flags.keys())
#-----------------------------------------------------------------------------
# Classes
#-----------------------------------------------------------------------------
class ZMQTerminalIPythonApp(JupyterApp, JupyterConsoleApp):
name = "jupyter-console"
version = __version__
"""Start a terminal frontend to the IPython zmq kernel."""
description = """
The Jupyter terminal-based Console.
This launches a Console application inside a terminal.
The Console supports various extra features beyond the traditional
single-process Terminal IPython shell, such as connecting to an
existing ipython session, via:
jupyter console --existing
where the previous session could have been created by another ipython
console, an ipython qtconsole, or by opening an ipython notebook.
"""
examples = _examples
classes = [ZMQTerminalInteractiveShell] + JupyterConsoleApp.classes
flags = Dict(flags)
aliases = Dict(aliases)
frontend_aliases = Any(frontend_aliases)
frontend_flags = Any(frontend_flags)
subcommands = Dict()
force_interact = True
def parse_command_line(self, argv=None):
super(ZMQTerminalIPythonApp, self).parse_command_line(argv)
self.build_kernel_argv(self.extra_args)
def init_shell(self):
JupyterConsoleApp.initialize(self)
# relay sigint to kernel
signal.signal(signal.SIGINT, self.handle_sigint)
self.shell = ZMQTerminalInteractiveShell.instance(parent=self,
manager=self.kernel_manager,
client=self.kernel_client,
confirm_exit=self.confirm_exit,
)
self.shell.own_kernel = not self.existing
def init_gui_pylab(self):
# no-op, because we don't want to import matplotlib in the frontend.
pass
def handle_sigint(self, *args):
if self.shell._executing:
if self.kernel_manager:
self.kernel_manager.interrupt_kernel()
else:
print("ERROR: Cannot interrupt kernels we didn't start.",
file = sys.stderr)
else:
# raise the KeyboardInterrupt if we aren't waiting for execution,
# so that the interact loop advances, and prompt is redrawn, etc.
raise KeyboardInterrupt
@catch_config_error
def initialize(self, argv=None):
"""Do actions after construct, but before starting the app."""
super(ZMQTerminalIPythonApp, self).initialize(argv)
if self._dispatching:
return
# create the shell
self.init_shell()
# and draw the banner
self.init_banner()
def init_banner(self):
"""optionally display the banner"""
self.shell.show_banner()
# Make sure there is a space below the banner.
#if self.log_level <= logging.INFO: print()
def start(self):
# JupyterApp.start dispatches on NoStart
super(ZMQTerminalIPythonApp, self).start()
self.log.debug("Starting the jupyter console mainloop...")
self.shell.mainloop()
main = launch_new_instance = ZMQTerminalIPythonApp.launch_instance
if __name__ == '__main__':
main()

View file

@ -0,0 +1,40 @@
# -*- coding: utf-8 -*-
"""Adapt readline completer interface to make ZMQ request."""
# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.
from traitlets.config import Configurable
from traitlets import Float
class ZMQCompleter(Configurable):
"""Client-side completion machinery.
How it works: self.complete will be called multiple times, with
state=0,1,2,... When state=0 it should compute ALL the completion matches,
and then return them for each value of state."""
timeout = Float(5.0, config=True, help='timeout before completion abort')
def __init__(self, shell, client, config=None):
super(ZMQCompleter,self).__init__(config=config)
self.shell = shell
self.client = client
self.matches = []
def complete_request(self, code, cursor_pos):
# send completion request to kernel
# Give the kernel up to 5s to respond
msg_id = self.client.complete(
code=code,
cursor_pos=cursor_pos,
)
msg = self.client.shell_channel.get_msg(timeout=self.timeout)
if msg['parent_header']['msg_id'] == msg_id:
return msg['content']
return {'matches': [], 'cursor_start': 0, 'cursor_end': 0,
'metadata': {}, 'status': 'ok'}

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,6 @@
import pytest
@pytest.fixture(autouse=True)
def env_setup(monkeypatch):
monkeypatch.setenv("JUPYTER_CONSOLE_TEST", "1")

View file

@ -0,0 +1,84 @@
"""Tests for two-process terminal frontend"""
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.
import os
import shutil
import sys
import tempfile
from subprocess import check_output
from nose import SkipTest
from traitlets.tests.utils import check_help_all_output
from ipython_genutils.testing import decorators as dec
@dec.skip_win32
def test_console_starts():
"""test that `jupyter console` starts a terminal"""
p, pexpect, t = start_console()
p.sendline('5')
idx = p.expect([r'Out\[\d+\]: 5', pexpect.EOF], timeout=t)
idx = p.expect([r'In \[\d+\]', pexpect.EOF], timeout=t)
stop_console(p, pexpect, t)
def test_help_output():
"""jupyter console --help-all works"""
check_help_all_output('jupyter_console')
def test_display_text():
"Ensure display protocol plain/text key is supported"
# equivalent of:
#
# x = %lsmagic
# from IPython.display import display; display(x);
p, pexpect, t = start_console()
p.sendline('x = %lsmagic')
p.expect(r'In \[\d+\]', timeout=t)
p.sendline('from IPython.display import display; display(x);')
p.expect(r'Available line magics:', timeout=t)
p.expect(r'In \[\d+\]', timeout=t)
stop_console(p, pexpect, t)
def stop_console(p, pexpect, t):
"Stop a running `jupyter console` running via pexpect"
# send ctrl-D;ctrl-D to exit
p.sendeof()
p.sendeof()
p.expect([pexpect.EOF, pexpect.TIMEOUT], timeout=t)
if p.isalive():
p.terminate()
def start_console():
"Start `jupyter console` using pexpect"
import pexpect
args = ['-m', 'jupyter_console', '--colors=NoColor']
cmd = sys.executable
env = os.environ.copy()
env["JUPYTER_CONSOLE_TEST"] = "1"
env["PROMPT_TOOLKIT_NO_CPR"] = "1"
try:
p = pexpect.spawn(cmd, args=args, env=env)
except IOError:
raise SkipTest("Couldn't find command %s" % cmd)
# timeout after one minute
t = 60
idx = p.expect(r'In \[\d+\]', timeout=t)
return p, pexpect, t
def test_generate_config():
"""jupyter console --generate-config works"""
td = tempfile.mkdtemp()
try:
check_output([sys.executable, '-m', 'jupyter_console', '--generate-config'],
env={'JUPYTER_CONFIG_DIR': td},
)
assert os.path.isfile(os.path.join(td, 'jupyter_console_config.py'))
finally:
shutil.rmtree(td)

View file

@ -0,0 +1,102 @@
# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.
import os
import sys
import unittest
import base64
from unittest.mock import patch
from jupyter_console.ptshell import ZMQTerminalInteractiveShell
from ipython_genutils.tempdir import TemporaryDirectory
from ipython_genutils.testing.decorators import skip_without
from ipython_genutils.ipstruct import Struct
SCRIPT_PATH = os.path.join(
os.path.abspath(os.path.dirname(__file__)), 'writetofile.py')
class NonCommunicatingShell(ZMQTerminalInteractiveShell):
"""A testing shell class that doesn't attempt to communicate with the kernel"""
def init_kernel_info(self):
pass
class ZMQTerminalInteractiveShellTestCase(unittest.TestCase):
def setUp(self):
self.shell = NonCommunicatingShell()
self.raw = b'dummy data'
self.mime = 'image/png'
self.data = {self.mime: base64.encodebytes(self.raw).decode('ascii')}
def test_call_pil_by_default(self):
pil_called_with = []
def pil_called(data, mime):
pil_called_with.append(data)
def raise_if_called(*args, **kwds):
assert False
shell = self.shell
shell.handle_image_PIL = pil_called
shell.handle_image_stream = raise_if_called
shell.handle_image_tempfile = raise_if_called
shell.handle_image_callable = raise_if_called
shell.handle_image(None, None) # arguments are dummy
assert len(pil_called_with) == 1
@skip_without('PIL')
def test_handle_image_PIL(self):
from PIL import Image, ImageShow
open_called_with = []
show_called_with = []
def fake_open(arg):
open_called_with.append(arg)
def fake_show(img):
show_called_with.append(img)
with patch.object(Image, 'open', fake_open), \
patch.object(ImageShow, 'show', fake_show):
self.shell.handle_image_PIL(self.data, self.mime)
self.assertEqual(len(open_called_with), 1)
self.assertEqual(len(show_called_with), 1)
self.assertEqual(open_called_with[0].getvalue(), self.raw)
def check_handler_with_file(self, inpath, handler):
shell = self.shell
configname = '{0}_image_handler'.format(handler)
funcname = 'handle_image_{0}'.format(handler)
assert hasattr(shell, configname)
assert hasattr(shell, funcname)
with TemporaryDirectory() as tmpdir:
outpath = os.path.join(tmpdir, 'data')
cmd = [sys.executable, SCRIPT_PATH, inpath, outpath]
setattr(shell, configname, cmd)
getattr(shell, funcname)(self.data, self.mime)
# cmd is called and file is closed. So it's safe to open now.
with open(outpath, 'rb') as file:
transferred = file.read()
self.assertEqual(transferred, self.raw)
def test_handle_image_stream(self):
self.check_handler_with_file('-', 'stream')
def test_handle_image_tempfile(self):
self.check_handler_with_file('{file}', 'tempfile')
def test_handle_image_callable(self):
called_with = []
self.shell.callable_image_handler = called_with.append
self.shell.handle_image_callable(self.data, self.mime)
self.assertEqual(len(called_with), 1)
assert called_with[0] is self.data

View file

@ -0,0 +1,29 @@
#-----------------------------------------------------------------------------
# Copyright (C) 2012 The IPython Development Team
#
# Distributed under the terms of the BSD License. The full license is in
# the file COPYING, distributed as part of this software.
#-----------------------------------------------------------------------------
"""
Copy data from input file to output file for testing.
Command line usage:
python writetofile.py INPUT OUTPUT
Binary data from INPUT file is copied to OUTPUT file.
If INPUT is '-', stdin is used.
"""
if __name__ == '__main__':
import sys
(inpath, outpath) = sys.argv[1:]
if inpath == '-':
infile = sys.stdin.buffer
else:
infile = open(inpath, 'rb')
open(outpath, 'w+b').write(infile.read())

View file

@ -0,0 +1,92 @@
""" ZMQ Kernel History accessor and manager. """
#-----------------------------------------------------------------------------
# Copyright (C) 2010-2011 The IPython Development Team.
#
# Distributed under the terms of the BSD License.
#
# The full license is in the file COPYING.txt, distributed with this software.
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
from IPython.core.history import HistoryAccessorBase
from traitlets import Dict, List
from queue import Empty # Py 3
class ZMQHistoryManager(HistoryAccessorBase):
"""History accessor and manager for ZMQ-based kernels"""
input_hist_parsed = List([""])
output_hist = Dict()
dir_hist = List()
output_hist_reprs = Dict()
def __init__(self, client):
"""
Class to load the command-line history from a ZMQ-based kernel,
and access the history.
Parameters
----------
client: `IPython.kernel.KernelClient`
The kernel client in order to request the history.
"""
self.client = client
def _load_history(self, raw=True, output=False, hist_access_type='range',
**kwargs):
"""
Load the history over ZMQ from the kernel. Wraps the history
messaging with loop to wait to get history results.
"""
history = []
if hasattr(self.client, "history"):
## In tests, KernelClient may not have a history method
msg_id = self.client.history(raw=raw, output=output,
hist_access_type=hist_access_type,
**kwargs)
while True:
try:
reply = self.client.get_shell_msg(timeout=1)
except Empty:
break
else:
if reply['parent_header'].get('msg_id') == msg_id:
history = reply['content'].get('history', [])
break
return history
def get_tail(self, n=10, raw=True, output=False, include_latest=False):
return self._load_history(hist_access_type='tail', n=n, raw=raw,
output=output)
def search(self, pattern="*", raw=True, search_raw=True,
output=False, n=None, unique=False):
return self._load_history(hist_access_type='search', pattern=pattern,
raw=raw, search_raw=search_raw,
output=output, n=n, unique=unique)
def get_range(self, session, start=1, stop=None, raw=True,output=False):
return self._load_history(hist_access_type='range', raw=raw,
output=output, start=start, stop=stop,
session=session)
def get_range_by_str(self, rangestr, raw=True, output=False):
return self._load_history(hist_access_type='range', raw=raw,
output=output, rangestr=rangestr)
def end_session(self):
"""
Nothing to do for ZMQ-based histories.
"""
pass
def reset(self, new_session=True):
"""
Nothing to do for ZMQ-based histories.
"""
pass