diff --git a/bash_kernel/__init__.py b/bash_kernel/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/bash_kernel/__main__.py b/bash_kernel/__main__.py new file mode 100644 index 0000000..683580b --- /dev/null +++ b/bash_kernel/__main__.py @@ -0,0 +1,3 @@ +from IPython.kernel.zmq.kernelapp import IPKernelApp +from .kernel import BashKernel +IPKernelApp.launch_instance(kernel_class=BashKernel) diff --git a/bash_kernel/images.py b/bash_kernel/images.py new file mode 100644 index 0000000..44f7026 --- /dev/null +++ b/bash_kernel/images.py @@ -0,0 +1,48 @@ +import base64 +import imghdr +import os + +#from IPython. + +_TEXT_SAVED_IMAGE = "bash_kernel: saved image data to:" + +image_setup_cmd = """ +display () { + TMPFILE=$(mktemp ${TMPDIR-/tmp}/bash_kernel.XXXXXXXXXX) + cat > $TMPFILE + echo "%s $TMPFILE" >&2 +} +""" % _TEXT_SAVED_IMAGE + +def display_data_for_image(filename): + with open(filename, 'rb') as f: + image = f.read() + os.unlink(filename) + + image_type = imghdr.what(None, image) + if image_type is None: + raise ValueError("Not a valid image: %s" % image) + + image_data = base64.b64encode(image).decode('ascii') + content = { + 'data': { + 'image/' + image_type: image_data + }, + 'metadata': {} + } + return content + + +def extract_image_filenames(output): + output_lines = [] + image_filenames = [] + + for line in output.split("\n"): + if line.startswith(_TEXT_SAVED_IMAGE): + filename = line.rstrip().split(": ")[-1] + image_filenames.append(filename) + else: + output_lines.append(line) + + output = "\n".join(output_lines) + return image_filenames, output diff --git a/bash_kernel.py b/bash_kernel/kernel.py similarity index 74% rename from bash_kernel.py rename to bash_kernel/kernel.py index a90837b..f88a46b 100644 --- a/bash_kernel.py +++ b/bash_kernel/kernel.py @@ -14,13 +14,14 @@ __version__ = '0.2' version_pat = re.compile(r'version (\d+(\.\d+)+)') -_TEXT_SAVED_IMAGE = "bash_kernel: saved image data to:" +from .images import ( + extract_image_filenames, display_data_for_image, image_setup_cmd +) class BashKernel(Kernel): implementation = 'bash_kernel' implementation_version = __version__ - language = 'bash' @property def language_version(self): @@ -35,9 +36,10 @@ class BashKernel(Kernel): self._banner = check_output(['bash', '--version']).decode('utf-8') return self._banner - language_info = {'codemirror_mode': 'shell', + language_info = {'name': 'bash', + 'codemirror_mode': 'shell', 'mimetype': 'text/x-sh', - 'file_extension': 'sh'} + 'file_extension': '.sh'} def __init__(self, **kwargs): Kernel.__init__(self, **kwargs) @@ -55,14 +57,7 @@ class BashKernel(Kernel): signal.signal(signal.SIGINT, sig) # Register Bash function to write image data to temporary file - bash_rc = """ - display () { - TMPFILE=$(mktemp ${TMPDIR-/tmp}/bash_kernel.XXXXXXXXXX) - cat > $TMPFILE - echo "%s $TMPFILE" >&2 - } - """ % _TEXT_SAVED_IMAGE - self.bashwrapper.run_command(bash_rc) + self.bashwrapper.run_command(image_setup_cmd) def do_execute(self, code, silent, store_history=True, user_expressions=None, allow_stdin=False): @@ -92,7 +87,7 @@ class BashKernel(Kernel): # Send images, if any for filename in image_filenames: try: - data = display_data(filename) + data = display_data_for_image(filename) except ValueError as e: message = {'name': 'stdout', 'text': str(e)} self.send_response(self.iopub_socket, 'stream', message) @@ -142,39 +137,3 @@ class BashKernel(Kernel): 'status': 'ok'} -def display_data(filename): - with open(filename, 'rb') as f: - image = f.read() - unlink(filename) - - image_type = imghdr.what(None, image) - if image_type is None: - raise ValueError("Not a valid image: %s" % image) - - image_data = urllib.parse.quote(base64.b64encode(image)) - content = { - 'source': 'kernel', - 'data': { - 'image/' + image_type: image_data - } - } - return content - - -def extract_image_filenames(output): - output_lines = [] - image_filenames = [] - - for line in output.split("\n"): - if line.startswith(_TEXT_SAVED_IMAGE): - filename = line.rstrip().split(": ")[-1] - image_filenames.append(filename) - else: - output_lines.append(line) - - output = "\n".join(output_lines) - return image_filenames, output - -if __name__ == '__main__': - from IPython.kernel.zmq.kernelapp import IPKernelApp - IPKernelApp.launch_instance(kernel_class=BashKernel) diff --git a/setup.py b/setup.py index 62db62b..4e2a444 100644 --- a/setup.py +++ b/setup.py @@ -43,7 +43,7 @@ setup(name='bash_kernel', author='Thomas Kluyver', author_email='thomas@kluyver.me.uk', url='https://github.com/takluyver/bash_kernel', - py_modules=['bash_kernel'], + packages=['bash_kernel'], cmdclass={'install': install_with_kernelspec}, install_requires=['pexpect>=3.3'], classifiers = [