-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathkernel.py
More file actions
683 lines (582 loc) · 23.6 KB
/
kernel.py
File metadata and controls
683 lines (582 loc) · 23.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
"""Definition of KernelConnection class.
KernelConnection class provides interaction with Jupyter kernels.
Copyright (c) 2017-2018, NEGORO Tetsuya (https://github.com/ngr-t)
"""
import re
from collections import defaultdict
from datetime import datetime
from queue import Empty, Queue
from threading import Event, RLock, Thread
import sublime
from .utils import get_cell, get_png_dimensions, show_password_input
JUPYTER_PROTOCOL_VERSION = "5.0"
REPLY_STATUS_OK = "ok"
REPLY_STATUS_ERROR = "error"
REPLY_STATUS_ABORT = "abort"
MSG_TYPE_EXECUTE_INPUT = "execute_input"
MSG_TYPE_EXECUTE_REQUEST = "execute_request"
MSG_TYPE_EXECUTE_RESULT = "execute_result"
MSG_TYPE_EXECUTE_REPLY = "execute_reply"
MSG_TYPE_COMPLETE_REQUEST = "complete_request"
MSG_TYPE_COMPLETE_REPLY = "complete_reply"
MSG_TYPE_DISPLAY_DATA = "display_data"
MSG_TYPE_INSPECT_REQUEST = "inspect_request"
MSG_TYPE_INSPECT_REPLY = "inspect_reply"
MSG_TYPE_INPUT_REQUEST = "input_request"
MSG_TYPE_INPUT_REPLY = "input_reply"
MSG_TYPE_ERROR = "error"
MSG_TYPE_STREAM = "stream"
MSG_TYPE_STATUS = "status"
HELIUM_FIGURE_PHANTOMS = "helium_figure_phantoms"
MAX_PHANTOMS = 65536
# Used as key of status bar.
KERNEL_STATUS_KEY = "helium_kernel_status"
HELIUM_OBJECT_INSPECT_PANEL = "helium_object_inspect"
ANSI_ESCAPE_PATTERN = re.compile(r"\x1b[^m]*m")
DOLLAR_REGEX = re.compile(r"(?<!\\)\$")
OUTPUT_VIEW_SEPARATOR = "-" * 80
TEXT_PHANTOM = """<body id="helium-result">
<style>
.stdout {{ color: color(var(--foreground) alpha(0.7)) }}
.error {{ color: var(--redish) }}
.other {{ color: var(--yellowish) }}
.closebutton {{ text-decoration: none }}
</style>
<a class=closebutton href=hide>×</a>
{content}
</body>"""
IMAGE_PHANTOM = """<body id="helium-image-result" style="background-color:none">
<style>
.image {{ background-color: none }}
.closebutton {{ text-decoration: none }}
</style>
<a class=closebutton href=hide>×</a>
<br>
<img class="image" alt="Out" style="width: {width}; height: {height}" src="data:image/png;base64,{data}" />
</body>"""
STREAM_PHANTOM = "<div class={name}>{content}</div>"
def fix_whitespace_for_phantom(text: str):
"""Transform output for proper display.
This is important to display pandas DataFrames, for instance.
"""
text = text.replace(" ", r" ")
text = "<br>".join(text.splitlines())
return text
def extract_content(messages, msg_type):
"""Extract content from messages received from a kernel."""
return [
message["content"]
for message in messages
if message["header"]["msg_type"] == msg_type
]
def remove_ansi_escape(text: str):
return ANSI_ESCAPE_PATTERN.sub("", text)
# Dollar sign have meanings in ST completions but not in Jupyter
# We must therefore escape them
def escape_dollar_signs(string):
"""Escape any dollar sign present in string"""
return DOLLAR_REGEX.sub(r'\\$', string)
def get_msg_type(message):
return message["header"]["msg_type"]
def extract_data(result):
"""Extract plain text data."""
try:
return result["data"]
except KeyError:
return ""
class KernelConnection(object):
"""Interact with a Jupyter kernel."""
class MessageReceiver(Thread): # noqa
def __init__(self, kernel):
"""Initialize AsyncCommunicator class."""
super().__init__()
self._kernel = kernel
self.exit = Event()
def shutdown(self):
self.exit.set()
class ShellMessageReceiver(MessageReceiver):
"""Communicator that runs asynchroniously."""
def run(self):
"""Run main routine."""
# TODO: implement logging
# TODO: remove view and regions from id2region
while not self.exit.is_set():
try:
msg = self._kernel.client.get_shell_msg(timeout=1)
self._kernel.shell_msg_queues_lock.acquire()
try:
queue = self._kernel.shell_msg_queues[
msg["parent_header"]["msg_id"]
]
finally:
self._kernel.shell_msg_queues_lock.release()
queue.put(msg)
except Empty:
pass
except Exception as ex:
self._kernel._logger.exception(ex)
class IOPubMessageReceiver(MessageReceiver):
"""Receive and process IOPub messages."""
def run(self):
"""Run main routine."""
# TODO: log, handle other message types.
while not self.exit.is_set():
try:
msg = self._kernel.client.get_iopub_msg(timeout=1)
self._kernel._logger.info(msg)
content = msg.get("content", {})
execution_count = content.get("execution_count", None)
msg_type = msg["msg_type"]
view, region = self._kernel.id2region.get(
msg["parent_header"].get("msg_id", None), (None, None)
)
if msg_type == MSG_TYPE_STATUS:
self._kernel._execution_state = content["execution_state"]
elif msg_type == MSG_TYPE_EXECUTE_INPUT:
# if code is executed deleted all phantoms in this region
self._kernel._clear_phantoms_in_region(region, view)
self._kernel._write_text_to_view("\n\n")
if sublime.load_settings("Helium.sublime-settings").get(
"output_code"
):
self._kernel._output_input_code(
content["code"], content["execution_count"]
)
elif msg_type == MSG_TYPE_ERROR:
self._kernel._logger.info("Handling error")
self._kernel._handle_error(
content["ename"],
content["evalue"],
content["traceback"],
region,
view,
)
elif msg_type == MSG_TYPE_DISPLAY_DATA:
self._kernel._write_mime_data_to_view(
content["data"], region, view
)
elif msg_type == MSG_TYPE_EXECUTE_RESULT:
self._kernel._write_mime_data_to_view(
content["data"], region, view
)
elif msg_type == MSG_TYPE_STREAM:
self._kernel._handle_stream(
content["name"],
content["text"],
region,
view,
)
except Empty:
pass
except Exception as ex:
self._kernel._logger.exception(ex)
class StdInMessageReceiver(MessageReceiver):
"""Receive and process IOPub messages."""
def _handle_input_request(self, prompt, password):
def interrupt():
self._kernel.interrupt_kernel(self.kernel_id)
if password:
show_password_input(prompt, self._kernel.input, interrupt)
else:
(
sublime.active_window().show_input_panel(
prompt, "", self._kernel.client.input, lambda x: None, interrupt
)
)
def run(self):
"""Run main routine."""
# TODO: log, handle other message types.
while not self.exit.is_set():
try:
msg = self._kernel.client.get_stdin_msg(timeout=1)
msg_type = msg["msg_type"]
content = msg["content"]
if msg_type == MSG_TYPE_INPUT_REQUEST:
self._handle_input_request(
content["prompt"], content["password"]
)
except Empty:
pass
except Exception as ex:
self._kernel._logger.exception(ex)
def _init_receivers(self):
# Set the attributes refered by receivers before they start.
self._shell_msg_receiver = self.ShellMessageReceiver(self)
self._shell_msg_receiver.start()
self._iopub_msg_receiver = self.IOPubMessageReceiver(self)
self._iopub_msg_receiver.start()
self._stdin_msg_receiver = self.StdInMessageReceiver(self)
self._stdin_msg_receiver.start()
def __init__(
self,
kernel_id,
kernel_manager,
parent,
connection_name=None,
logger=None,
):
"""Initialize KernelConnection class.
parameters
---------
kernel_id str: kernel ID
parent parent kernel manager
"""
self._logger = logger
self.shell_msg_queues = defaultdict(Queue)
self._kernel_id = kernel_id
self.parent = parent
self.kernel_manager = kernel_manager
self.client = self.kernel_manager.client()
self.client.start_channels()
self.shell_msg_queues_lock = RLock()
self.id2region = {}
self._connection_name = connection_name
self._execution_state = "unknown"
self._init_receivers()
self.phantoms = {}
def __del__(self): # noqa
self._shell_msg_receiver.shutdown()
self._iopub_msg_receiver.shutdown()
self._stdin_msg_receiver.shutdown()
@property
def lang(self):
"""Language of kernel."""
return self.kernel_manager.kernel_name
@property
def kernel_id(self):
"""ID of kernel."""
return self._kernel_id
def shutdown_kernel(self):
self.kernel_manager.shutdown_kernel()
def restart_kernel(self):
self.kernel_manager.restart_kernel()
def interrupt_kernel(self):
self.kernel_manager.interrupt_kernel()
def get_connection_name(self):
return self._connection_name
def set_connection_name(self, new_name):
# We also have to change the view name now.
view = self.get_view()
self._connection_name = new_name
view.set_name(self.view_name)
def del_connection_name(self):
self._connection_name = None
connection_name = property(
get_connection_name,
set_connection_name,
del_connection_name,
"Name of kernel connection shown in a view title.",
)
@property
def view_name(self):
"""Return name of output view."""
return "*Helium Output* {repr}".format(repr=self.repr)
@property
def repr(self):
"""Return string representation of the connection."""
if self.connection_name:
return "{connection_name} ([{lang}] {kernel_id})".format(
connection_name=self.connection_name,
lang=self.lang,
kernel_id=self.kernel_id,
)
else:
return "[{lang}] {kernel_id}".format(
lang=self.lang, kernel_id=self.kernel_id
)
@property
def execution_state(self):
return self._execution_state
@property
def _show_inline_output(self):
return sublime.load_settings("Helium.sublime-settings").get("inline_output")
def activate_view(self):
"""Activate view to show the output of kernel."""
view = self.get_view()
current_view = sublime.active_window().active_view()
sublime.active_window().focus_view(view)
view.set_scratch(True) # avoids prompting to save
view.settings().set("word_wrap", "false")
sublime.active_window().focus_view(current_view)
def _output_input_code(self, code, execution_count):
line = "In[{execution_count}]: {code}".format(
execution_count=execution_count, code=code
)
self._write_text_to_view(line)
def _handle_error(
self,
ename,
evalue,
traceback,
region: sublime.Region = None,
view: sublime.View = None,
) -> None:
try:
lines = """\nError: {ename}, {evalue}.
\nTraceback:\n{traceback}""".format(
ename=ename,
evalue=evalue,
traceback="\n".join(traceback),
)
lines = remove_ansi_escape(lines)
self._write_text_to_view(lines)
if region is not None:
phantom_html = STREAM_PHANTOM.format(
name="error", content=fix_whitespace_for_phantom(lines)
)
self._write_inline_html_phantom(phantom_html, region, view)
except AttributeError:
# Just there is no error.
pass
def _handle_stream(
self, name, text, region: sublime.Region = None, view: sublime.View = None
) -> None:
# Currently don't consider real time catching of streams.
try:
lines = "\n({name}):\n{text}".format(name=name, text=text)
phantom_html = STREAM_PHANTOM.format(
name=name, content=fix_whitespace_for_phantom(text)
)
self._write_text_to_view(lines)
if phantom_html and (region is not None):
self._write_inline_html_phantom(phantom_html, region, view)
except AttributeError:
# Just there is no error.
pass
def _write_out_execution_count(self, execution_count) -> None:
self._write_text_to_view("\nOut[{}]: \n".format(execution_count))
def _write_text_to_view(self, text: str) -> None:
if self._show_inline_output:
return
self.activate_view()
view = self.get_view()
view.set_read_only(False)
view.run_command("append", {"characters": text})
view.set_read_only(True)
view.show(view.size())
def _write_phantom(self, content: str):
if self._show_inline_output:
return
self.activate_view()
file_size = self.get_view().size()
region = sublime.Region(file_size, file_size)
self.get_view().add_phantom(
HELIUM_FIGURE_PHANTOMS, region, content, sublime.LAYOUT_BLOCK
)
self._logger.info("Created phantom {}".format(content))
def _write_inline_html_phantom(
self, content: str, region: sublime.Region, view: sublime.View
):
if self._show_inline_output:
id = HELIUM_FIGURE_PHANTOMS + datetime.now().isoformat()
html = TEXT_PHANTOM.format(content=content)
self._add_phantom(view, id, region, html)
def _write_inline_image_phantom(
self, data: str, region: sublime.Region, view: sublime.View
):
if self._show_inline_output:
id = HELIUM_FIGURE_PHANTOMS + datetime.now().isoformat()
img_size = sublime.load_settings("Helium.sublime-settings").get(
"image_size", "optimal"
)
width = view.viewport_extent()[0] - 2
dimensions = get_png_dimensions(data)
if img_size == "original" or (
img_size == "optimal" and (dimensions[0] < width)
):
html = IMAGE_PHANTOM.format(
data=data, width=dimensions[0], height=dimensions[1]
)
else:
scale_factor = width / dimensions[0]
height = dimensions[1] * scale_factor
html = IMAGE_PHANTOM.format(data=data, width=width, height=height)
self._add_phantom(view, id, region, html)
def _add_phantom(
self, view: sublime.View, id: str, region: sublime.Region, html: str
):
int_id = view.add_phantom(
id,
region,
html,
sublime.LAYOUT_BLOCK,
on_navigate=lambda href, id=id, view=view: self._erase_phantom(
id, view=view
),
)
self.phantoms[id] = int_id
if int_id > MAX_PHANTOMS:
sublime.message_dialog(
"Please close and reopen your current tab. "
+ "Otherwise the 'Clear All Cells' command might not work as intended."
)
self._logger.info("Created inline phantom image")
def _clear_phantoms_in_region(self, region: sublime.Region, view: sublime.View):
_, cell = get_cell(view, region, logger="")
remove = [
pid
for pid, int_id in self.phantoms.items()
if cell.contains(view.query_phantom(int_id)[0])
]
for pid in remove:
self._erase_phantom(pid, view=view)
def _erase_phantom(self, pid: str, *, view: sublime.View):
if pid in self.phantoms:
_ = self.phantoms.pop(pid)
view.erase_phantoms(pid)
def _write_mime_data_to_view(
self, mime_data: dict, region: sublime.Region, view: sublime.View
) -> None:
# Now we use basically text/plain for text type.
# Jupyter kernels often emits html whom minihtml cannot render.
if "text/plain" in mime_data:
content = mime_data["text/plain"]
lines = "\n(display data): {content}".format(content=content)
self._write_text_to_view(lines)
self._write_inline_html_phantom(
fix_whitespace_for_phantom(content), region, view
)
elif "text/html" in mime_data:
self._logger.info(
"Caught 'text/html' output without plain text. "
"Try to show with phantom."
)
content = mime_data["text/html"]
self._write_phantom(content)
self._write_inline_html_phantom(content, region, view)
if "image/png" in mime_data:
data = mime_data["image/png"].strip()
img_size = sublime.load_settings("Helium.sublime-settings").get(
"image_size", "optimal"
)
self._logger.info(self.get_view().viewport_extent())
width = self.get_view().viewport_extent()[0] - 2
dimensions = get_png_dimensions(data)
if img_size == "original" or (
img_size == "optimal" and (dimensions[0] < width)
):
width, height = dimensions
else:
scale_factor = width / dimensions[0]
height = dimensions[1] * scale_factor
content = (
'<body style="background-color: none">'
'<img alt="Out" style="width: {width}; height: {height}" src="data:image/png;base64,{data}" />'
+ "</body>"
).format(data=data, width=width, height=height, bgcolor="white")
self._write_phantom(content)
self._write_inline_image_phantom(data, region, view)
def _handle_inspect_reply(self, reply: dict):
window = sublime.active_window()
if window.find_output_panel(HELIUM_OBJECT_INSPECT_PANEL) is not None:
window.destroy_output_panel(HELIUM_OBJECT_INSPECT_PANEL)
view = window.create_output_panel(HELIUM_OBJECT_INSPECT_PANEL)
try:
self._logger.debug(reply)
text = remove_ansi_escape(reply["text/plain"])
view.run_command("append", {"characters": text})
window.run_command(
"show_panel", {"panel": "output." + HELIUM_OBJECT_INSPECT_PANEL}
)
except KeyError as ex:
self._logger.exception(ex)
def get_view(self):
"""Get view corresponds to the KernelConnection."""
view = None
view_name = self.view_name
window = sublime.active_window()
views = window.views()
for view_candidate in views:
if view_candidate.name() == view_name:
return view_candidate
if not view:
active_group = window.active_group()
view = window.new_file()
view.set_name(view_name)
view.settings().set("syntax", "Packages/Helium/Helium.sublime-syntax")
num_group = window.num_groups()
if num_group != 1:
if active_group + 1 < num_group:
new_group = active_group + 1
else:
new_group = active_group - 1
window.set_view_index(
view, new_group, len(window.sheets_in_group(new_group))
)
return view
def execute_code(self, code, phantom_region, view):
"""Run code with Jupyter kernel."""
msg_id = self.client.execute(code)
self.id2region[msg_id] = (
view,
sublime.Region(phantom_region.end() - 1, phantom_region.end() - 1),
)
info_message = "Kernel executed code ```{code}```.".format(code=code)
self._logger.info(info_message)
def is_alive(self):
"""Return True if kernel is alive."""
return self.client.hb_channel.is_beating()
def get_complete(self, code, cursor_pos, timeout=None):
"""Generate complete request."""
if self.execution_state != "idle":
return []
msg_id = self.client.complete(code, cursor_pos)
self.shell_msg_queues_lock.acquire()
try:
queue = self.shell_msg_queues[msg_id]
finally:
self.shell_msg_queues_lock.release()
try:
recv_msg = queue.get(timeout=timeout)
recv_content = recv_msg["content"]
self._logger.info(recv_content)
if "_jupyter_types_experimental" in recv_content.get("metadata", {}):
# If the reply has typing metadata, use it.
# This metadata for typing is obviously experimental
# and not documented yet.
return [
(
match["text"]
+ "\t"
+ (
"<no type info>" if match["type"] is None else match["type"]
),
escape_dollar_signs(match["text"]),
)
for match in recv_content["metadata"]["_jupyter_types_experimental"]
]
else:
# Just say the completion is came from this plugin, otherwise.
return [
(match + "\tHelium", escape_dollar_signs(match)) for match in recv_content["matches"]
]
except Empty:
self._logger.info("Completion timeout.")
except Exception as ex:
self._logger.exception(ex)
finally:
self.shell_msg_queues_lock.acquire()
try:
self.shell_msg_queues.pop(msg_id, None)
finally:
self.shell_msg_queues_lock.release()
return []
def get_inspection(self, code, cursor_pos, detail_level=0, timeout=None):
"""Get object inspection by sending a `inspect_request` message to kernel."""
msg_id = self.client.inspect(code, cursor_pos, detail_level)
self.shell_msg_queues_lock.acquire()
try:
queue = self.shell_msg_queues[msg_id]
finally:
self.shell_msg_queues_lock.release()
try:
recv_msg = queue.get(timeout=timeout)
self._handle_inspect_reply(recv_msg["content"]["data"])
except Empty:
self._logger.info("Object inspection timeout.")
finally:
self.shell_msg_queues_lock.acquire()
try:
self.shell_msg_queues.pop(msg_id, None)
finally:
self.shell_msg_queues_lock.release()