93 lines
2.7 KiB
Plaintext
93 lines
2.7 KiB
Plaintext
|
from __future__ import print_function
|
||
|
|
||
|
from metakernel import MetaKernel
|
||
|
|
||
|
from metakernel import register_ipython_magics
|
||
|
register_ipython_magics()
|
||
|
|
||
|
class MetaKernelyap(MetaKernel):
|
||
|
implementation = 'MetaKernel YAP'
|
||
|
implementation_version = '1.0'
|
||
|
language = 'text'
|
||
|
language_version = '0.1'
|
||
|
banner = "MetaKernel YAP"
|
||
|
language_info = {
|
||
|
'mimetype': 'text/plain',
|
||
|
'name': 'text',
|
||
|
# ------ If different from 'language':
|
||
|
'codemirror_mode': {
|
||
|
"version": 2,
|
||
|
"name": "prolog"
|
||
|
}
|
||
|
'pygments_lexer': 'language',
|
||
|
'version' : "0.0.1",
|
||
|
'file_extension': '.yap',
|
||
|
'help_links': MetaKernel.help_links,
|
||
|
}
|
||
|
|
||
|
def __init__(self, **kwargs):
|
||
|
|
||
|
MetaKernel.__init__(self, **kwargs)
|
||
|
self._start_yap()
|
||
|
self.qq = None sq
|
||
|
|
||
|
def _start_yap(self):
|
||
|
# Signal handlers are inherited by forked processes, and we can't easily
|
||
|
# reset it from the subprocess. Since kernelapp ignores SIGINT except in
|
||
|
# message handlers, we need to temporarily reset the SIGINT handler here
|
||
|
# so that yap and its children are interruptible.
|
||
|
sig = signal.signal(signal.SIGINT, signal.SIG_DFL)
|
||
|
try:
|
||
|
engine = yap.YAPEngine()
|
||
|
engine.query("load_files(library(python), [])").command()
|
||
|
banner = "YAP {0} Kernel".format(self.engine.version())
|
||
|
|
||
|
finally:
|
||
|
signal.signal(signal.SIGINT, sig)
|
||
|
|
||
|
# Register Yap function to write image data to temporary file
|
||
|
#self.yapwrapper.run_command(image_setup_cmd)
|
||
|
|
||
|
def get_usage(self):
|
||
|
return "This is the YAP kernel."
|
||
|
|
||
|
def do_execute_direct(self, code):
|
||
|
if not code.strip():
|
||
|
return {'status': 'ok', 'execution_count': self.execution_count,
|
||
|
'payload': [], 'user_expressions': {}}
|
||
|
|
||
|
interrupted = False
|
||
|
try:
|
||
|
print self.q
|
||
|
if self.q is None:
|
||
|
self.q = self.engine.query(code.rstrip())
|
||
|
if self.q.next():
|
||
|
vs = self.q.namedVars()
|
||
|
if vs.length() > 0:
|
||
|
l = []
|
||
|
while vs.length() > 0:
|
||
|
eq = vs.car()
|
||
|
l.append(' '.join([getArg(1).text(), '=', eq.getArg(2).text())
|
||
|
vs = vs.cdr()
|
||
|
l.append(';')
|
||
|
o = '\n'.join(l)
|
||
|
else:
|
||
|
return 'yes'
|
||
|
self.q = None
|
||
|
|
||
|
else:
|
||
|
return 'no'
|
||
|
self.q = None
|
||
|
|
||
|
|
||
|
|
||
|
def repr(self, data):
|
||
|
return repr(data)
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
try:
|
||
|
from ipykernel.kernelapp import IPKernelApp
|
||
|
except ImportError:
|
||
|
from IPython.kernel.zmq.kernelapp import IPKernelApp
|
||
|
IPKernelApp.launch_instance(kernel_class=MetaKernelyap)
|