GeezerVison is showing github.com — hooked in returning of kernelspecs · meflyup/pythonVSCode@a9b7d5a
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: pythonFiles/PythonTools/ipythonServer.py +8-8Lines changed: 8 additions & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -80,9 +80,9 @@ def _debug_write(out): | 80 | 80 | sys.__stdout__.flush() | 81 | 81 |
| 82 | 82 |
| 83 | | -def listKernelNames(): | | 83 | +def listKernelSpecs(): | 84 | 84 | """Returns a dict mapping kernel names to resource directories.""" | 85 | | - return list(kernelSpecManager.find_kernel_specs().keys()) | | 85 | + return kernelSpecManager.get_all_specs() | 86 | 86 |
| 87 | 87 |
| 88 | 88 | class IPythonExitException(Exception): | @@ -123,7 +123,7 @@ class iPythonSocketServer(object): | 123 | 123 | """Messages sent back as responses""" | 124 | 124 | _PONG = to_bytes('PONG') | 125 | 125 | _EXIT = to_bytes('EXIT') | 126 | | - _LSTK = to_bytes('LSTK') | | 126 | + _LSKS = to_bytes('LSKS') | 127 | 127 | _EROR = to_bytes('EROR') | 128 | 128 | _TEST = to_bytes('TEST') | 129 | 129 |
| @@ -251,10 +251,10 @@ def _cmd_ping(self, id): | 251 | 251 | def _cmd_lstk(self, id): | 252 | 252 | """List kernel specs""" | 253 | 253 | _debug_write('Listing kernel specs') | 254 | | - kernelspecs = json.dumps(listKernelNames()) | | 254 | + kernelspecs = json.dumps(listKernelSpecs()) | 255 | 255 | with self.send_lock: | 256 | | - _debug_write('Replying with kernels = ' + kernelspecs) | 257 | | - write_bytes(self.conn, iPythonSocketServer._LSTK) | | 256 | + _debug_write('Replying with kernel Specs= ' + kernelspecs) | | 257 | + write_bytes(self.conn, iPythonSocketServer._LSKS) | 258 | 258 | write_string(self.conn, id) | 259 | 259 | write_string(self.conn, kernelspecs) | 260 | 260 |
| @@ -348,11 +348,11 @@ def flush(self): | 348 | 348 | to_bytes('exit'): _cmd_exit, | 349 | 349 | to_bytes('ping'): _cmd_ping, | 350 | 350 | to_bytes('inpl'): _cmd_inpl, | 351 | | - to_bytes('lstk'): _cmd_lstk, | | 351 | + to_bytes('lsks'): _cmd_lstk, | 352 | 352 | } | 353 | 353 |
| 354 | 354 | _COMMANDS_WITH_IDS = { | 355 | | - to_bytes('lstk'): True, | | 355 | + to_bytes('lsks'): True, | 356 | 356 | to_bytes('ping'): True, | 357 | 357 | } | 358 | 358 |
| |
Collapse file Copy file name to clipboardExpand all lines: src/client/jupyter/jupyter_client/commands.ts +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 3 | 3 | export class Commands { | 4 | 4 | public static ExitCommandBytes: Buffer = new Buffer("exit"); | 5 | 5 | public static PingBytes: Buffer = new Buffer("ping"); | 6 | | - public static ListKernelsBytes: Buffer = new Buffer("lstk"); | | 6 | + public static ListKernelSpecsBytes: Buffer = new Buffer("lsks"); | 7 | 7 | } | 8 | 8 |
| 9 | 9 | export namespace ResponseCommands { | 10 | 10 | export const Pong = 'PONG'; | 11 | | - export const ListKernels = 'LSTK'; | | 11 | + export const ListKernelsSpecs = 'LSKS'; | 12 | 12 | export const Error = 'EROR'; | 13 | 13 | } |
Collapse file Copy file name to clipboardExpand all lines: src/client/jupyter/jupyter_client/ipythonAdapter.ts +5-5Lines changed: 5 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -13,7 +13,7 @@ export class iPythonAdapter extends SocketCallbackHandler { | 13 | 13 | constructor(socketServer: SocketServer) { | 14 | 14 | super(socketServer); | 15 | 15 | this.registerCommandHandler(ResponseCommands.Pong, this.onPong.bind(this)); | 16 | | - this.registerCommandHandler(ResponseCommands.ListKernels, this.onKernelsListed.bind(this)); | | 16 | + this.registerCommandHandler(ResponseCommands.ListKernelsSpecs, this.onKernelsListed.bind(this)); | 17 | 17 | this.registerCommandHandler(ResponseCommands.Error, this.onError.bind(this)); | 18 | 18 | this.idDispenser = new IdDispenser(); | 19 | 19 | } | @@ -53,9 +53,9 @@ export class iPythonAdapter extends SocketCallbackHandler { | 53 | 53 | this.idDispenser.Free(parseInt(id)); | 54 | 54 | } | 55 | 55 |
| 56 | | - public listKernels(): Promise<string[]> { | 57 | | - const [def, id] = this.createId<string[]>(); | 58 | | - this.SendRawCommand(Commands.ListKernelsBytes); | | 56 | + public listKernelSpecs(): Promise<any> { | | 57 | + const [def, id] = this.createId<any>(); | | 58 | + this.SendRawCommand(Commands.ListKernelSpecsBytes); | 59 | 59 | this.stream.WriteString(id); | 60 | 60 | return def.promise; | 61 | 61 | } | @@ -70,7 +70,7 @@ export class iPythonAdapter extends SocketCallbackHandler { | 70 | 70 | const def = this.pendingCommands.get(id); | 71 | 71 | this.releaseId(id); | 72 | 72 |
| 73 | | - let kernelList: string[]; | | 73 | + let kernelList: any; | 74 | 74 | try { | 75 | 75 | kernelList = JSON.parse(kernels) | 76 | 76 | } | |
Collapse file Copy file name to clipboardExpand all lines: src/client/jupyter/jupyter_client/jupyterClient.ts +20-1Lines changed: 20 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -4,6 +4,7 @@ import * as child_process from 'child_process'; | 4 | 4 | import * as path from 'path'; | 5 | 5 | import * as vscode from 'vscode'; | 6 | 6 | import { createDeferred, Deferred } from '../../common/helpers'; | | 7 | +import { KernelspecMetadata, Kernelspec } from '../contracts'; | 7 | 8 |
| 8 | 9 | export class JupyterClient { | 9 | 10 | constructor(private outputChannel: vscode.OutputChannel) { | @@ -12,9 +13,17 @@ export class JupyterClient { | 12 | 13 | private process: child_process.ChildProcess; | 13 | 14 | private socketServer: SocketServer; | 14 | 15 | private ipythonAdapter: iPythonAdapter; | | 16 | + | | 17 | + private startDef: Deferred<any>; | 15 | 18 | public start(): Promise<any> { | | 19 | + if (this.startDef) { | | 20 | + return this.startDef.promise; | | 21 | + } | | 22 | + | | 23 | + this.startDef = createDeferred<any>(); | 16 | 24 | const pyFile = path.join(__dirname, '..', '..', '..', '..', 'pythonFiles', 'PythonTools', 'ipythonServer.py'); | 17 | | - return this.startSocketServer().then(port => { | | 25 | + | | 26 | + this.startSocketServer().then(port => { | 18 | 27 | const def = createDeferred<any>(); | 19 | 28 | const newEnv = { "DEBUG_DJAYAMANNE_IPYTHON": "1" }; | 20 | 29 | Object.assign(newEnv, process.env); | @@ -68,13 +77,23 @@ export class JupyterClient { | 68 | 77 | } | 69 | 78 |
| 70 | 79 | return def.promise; | | 80 | + }).then(() => { | | 81 | + this.startDef.resolve(); | | 82 | + }).catch(reason => { | | 83 | + this.startDef.reject(reason); | 71 | 84 | }); | | 85 | + | | 86 | + return this.startDef.promise; | 72 | 87 | } | 73 | 88 | private startSocketServer(): Promise<number> { | 74 | 89 | this.socketServer = new SocketServer(); | 75 | 90 | this.ipythonAdapter = new iPythonAdapter(this.socketServer); | 76 | 91 | return this.socketServer.Start(); | 77 | 92 | } | | 93 | + | | 94 | + public getAllKernelSpecs(): Promise<{ [key: string]: Kernelspec }> { | | 95 | + return this.start().then(() => this.ipythonAdapter.listKernelSpecs()); | | 96 | + } | 78 | 97 | } | 79 | 98 |
| 80 | 99 |
| |
Collapse file Copy file name to clipboardExpand all lines: src/client/jupyter/kernel-manager.ts +13-36Lines changed: 13 additions & 36 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 2 | 2 | import * as child_process from 'child_process'; | 3 | 3 | import * as os from 'os'; | 4 | 4 | import * as vscode from 'vscode'; | 5 | | -import {Kernel} from './kernel'; | 6 | | -import {WSKernel} from './ws-kernel'; | 7 | | -import {ZMQKernel} from './zmq-kernel'; | 8 | | -import {launchSpec} from 'spawnteract'; | 9 | | -import {KernelspecMetadata, Kernelspec} from './contracts'; | 10 | | -import {Commands, Documentation} from '../common/constants'; | 11 | | -import {EventEmitter} from 'events'; | 12 | | -import {PythonSettings} from '../common/configSettings'; | 13 | | -import {formatErrorForLogging} from '../common/utils'; | 14 | | -import {JmpModuleLoadError} from '../common/errors'; | 15 | | - | | 5 | +import { Kernel } from './kernel'; | | 6 | +import { WSKernel } from './ws-kernel'; | | 7 | +import { ZMQKernel } from './zmq-kernel'; | | 8 | +import { launchSpec } from 'spawnteract'; | | 9 | +import { KernelspecMetadata, Kernelspec } from './contracts'; | | 10 | +import { Commands, Documentation } from '../common/constants'; | | 11 | +import { EventEmitter } from 'events'; | | 12 | +import { PythonSettings } from '../common/configSettings'; | | 13 | +import { formatErrorForLogging } from '../common/utils'; | | 14 | +import { JmpModuleLoadError } from '../common/errors'; | | 15 | +import { JupyterClient } from './jupyter_client/jupyterClient'; | 16 | 16 | // Todo: Refactor the error handling and displaying of messages | 17 | 17 |
| 18 | 18 | const pythonSettings = PythonSettings.getInstance(); | @@ -21,7 +21,7 @@ export class KernelManagerImpl extends EventEmitter { | 21 | 21 | private _runningKernels: Map<string, Kernel>; | 22 | 22 | private _kernelSpecs: { [key: string]: Kernelspec }; | 23 | 23 | private disposables: vscode.Disposable[]; | 24 | | - constructor(private outputChannel: vscode.OutputChannel) { | | 24 | + constructor(private outputChannel: vscode.OutputChannel, private jupyterClient: JupyterClient) { | 25 | 25 | super(); | 26 | 26 | this.disposables = []; | 27 | 27 | this._runningKernels = new Map<string, Kernel>(); | @@ -221,29 +221,6 @@ export class KernelManagerImpl extends EventEmitter { | 221 | 221 | } | 222 | 222 |
| 223 | 223 | public getKernelSpecsFromJupyter(): Promise<any> { | 224 | | - const jupyter = 'jupyter kernelspec list --json --log-level=CRITICAL'; | 225 | | - const ipython = 'ipython kernelspec list --json --log-level=CRITICAL'; | 226 | | - return this.getKernelSpecsFrom(jupyter).catch(jupyterError => { | 227 | | - return this.getKernelSpecsFrom(ipython); | 228 | | - }); | 229 | | - } | 230 | | - | 231 | | - public getKernelSpecsFrom(command: string): Promise<any> { | 232 | | - const options = { | 233 | | - killSignal: 'SIGINT' | 234 | | - }; | 235 | | - return new Promise<any>((resolve, reject) => { | 236 | | - return child_process.exec(command, options, (err, stdout, stderr) => { | 237 | | - if (err) { | 238 | | - return reject(err); | 239 | | - } | 240 | | - try { | 241 | | - const kernelSpecs = JSON.parse(stdout).kernelspecs; | 242 | | - resolve(kernelSpecs); | 243 | | - } catch (err) { | 244 | | - return reject(err); | 245 | | - } | 246 | | - }); | 247 | | - }); | | 224 | + return this.jupyterClient.getAllKernelSpecs(); | 248 | 225 | } | 249 | 226 | } |
Collapse file Copy file name to clipboardExpand all lines: src/client/jupyter/main.ts +21-19Lines changed: 21 additions & 19 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -27,7 +27,9 @@ export class Jupyter extends vscode.Disposable { | 27 | 27 | this.registerKernelCommands(); | 28 | 28 | } | 29 | 29 | activate(state) { | 30 | | - this.kernelManager = new KernelManagerImpl(this.outputChannel); | | 30 | + const m = new main.JupyterClient(this.outputChannel); | | 31 | + m.start(); | | 32 | + this.kernelManager = new KernelManagerImpl(this.outputChannel, m); | 31 | 33 | this.disposables.push(this.kernelManager); | 32 | 34 | this.disposables.push(vscode.window.onDidChangeActiveTextEditor(this.onEditorChanged.bind(this))); | 33 | 35 | this.codeLensProvider = new JupyterCodeLensProvider(); | @@ -79,25 +81,25 @@ export class Jupyter extends vscode.Disposable { | 79 | 81 | this.status.setActiveKernel(this.kernel ? this.kernel.kernelSpec : null); | 80 | 82 | } | 81 | 83 | executeCode(code: string, language: string): Promise<any> { | 82 | | - const m = new main.JupyterClient(this.outputChannel); | 83 | | - m.start(); | 84 | | - return Promise.resolve(); | 85 | | - // telemetryHelper.sendTelemetryEvent(telemetryContracts.Jupyter.Usage); | | 84 | + // const m = new main.JupyterClient(this.outputChannel); | | 85 | + // m.start(); | | 86 | + // return Promise.resolve(); | | 87 | + telemetryHelper.sendTelemetryEvent(telemetryContracts.Jupyter.Usage); | 86 | 88 |
| 87 | | - // if (this.kernel && this.kernel.kernelSpec.language === language) { | 88 | | - // return this.executeAndDisplay(this.kernel, code); | 89 | | - // } | 90 | | - // return this.kernelManager.startKernelFor(language) | 91 | | - // .then(kernel => { | 92 | | - // if (kernel) { | 93 | | - // this.onKernelChanged(kernel); | 94 | | - // return this.executeAndDisplay(kernel, code); | 95 | | - // } | 96 | | - // }).catch(reason => { | 97 | | - // const message = typeof reason === 'string' ? reason : reason.message; | 98 | | - // vscode.window.showErrorMessage(message); | 99 | | - // this.outputChannel.appendLine(formatErrorForLogging(reason)); | 100 | | - // }); | | 89 | + if (this.kernel && this.kernel.kernelSpec.language === language) { | | 90 | + return this.executeAndDisplay(this.kernel, code); | | 91 | + } | | 92 | + return this.kernelManager.startKernelFor(language) | | 93 | + .then(kernel => { | | 94 | + if (kernel) { | | 95 | + this.onKernelChanged(kernel); | | 96 | + return this.executeAndDisplay(kernel, code); | | 97 | + } | | 98 | + }).catch(reason => { | | 99 | + const message = typeof reason === 'string' ? reason : reason.message; | | 100 | + vscode.window.showErrorMessage(message); | | 101 | + this.outputChannel.appendLine(formatErrorForLogging(reason)); | | 102 | + }); | 101 | 103 | } | 102 | 104 | private executeAndDisplay(kernel: Kernel, code: string) { | 103 | 105 | return this.executeCodeInKernel(kernel, code).then(result => { | |
|