Initial commit (made in the IPython Qtconsole)
commit
cb0df82ba8
@ -0,0 +1,64 @@
|
||||
from IPython.kernel.zmq.kernelbase import Kernel
|
||||
from pexpect import replwrap
|
||||
from subprocess import check_output
|
||||
import re
|
||||
|
||||
version_pat = re.compile(r'version (\d+(\.\d+)+)')
|
||||
|
||||
class BashKernel(Kernel):
|
||||
implementation = 'bash_kernel'
|
||||
implementation_version = '1.0'
|
||||
language = 'bash'
|
||||
@property
|
||||
def language_version(self):
|
||||
m = version_pat.search(self.banner)
|
||||
return m.group(1)
|
||||
|
||||
_banner = None
|
||||
@property
|
||||
def banner(self):
|
||||
if self._banner is None:
|
||||
self._banner = check_output(['bash', '--version']).decode('utf-8')
|
||||
return self._banner
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
Kernel.__init__(self, **kwargs)
|
||||
self.bashwrapper = replwrap.bash()
|
||||
|
||||
def do_execute(self, code, silent, store_history=True, user_expressions=None,
|
||||
allow_stdin=False):
|
||||
if not code.strip():
|
||||
return {'status': 'ok', 'execution_count': self.execution_count,
|
||||
'payloads': [], 'user_expressions': {}}
|
||||
|
||||
interrupted = False
|
||||
try:
|
||||
output = self.bashwrapper.run_command(code.rstrip(), timeout=None)
|
||||
except KeyboardInterrupt:
|
||||
self.bashwrapper.child.sendintr()
|
||||
interrupted = True
|
||||
self.bashwrapper._expect_prompt()
|
||||
output = self.bashwrapper.child.before
|
||||
|
||||
if not silent:
|
||||
stream_content = {'name': 'stdout', 'data':output}
|
||||
self.send_response(self.iopub_socket, 'stream', stream_content)
|
||||
|
||||
if interrupted:
|
||||
return {'status': 'abort', 'execution_count': self.execution_count}
|
||||
|
||||
try:
|
||||
exitcode = int(self.run_command('echo $?').rstrip())
|
||||
except Exception:
|
||||
exitcode = 1
|
||||
|
||||
if exitcode:
|
||||
return {'status': 'error', 'execution_count': self.execution_count,
|
||||
'ename': '', 'evalue': str(exitcode), 'traceback': []}
|
||||
else:
|
||||
return {'status': 'ok', 'execution_count': self.execution_count,
|
||||
'payloads': [], 'user_expressions': {}}
|
||||
|
||||
if __name__ == '__main__':
|
||||
from IPython.kernel.zmq.kernelapp import IPKernelApp
|
||||
IPKernelApp.launch_instance(kernel_class=BashKernel)
|
@ -0,0 +1,4 @@
|
||||
{"argv":["python3","-m","bash_kernel", "-f", "{connection_file}"],
|
||||
"display_name":"Bash",
|
||||
"language":"bash"
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
from distutils.core import setup
|
||||
from distutils.command.install import install
|
||||
|
||||
class install_with_kernelspec(install):
|
||||
def run(self):
|
||||
install.run(self)
|
||||
from IPython.kernel.kernelspec import install_kernel_spec
|
||||
install_kernel_spec('kernelspec', 'bash', replace=True)
|
||||
|
||||
setup(name='bash_kernel',
|
||||
version='0.1',
|
||||
description='A bash kernel for IPython',
|
||||
author='Thomas Kluyver',
|
||||
author_email='thomas@kluyver.me.uk',
|
||||
py_modules=['bash_kernel'],
|
||||
cmdclass={'install': install_with_kernelspec}
|
||||
)
|
Loading…
Reference in New Issue