forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathjupyterIntegration.ts
231 lines (216 loc) · 9.62 KB
/
jupyterIntegration.ts
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
// tslint:disable-next-line: no-single-line-block-comment
/* eslint-disable comma-dangle */
// tslint:disable-next-line: no-single-line-block-comment
/* eslint-disable implicit-arrow-linebreak */
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { inject, injectable, named } from 'inversify';
import { dirname } from 'path';
import { CancellationToken, Disposable, Event, Extension, Memento, Uri } from 'vscode';
import * as lsp from 'vscode-languageserver-protocol';
import { ILanguageServerCache, ILanguageServerConnection } from '../activation/types';
import { JUPYTER_EXTENSION_ID } from '../common/constants';
import { InterpreterUri } from '../common/installer/types';
import {
GLOBAL_MEMENTO,
IExtensions,
IInstaller,
IMemento,
InstallerResponse,
Product,
Resource
} from '../common/types';
import { isResource } from '../common/utils/misc';
import { getDebugpyPackagePath } from '../debugger/extension/adapter/remoteLaunchers';
import { IEnvironmentActivationService } from '../interpreter/activation/types';
import { IInterpreterQuickPickItem, IInterpreterSelector } from '../interpreter/configuration/types';
import { IInterpreterService } from '../interpreter/contracts';
import { IWindowsStoreInterpreter } from '../interpreter/locators/types';
import { WindowsStoreInterpreter } from '../pythonEnvironments/discovery/locators/services/windowsStoreInterpreter';
import { PythonEnvironment } from '../pythonEnvironments/info';
import { IDataViewerDataProvider, IJupyterUriProvider } from './types';
export interface ILanguageServer extends Disposable {
readonly connection: ILanguageServerConnection;
readonly capabilities: lsp.ServerCapabilities;
}
/**
* This allows Python exntension to update Product enum without breaking Jupyter.
* I.e. we have a strict contract, else using numbers (in enums) is bound to break across products.
*/
enum JupyterProductToInstall {
jupyter = 'jupyter',
ipykernel = 'ipykernel',
notebook = 'notebook',
kernelspec = 'kernelspec',
nbconvert = 'nbconvert',
pandas = 'pandas'
}
const ProductMapping: { [key in JupyterProductToInstall]: Product } = {
[JupyterProductToInstall.ipykernel]: Product.ipykernel,
[JupyterProductToInstall.jupyter]: Product.jupyter,
[JupyterProductToInstall.kernelspec]: Product.kernelspec,
[JupyterProductToInstall.nbconvert]: Product.nbconvert,
[JupyterProductToInstall.notebook]: Product.notebook,
[JupyterProductToInstall.pandas]: Product.pandas
};
type PythonApiForJupyterExtension = {
/**
* IInterpreterService
*/
onDidChangeInterpreter: Event<void>;
/**
* IInterpreterService
*/
getInterpreters(resource?: Uri): Promise<PythonEnvironment[]>;
/**
* IInterpreterService
*/
getActiveInterpreter(resource?: Uri): Promise<PythonEnvironment | undefined>;
/**
* IInterpreterService
*/
getInterpreterDetails(pythonPath: string, resource?: Uri): Promise<undefined | PythonEnvironment>;
/**
* IEnvironmentActivationService
*/
getActivatedEnvironmentVariables(
resource: Resource,
interpreter?: PythonEnvironment,
allowExceptions?: boolean
): Promise<NodeJS.ProcessEnv | undefined>;
isWindowsStoreInterpreter(pythonPath: string): Promise<boolean>;
/**
* IWindowsStoreInterpreter
*/
getSuggestions(resource: Resource): Promise<IInterpreterQuickPickItem[]>;
/**
* IInstaller
*/
install(
product: JupyterProductToInstall,
resource?: InterpreterUri,
cancel?: CancellationToken
): Promise<InstallerResponse>;
/**
* Returns path to where `debugpy` is. In python extension this is `/pythonFiles/lib/python`.
*/
getDebuggerPath(): Promise<string>;
/**
* Retrieve interpreter path selected for Jupyter server from Python memento storage
*/
getInterpreterPathSelectedForJupyterServer(): string | undefined;
/**
* Returns a ILanguageServer that can be used for communicating with a language server process.
* @param resource file that determines which connection to return
*/
getLanguageServer(resource?: InterpreterUri): Promise<ILanguageServer | undefined>;
};
export type JupyterExtensionApi = {
/**
* Registers python extension specific parts with the jupyter extension
* @param interpreterService
*/
registerPythonApi(interpreterService: PythonApiForJupyterExtension): void;
/**
* Launches Data Viewer component.
* @param {IDataViewerDataProvider} dataProvider Instance that will be used by the Data Viewer component to fetch data.
* @param {string} title Data Viewer title
*/
showDataViewer(dataProvider: IDataViewerDataProvider, title: string): Promise<void>;
/**
* Registers a remote server provider component that's used to pick remote jupyter server URIs
* @param serverProvider object called back when picking jupyter server URI
*/
registerRemoteServerProvider(serverProvider: IJupyterUriProvider): void;
};
@injectable()
export class JupyterExtensionIntegration {
private jupyterExtension: Extension<JupyterExtensionApi> | undefined;
constructor(
@inject(IExtensions) private readonly extensions: IExtensions,
@inject(IInterpreterService) private readonly interpreterService: IInterpreterService,
@inject(IInterpreterSelector) private readonly interpreterSelector: IInterpreterSelector,
@inject(WindowsStoreInterpreter) private readonly windowsStoreInterpreter: IWindowsStoreInterpreter,
@inject(IInstaller) private readonly installer: IInstaller,
@inject(IEnvironmentActivationService) private readonly envActivation: IEnvironmentActivationService,
@inject(ILanguageServerCache) private readonly languageServerCache: ILanguageServerCache,
@inject(IMemento) @named(GLOBAL_MEMENTO) private globalState: Memento
) {}
public registerApi(jupyterExtensionApi: JupyterExtensionApi) {
// Forward python parts
jupyterExtensionApi.registerPythonApi({
onDidChangeInterpreter: this.interpreterService.onDidChangeInterpreter,
getActiveInterpreter: async (resource?: Uri) => this.interpreterService.getActiveInterpreter(resource),
getInterpreterDetails: async (pythonPath: string) =>
this.interpreterService.getInterpreterDetails(pythonPath),
getInterpreters: async (resource: Uri | undefined) => this.interpreterService.getInterpreters(resource),
getActivatedEnvironmentVariables: async (
resource: Resource,
interpreter?: PythonEnvironment,
allowExceptions?: boolean
) => this.envActivation.getActivatedEnvironmentVariables(resource, interpreter, allowExceptions),
isWindowsStoreInterpreter: async (pythonPath: string): Promise<boolean> =>
this.windowsStoreInterpreter.isWindowsStoreInterpreter(pythonPath),
getSuggestions: async (resource: Resource): Promise<IInterpreterQuickPickItem[]> =>
this.interpreterSelector.getSuggestions(resource),
install: async (
product: JupyterProductToInstall,
resource?: InterpreterUri,
cancel?: CancellationToken
): Promise<InstallerResponse> => this.installer.install(ProductMapping[product], resource, cancel),
getDebuggerPath: async () => dirname(getDebugpyPackagePath()),
getInterpreterPathSelectedForJupyterServer: () =>
this.globalState.get<string | undefined>('INTERPRETER_PATH_SELECTED_FOR_JUPYTER_SERVER'),
getLanguageServer: async (r) => {
const resource = isResource(r) ? r : undefined;
const interpreter = !isResource(r) ? r : undefined;
const client = await this.languageServerCache.get(resource, interpreter);
// Some langauge servers don't support the connection yet. (like Jedi until we switch to LSP)
if (client && client.connection && client.capabilities) {
return {
connection: client.connection,
capabilities: client.capabilities,
dispose: client.dispose
};
}
return undefined;
}
});
}
public async integrateWithJupyterExtension(): Promise<void> {
const api = await this.getExtensionApi();
if (api) {
this.registerApi(api);
}
}
public registerRemoteServerProvider(serverProvider: IJupyterUriProvider) {
this.getExtensionApi()
.then((e) => {
if (e) {
e.registerRemoteServerProvider(serverProvider);
}
})
.ignoreErrors();
}
public async showDataViewer(dataProvider: IDataViewerDataProvider, title: string) {
const api = await this.getExtensionApi();
if (api) {
return api.showDataViewer(dataProvider, title);
}
}
private async getExtensionApi(): Promise<JupyterExtensionApi | undefined> {
if (!this.jupyterExtension) {
const jupyterExtension = this.extensions.getExtension<JupyterExtensionApi>(JUPYTER_EXTENSION_ID);
if (!jupyterExtension) {
return;
}
await jupyterExtension.activate();
if (jupyterExtension.isActive) {
this.jupyterExtension = jupyterExtension;
return this.jupyterExtension.exports;
}
} else {
return this.jupyterExtension.exports;
}
}
}