Skip to content

Commit 05197e4

Browse files
committed
#757
1 parent b70b3ad commit 05197e4

1 file changed

Lines changed: 81 additions & 0 deletions

File tree

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
"use strict";
2+
3+
import { EventEmitter } from "events";
4+
import * as Rx from 'rx';
5+
import { OutputChannel } from 'vscode';
6+
import { ParsedIOMessage } from '../contracts';
7+
import { Helpers } from '../common/helpers';
8+
type KernelMessage = any;
9+
10+
11+
export class MessageParser extends EventEmitter {
12+
private isDebugging: boolean;
13+
constructor(private outputChannel: OutputChannel) {
14+
super();
15+
this.isDebugging = process.env['DEBUG_DJAYAMANNE_IPYTHON'] === '1';
16+
}
17+
private writeToDebugLog(message: string) {
18+
if (!this.isDebugging) {
19+
return;
20+
}
21+
this.outputChannel.appendLine(message);
22+
}
23+
public processResponse(message: KernelMessage, observer?: Rx.Observer<ParsedIOMessage>) {
24+
if (!message) {
25+
return;
26+
}
27+
if (!Helpers.isValidMessag(message)) {
28+
return;
29+
}
30+
try {
31+
const msg_type = message.header.msg_type;
32+
if (msg_type === 'status') {
33+
this.writeToDebugLog(`Kernel Status = ${(message.content as any).execution_state}`);
34+
this.emit('status', (message.content as any).execution_state);
35+
}
36+
const msg_id = (message.parent_header as any).msg_id;
37+
if (!msg_id) {
38+
return;
39+
}
40+
const status = (message.content as any).status;
41+
let parsedMesage: ParsedIOMessage;
42+
switch (status) {
43+
case 'abort':
44+
case 'aborted':
45+
case 'error': {
46+
// http://jupyter-client.readthedocs.io/en/latest/messaging.html#request-reply
47+
if (msg_type !== 'complete_reply' && msg_type !== 'inspect_reply') {
48+
parsedMesage = {
49+
data: 'error',
50+
type: 'text',
51+
stream: 'status'
52+
};
53+
}
54+
break;
55+
}
56+
case 'ok': {
57+
// http://jupyter-client.readthedocs.io/en/latest/messaging.html#request-reply
58+
if (msg_type !== 'complete_reply' && msg_type !== 'inspect_reply') {
59+
parsedMesage = {
60+
data: 'ok',
61+
type: 'text',
62+
stream: 'status'
63+
};
64+
}
65+
}
66+
}
67+
this.writeToDebugLog(`Shell Result with msg_id = ${msg_id} with status = ${status}`);
68+
if (!parsedMesage) {
69+
parsedMesage = Helpers.parseIOMessage(message);
70+
}
71+
if (!parsedMesage || !observer) {
72+
return;
73+
}
74+
this.writeToDebugLog(`Shell Result with msg_id = ${msg_id} has message of: '\n${JSON.stringify(message)}`);
75+
observer.onNext(parsedMesage);
76+
}
77+
catch (ex) {
78+
this.emit('shellmessagepareerror', ex, JSON.stringify(message));
79+
}
80+
}
81+
}

0 commit comments

Comments
 (0)