version 1.1, 2019/05/27 07:07:43 |
version 1.3, 2019/05/27 23:30:27 |
|
|
# coding: utf-8 |
# coding: utf-8 |
# $OpenXM$ |
# $OpenXM: OpenXM/src/jupyter/kernel.py,v 1.2 2019/05/27 07:15:46 takayama Exp $ |
from __future__ import print_function |
from __future__ import print_function |
|
|
import codecs |
import codecs |
Line 49 def get_kernel_json(): |
|
Line 49 def get_kernel_json(): |
|
return data |
return data |
|
|
|
|
class OctaveKernel(ProcessMetaKernel): |
class AsirKernel(ProcessMetaKernel): |
implementation = 'Asir Kernel' |
implementation = 'Asir Kernel' |
implementation_version = __version__, |
implementation_version = __version__, |
language = 'asir' |
language = 'asir' |
help_links = HELP_LINKS |
help_links = HELP_LINKS |
kernel_json = get_kernel_json() |
kernel_json = get_kernel_json() |
|
|
_octave_engine = None |
_asir_engine = None |
_language_version = None |
_language_version = None |
|
|
@property |
@property |
def language_version(self): |
def language_version(self): |
if self._language_version: |
if self._language_version: |
return self._language_version |
return self._language_version |
ver = self.octave_engine.eval('version', silent=True) |
ver = self.asir_engine.eval('version', silent=True) |
ver = self._language_version = ver.split()[-1] |
ver = self._language_version = ver.split()[-1] |
return ver |
return ver |
|
|
Line 81 class OctaveKernel(ProcessMetaKernel): |
|
Line 81 class OctaveKernel(ProcessMetaKernel): |
|
return msg % (__version__, self.language_version) |
return msg % (__version__, self.language_version) |
|
|
@property |
@property |
def octave_engine(self): |
def asir_engine(self): |
if self._octave_engine: |
if self._asir_engine: |
return self._octave_engine |
return self._asir_engine |
self._octave_engine = OctaveEngine(plot_settings=self.plot_settings, |
self._asir_engine = AsirEngine(plot_settings=self.plot_settings, |
error_handler=self.Error, |
error_handler=self.Error, |
stdin_handler=self.raw_input, |
stdin_handler=self.raw_input, |
stream_handler=self.Print, |
stream_handler=self.Print, |
logger=self.log) |
logger=self.log) |
return self._octave_engine |
return self._asir_engine |
|
|
def makeWrapper(self): |
def makeWrapper(self): |
"""Start an Octave process and return a :class:`REPLWrapper` object. |
"""Start an Asir process and return a :class:`REPLWrapper` object. |
""" |
""" |
return self.octave_engine.repl |
return self.asir_engine.repl |
|
|
def do_execute_direct(self, code, silent=False): |
def do_execute_direct(self, code, silent=False): |
if code.strip() in ['quit', 'quit()', 'exit', 'exit()']: |
if code.strip() in ['quit', 'quit()', 'exit', 'exit()']: |
self._octave_engine = None |
self._asir_engine = None |
self.do_shutdown(True) |
self.do_shutdown(True) |
return |
return |
# f = open('tmptmp.txt','a');f.write(str(code));f.close() ##### |
# f = open('tmptmp.txt','a');f.write(str(code));f.close() ##### |
# self._octave_engine.logger.debug(str(code)) ##### |
# self._asir_engine.logger.debug(str(code)) ##### |
val = ProcessMetaKernel.do_execute_direct(self, code, silent=silent) |
val = ProcessMetaKernel.do_execute_direct(self, code+';;', silent=silent) |
if not silent: |
if not silent: |
try: |
try: |
plot_dir = self.octave_engine.make_figures() |
plot_dir = self.asir_engine.make_figures() |
except Exception as e: |
except Exception as e: |
self.Error(e) |
self.Error(e) |
return val |
return val |
if plot_dir: |
if plot_dir: |
for image in self.octave_engine.extract_figures(plot_dir, True): |
for image in self.asir_engine.extract_figures(plot_dir, True): |
self.Display(image) |
self.Display(image) |
return val |
return val |
|
|
Line 122 class OctaveKernel(ProcessMetaKernel): |
|
Line 122 class OctaveKernel(ProcessMetaKernel): |
|
return None |
return None |
else: |
else: |
return "" |
return "" |
return self.octave_engine.eval('help %s' % obj, silent=True) |
return self.asir_engine.eval('help %s' % obj, silent=True) |
|
|
def Print(self, *args, **kwargs): |
def Print(self, *args, **kwargs): |
# Ignore standalone input hook displays. |
# Ignore standalone input hook displays. |
Line 133 class OctaveKernel(ProcessMetaKernel): |
|
Line 133 class OctaveKernel(ProcessMetaKernel): |
|
if arg.strip().startswith(STDIN_PROMPT): |
if arg.strip().startswith(STDIN_PROMPT): |
arg = arg.replace(STDIN_PROMPT, '') |
arg = arg.replace(STDIN_PROMPT, '') |
out.append(arg) |
out.append(arg) |
super(OctaveKernel, self).Print(*out, **kwargs) |
super(AsirKernel, self).Print(*out, **kwargs) |
|
|
def raw_input(self, text): |
def raw_input(self, text): |
# Remove the stdin prompt to restore the original prompt. |
# Remove the stdin prompt to restore the original prompt. |
text = text.replace(STDIN_PROMPT, '') |
text = text.replace(STDIN_PROMPT, '') |
return super(OctaveKernel, self).raw_input(text) |
return super(AsirKernel, self).raw_input(text) |
|
|
def get_completions(self, info): |
def get_completions(self, info): |
""" |
""" |
Get completions from kernel based on info dict. |
Get completions from kernel based on info dict. |
""" |
""" |
cmd = 'completion_matches("%s")' % info['obj'] |
cmd = 'completion_matches("%s")' % info['obj'] |
val = self.octave_engine.eval(cmd, silent=True) |
val = self.asir_engine.eval(cmd, silent=True) |
return val and val.splitlines() or [] |
return val and val.splitlines() or [] |
|
|
def handle_plot_settings(self): |
def handle_plot_settings(self): |
"""Handle the current plot settings""" |
"""Handle the current plot settings""" |
self.octave_engine.plot_settings = self.plot_settings |
self.asir_engine.plot_settings = self.plot_settings |
|
|
|
|
class OctaveEngine(object): |
class AsirEngine(object): |
|
|
def __init__(self, error_handler=None, stream_handler=None, |
def __init__(self, error_handler=None, stream_handler=None, |
stdin_handler=None, plot_settings=None, |
stdin_handler=None, plot_settings=None, |
Line 355 class OctaveEngine(object): |
|
Line 355 class OctaveEngine(object): |
|
if 'version 4' in version: |
if 'version 4' in version: |
cmd += ' --no-gui' |
cmd += ' --no-gui' |
# Interactive mode prevents crashing on Windows on syntax errors. |
# Interactive mode prevents crashing on Windows on syntax errors. |
# Delay sourcing the "~/.octaverc" file in case it displays a pager. |
# Delay sourcing the "~/.asirrc" file in case it displays a pager. |
cmd += ' --interactive --quiet --no-init-file ' |
cmd += ' --interactive --quiet --no-init-file ' |
|
|
# Add cli options provided by the user. |
# Add cli options provided by the user. |
cmd += os.environ.get('OCTAVE_CLI_OPTIONS', '') |
cmd += os.environ.get('ASIR_CLI_OPTIONS', '') |
|
|
orig_prompt = u('PEXPECT_PROMPT>') |
orig_prompt = u('PEXPECT_PROMPT>') |
change_prompt = u("base_prompt('{0}')") |
change_prompt = u("base_prompt('{0}')") |
Line 375 class OctaveEngine(object): |
|
Line 375 class OctaveEngine(object): |
|
|
|
def _interrupt(self, silent=False): |
def _interrupt(self, silent=False): |
if (os.name == 'nt'): |
if (os.name == 'nt'): |
msg = '** Warning: Cannot interrupt Octave on Windows' |
msg = '** Warning: Cannot interrupt Asir on Windows' |
if self.stream_handler: |
if self.stream_handler: |
self.stream_handler(msg) |
self.stream_handler(msg) |
elif self.logger: |
elif self.logger: |
Line 419 class OctaveEngine(object): |
|
Line 419 class OctaveEngine(object): |
|
return '\n'.join(lines) |
return '\n'.join(lines) |
|
|
def _get_executable(self): |
def _get_executable(self): |
"""Find the best octave executable. |
"""Find the best asir executable. |
""" |
""" |
executable = os.environ.get('ASIR_EXECUTABLE', None) |
executable = os.environ.get('ASIR_EXECUTABLE', None) |
if not executable or not which(executable): |
if not executable or not which(executable): |