New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add image message handler in ZMQTerminalInteractiveShell #1946
Changes from 20 commits
d232d49
0cd1e6d
b69c268
f462822
4a8f7c1
7cdae79
4528491
c2a1f15
beb6980
ce85550
fda5a33
5e90b7a
c43b749
ca773bf
7366f52
33978b3
57462a9
61e38b8
2e36d88
37580bc
449f64a
e5c748e
e9b4631
235a478
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
#----------------------------------------------------------------------------- | ||
# 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. | ||
#----------------------------------------------------------------------------- | ||
|
||
import os | ||
import unittest | ||
import base64 | ||
|
||
from IPython.zmq.kernelmanager import KernelManager | ||
from IPython.frontend.terminal.console.interactiveshell \ | ||
import ZMQTerminalInteractiveShell | ||
from IPython.utils.tempdir import NamedFileInTemporaryDirectory | ||
from IPython.testing.tools import monkeypatch | ||
from IPython.testing.decorators import skip_without | ||
from IPython.utils.ipstruct import Struct | ||
from IPython.utils.process import find_cmd | ||
|
||
|
||
SCRIPT_PATH = os.path.join( | ||
os.path.abspath(os.path.dirname(__file__)), 'writetofile.py') | ||
|
||
|
||
class ZMQTerminalInteractiveShellTestCase(unittest.TestCase): | ||
|
||
def setUp(self): | ||
km = KernelManager() | ||
self.shell = ZMQTerminalInteractiveShell(kernel_manager=km) | ||
self.raw = b'dummy data' | ||
self.mime = 'image/png' | ||
self.data = {self.mime: base64.encodestring(self.raw).decode('ascii')} | ||
|
||
def test_no_call_by_default(self): | ||
def raise_if_called(*args, **kwds): | ||
assert False | ||
|
||
shell = self.shell | ||
shell.handle_image_PIL | ||
shell.handle_image_stream | ||
shell.handle_image_tempfile | ||
shell.handle_image_callable | ||
|
||
shell.handle_image(None, None) # arguments are dummy | ||
|
||
@skip_without('PIL') | ||
def test_handle_image_PIL(self): | ||
import PIL.Image | ||
|
||
open_called_with = [] | ||
show_called_with = [] | ||
|
||
def fake_open(arg): | ||
open_called_with.append(arg) | ||
return Struct(show=lambda: show_called_with.append(None)) | ||
|
||
with monkeypatch(PIL.Image, 'open', fake_open): | ||
self.shell.handle_image_PIL(self.data, self.mime) | ||
|
||
assert len(open_called_with) == 1 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In these cases, it's nice to use |
||
assert len(show_called_with) == 1 | ||
assert 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 NamedFileInTemporaryDirectory('data') as file: | ||
cmd = [find_cmd('python'), SCRIPT_PATH, inpath, file.name] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
setattr(shell, configname, cmd) | ||
getattr(shell, funcname)(self.data, self.mime) | ||
transferred = file.read() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will this work on all OSs? You open the file here, write to it in another process, and then read it here. I assume that works on Linux, since the tests pass, but I've no idea about Windows in particular. @jstenar : Do you have time to test? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't need to open file before calling command, so I rewrote using TemporaryDirectory. |
||
|
||
assert 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) | ||
assert len(called_with) == 1 | ||
assert called_with[0] is self.data |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#----------------------------------------------------------------------------- | ||
# 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 | ||
from IPython.utils.py3compat import PY3 | ||
(inpath, outpath) = sys.argv[1:] | ||
|
||
if inpath == '-': | ||
if PY3: | ||
infile = sys.stdin.buffer | ||
else: | ||
infile = sys.stdin | ||
else: | ||
infile = open(inpath, 'rb') | ||
|
||
open(outpath, 'w+b').write(infile.read()) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#----------------------------------------------------------------------------- | ||
# 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. | ||
#----------------------------------------------------------------------------- | ||
|
||
import os | ||
|
||
from IPython.utils.tempdir import NamedFileInTemporaryDirectory | ||
|
||
|
||
def test_named_file_in_temporary_directory(): | ||
with NamedFileInTemporaryDirectory('filename') as file: | ||
name = file.name | ||
assert not file.closed | ||
assert os.path.exists(name) | ||
file.write(b'test') | ||
assert file.closed | ||
assert not os.path.exists(name) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should these lines be like
shell.handle_image_PIL = raise_if_called
?