Skip to content

Commit

Permalink
Added ipython kernel. Fixed kernel inheritance
Browse files Browse the repository at this point in the history
  • Loading branch information
Derek Ruths committed Mar 16, 2017
1 parent 2d11262 commit c2106fe
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* Added support for a configuration file
* Added support for pluggable kernels

* Added an ipython kernel

### Changed

* Modularized the handling of code block implementations.
Expand Down
1 change: 1 addition & 0 deletions xp/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def configure_parser(config_parser):
xp.kernels.gnuplot.GNUPlotKernel
xp.kernels.awk.AwkKernel
xp.kernels.python.PythonKernel
xp.kernels.ipython.IPythonKernel
xp.kernels.pyhmr.PythonHadoopMapReduceKernel""")

return
Expand Down
2 changes: 1 addition & 1 deletion xp/kernels/awk.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

logger = logging.getLogger(os.path.basename(__file__))

class AwkKernel:
class AwkKernel(Kernel):

@staticmethod
def default_lang_suffix():
Expand Down
2 changes: 1 addition & 1 deletion xp/kernels/gnuplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

logger = logging.getLogger(os.path.basename(__file__))

class GNUPlotKernel:
class GNUPlotKernel(Kernel):

@staticmethod
def default_lang_suffix():
Expand Down
75 changes: 75 additions & 0 deletions xp/kernels/ipython.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
"""
Copyright 2016 Derek Ruths
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""

import subprocess
from subprocess import CalledProcessError
import logging
import os.path
import tempfile

from xp.kernels.base import Kernel, get_total_context

logger = logging.getLogger(os.path.basename(__file__))

class IPythonKernel(Kernel):

@staticmethod
def default_lang_suffix():
return 'ipy'

@staticmethod
def short_help():
"""
Return a short description of the kernel.
"""
return 'run ipython code'

@staticmethod
def long_help():
"""
Return a detailed description of the kernel, how it works, how it is configured, and used.
"""
return 'Run the commands in whatever the default ipython VM is on the host system.'

@staticmethod
def env_vars_help():
"""
Return a dictionary of environment variables (keys) and their meaning (values).
"""
return {
'IPYTHON_CMD': 'the python executable that will be run to execute the block code'
}

def run(self,arg_str,context,cwd,content):
"""
Raises a CalledProcessError if this fails.
"""

# write python code to a tmp file
fh,tmp_filename = tempfile.mkstemp(suffix='py')
os.write(fh,'\n'.join(content))
os.close(fh)

logger.debug('wrote ipython content to %s' % tmp_filename)

exec_name = context.get('IPYTHON_CMD','ipython')
cmd = '%s %s %s' % (exec_name,arg_str,tmp_filename)
logger.debug('using cmd: %s' % cmd)
retcode = subprocess.call(cmd,shell=True,cwd=cwd,env=get_total_context(context))

if retcode != 0:
raise CalledProcessError(retcode,cmd,None)

2 changes: 1 addition & 1 deletion xp/kernels/pyhmr.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

logger = logging.getLogger(os.path.basename(__file__))

class PythonHadoopMapReduceKernel:
class PythonHadoopMapReduceKernel(Kernel):

@staticmethod
def default_lang_suffix():
Expand Down
6 changes: 3 additions & 3 deletions xp/kernels/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

logger = logging.getLogger(os.path.basename(__file__))

class PythonKernel:
class PythonKernel(Kernel):

@staticmethod
def default_lang_suffix():
Expand All @@ -50,7 +50,7 @@ def env_vars_help():
Return a dictionary of environment variables (keys) and their meaning (values).
"""
return {
'PYTHON': 'the python executable that will be run to execute the block code'
'PYTHON_CMD': 'the python executable that will be run to execute the block code'
}

def run(self,arg_str,context,cwd,content):
Expand All @@ -65,7 +65,7 @@ def run(self,arg_str,context,cwd,content):

logger.debug('wrote python content to %s' % tmp_filename)

exec_name = context.get('PYTHON','python')
exec_name = context.get('PYTHON_CMD','python')
cmd = '%s %s %s' % (exec_name,arg_str,tmp_filename)
logger.debug('using cmd: %s' % cmd)
retcode = subprocess.call(cmd,shell=True,cwd=cwd,env=get_total_context(context))
Expand Down
2 changes: 1 addition & 1 deletion xp/kernels/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

logger = logging.getLogger(os.path.basename(__file__))

class ShellKernel:
class ShellKernel(Kernel):

@staticmethod
def default_lang_suffix():
Expand Down

0 comments on commit c2106fe

Please sign in to comment.