improvements

This commit is contained in:
Vitor Santos Costa 2016-08-30 12:20:31 -05:00
parent 1e118bee6d
commit e4c82910a2

View File

@ -5,14 +5,14 @@ from ipykernel.ipkernel import IPythonKernel
import sys import sys
import signal import signal
import yap import yap
#import ipdb # import ipdb
#ipdb,set_trace() # ipdb,set_trace()
kernel_json = { kernel_json = {
"argv": [sys.executable, "argv": [sys.executable,
"-m", "yap_kernel", "-m", "yap_kernel",
"-f", "{connection_file}"], "-f", "{connection_file}"],
"display_name": " YAP-6.3" , "display_name": " YAP-6.3",
"language": "prolog", "language": "prolog",
"name": "yap_kernel", "name": "yap_kernel",
} }
@ -21,12 +21,13 @@ kernel_json = {
def eprint(*args, **kwargs): def eprint(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs) print(*args, file=sys.stderr, **kwargs)
class YAPKernel(IPythonKernel): class YAPKernel(IPythonKernel):
implementation = 'MetaKernel YAP' implementation = 'MetaKernel YAP'
implementation_version = '1.0' implementation_version = '1.0'
language = 'text' language = 'text'
language_version = '0.1' language_version = '0.1'
banner = "YAP-6.3" banner = "YAP-6.3"
language_info = { language_info = {
'mimetype': 'text/prolog', 'mimetype': 'text/prolog',
'name': 'text', 'name': 'text',
@ -36,15 +37,19 @@ class YAPKernel(IPythonKernel):
"name": "prolog" "name": "prolog"
}, },
'pygments_lexer': 'prolog', 'pygments_lexer': 'prolog',
'version' : "0.0.1", 'version': "0.0.1",
'file_extension': '.yap', 'file_extension': '.yap',
} }
def init_yap(self, **kwargs): def init_yap(self, **kwargs):
# Signal handlers are inherited by forked processes, and we can't easily # Signal handlers are inherited by
# reset it from the subprocess. Since kernelapp ignores SIGINT except in # forked processes,
# message handlers, we need to temporarily reset the SIGINT handler here # and we can't easily
# so that yap and its children are interruptible. # 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) sig = signal.signal(signal.SIGINT, signal.SIG_DFL)
try: try:
self.engine = yap.YAPEngine() self.engine = yap.YAPEngine()
@ -54,24 +59,23 @@ class YAPKernel(IPythonKernel):
banner = "YAP6-3 Kernel" banner = "YAP6-3 Kernel"
self.olines = banner self.olines = banner
finally: finally:
signal.signal(signal.SIGINT, sig) signal.signal(signal.SIGINT, sig)
def __init__(self, **kwargs): def __init__(self, **kwargs):
super(YAPKernel, self).__init__( **kwargs) super(YAPKernel, self).__init__(**kwargs)
self.init_yap( **kwargs ) self.init_yap(**kwargs)
self.shell.run_cell = self.yap_run_cell self.shell.run_cell = self.yap_run_cell
def get_usage(self): def get_usage(self):
return "This is the YAP kernel." return "This is the YAP kernel."
def yap_run_cell(self, s, store_history=False, silent=False,
def yap_run_cell(self, s, store_history=False, silent=False, shell_futures=True): shell_futures=True):
if not self.q: if not self.q:
self.q = self.engine.query(s) self.q = self.engine.query(s)
if self.q.next(): if self.q.next():
myvs = self.q.namedVarsCopy() myvs = self.q.namedVarsCopy()
wrote = False
if myvs: if myvs:
i = 0 i = 0
for peq in myvs: for peq in myvs:
@ -84,7 +88,6 @@ class YAPKernel(IPythonKernel):
else: else:
i = bind.numberVars(i, True) i = bind.numberVars(i, True)
print(name.text() + " = " + bind.text()) print(name.text() + " = " + bind.text())
wrote = True
print("yes") print("yes")
if self.q.deterministic(): if self.q.deterministic():
self.closeq() self.closeq()
@ -93,7 +96,7 @@ class YAPKernel(IPythonKernel):
self.closeq() self.closeq()
return return
def closeq( self): def closeq(self):
if self.q: if self.q:
self.q.close() self.q.close()
self.q = None self.q = None