-
Notifications
You must be signed in to change notification settings - Fork 295
/
dataViewerCommandRegistry.ts
161 lines (152 loc) · 8.28 KB
/
dataViewerCommandRegistry.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import { inject, injectable, named, optional } from 'inversify';
import { DebugConfiguration, Uri } from 'vscode';
import { DebugProtocol } from 'vscode-debugprotocol';
import { convertDebugProtocolVariableToIJupyterVariable } from '../../../kernels/variables/helpers';
import { IJupyterVariables } from '../../../kernels/variables/types';
import { IExtensionSingleActivationService } from '../../../platform/activation/types';
import { ICommandNameArgumentTypeMapping } from '../../../commands';
import {
IApplicationShell,
ICommandManager,
IDebugService,
IWorkspaceService
} from '../../../platform/common/application/types';
import { Commands, Identifiers } from '../../../platform/common/constants';
import { IPlatformService } from '../../../platform/common/platform/types';
import { IConfigurationService, IDisposableRegistry } from '../../../platform/common/types';
import { DataScience } from '../../../platform/common/utils/localize';
import { noop } from '../../../platform/common/utils/misc';
import { untildify } from '../../../platform/common/utils/platform';
import { IInterpreterService } from '../../../platform/interpreter/contracts';
import { traceError, traceInfo } from '../../../platform/logging';
import { IShowDataViewerFromVariablePanel } from '../../../messageTypes';
import { sendTelemetryEvent } from '../../../telemetry';
import { EventName } from '../../../platform/telemetry/constants';
import { PythonEnvironment } from '../../../standalone/api/extension';
import { IDataScienceErrorHandler } from '../../../kernels/errors/types';
import { DataViewerChecker } from './dataViewerChecker';
import { IDataViewerDependencyService, IDataViewerFactory, IJupyterVariableDataProviderFactory } from './types';
@injectable()
export class DataViewerCommandRegistry implements IExtensionSingleActivationService {
private dataViewerChecker: DataViewerChecker;
constructor(
@inject(IDisposableRegistry) private readonly disposables: IDisposableRegistry,
@inject(ICommandManager) private readonly commandManager: ICommandManager,
@inject(IDebugService) @optional() private debugService: IDebugService | undefined,
@inject(IConfigurationService) configService: IConfigurationService,
@inject(IApplicationShell) appShell: IApplicationShell,
@inject(IJupyterVariableDataProviderFactory)
@optional()
private readonly jupyterVariableDataProviderFactory: IJupyterVariableDataProviderFactory | undefined,
@inject(IDataViewerFactory) @optional() private readonly dataViewerFactory: IDataViewerFactory | undefined,
@inject(IJupyterVariables)
@optional()
@named(Identifiers.DEBUGGER_VARIABLES)
private variableProvider: IJupyterVariables | undefined,
@inject(IWorkspaceService) private readonly workspace: IWorkspaceService,
@inject(IDataScienceErrorHandler) private readonly errorHandler: IDataScienceErrorHandler,
@inject(IDataViewerDependencyService)
@optional()
private readonly dataViewerDependencyService: IDataViewerDependencyService | undefined,
@inject(IInterpreterService) @optional() private readonly interpreterService: IInterpreterService | undefined,
@inject(IPlatformService) private readonly platformService: IPlatformService
) {
this.dataViewerChecker = new DataViewerChecker(configService, appShell);
if (!this.workspace.isTrusted) {
this.workspace.onDidGrantWorkspaceTrust(this.registerCommandsIfTrusted, this, this.disposables);
}
}
async activate(): Promise<void> {
this.registerCommandsIfTrusted();
}
private registerCommandsIfTrusted() {
if (!this.workspace.isTrusted) {
return;
}
this.registerCommand(Commands.ShowDataViewer, this.onVariablePanelShowDataViewerRequest);
}
private registerCommand<
E extends keyof ICommandNameArgumentTypeMapping,
U extends ICommandNameArgumentTypeMapping[E]
// eslint-disable-next-line @typescript-eslint/no-explicit-any
>(command: E, callback: (...args: U) => any) {
const disposable = this.commandManager.registerCommand(command, callback, this);
this.disposables.push(disposable);
}
private async onVariablePanelShowDataViewerRequest(request: IShowDataViewerFromVariablePanel) {
sendTelemetryEvent(EventName.OPEN_DATAVIEWER_FROM_VARIABLE_WINDOW_REQUEST);
if (
this.debugService?.activeDebugSession &&
this.variableProvider &&
this.jupyterVariableDataProviderFactory &&
this.dataViewerFactory
) {
try {
// First find out the current python environment that we are working with
if (
this.debugService.activeDebugSession.configuration &&
this.dataViewerDependencyService &&
this.interpreterService
) {
// Check the debug adapter session to get the python env that launched it
const pythonEnv = await this.getDebugAdapterPython(
this.debugService.activeDebugSession.configuration
);
pythonEnv && (await this.dataViewerDependencyService.checkAndInstallMissingDependencies(pythonEnv));
}
const variable = convertDebugProtocolVariableToIJupyterVariable(
request.variable as DebugProtocol.Variable
);
const jupyterVariable = await this.variableProvider.getFullVariable(variable);
const jupyterVariableDataProvider = await this.jupyterVariableDataProviderFactory.create(
jupyterVariable
);
const dataFrameInfo = await jupyterVariableDataProvider.getDataFrameInfo();
const columnSize = dataFrameInfo?.columns?.length;
if (columnSize && (await this.dataViewerChecker.isRequestedColumnSizeAllowed(columnSize))) {
const title: string = `${DataScience.dataExplorerTitle()} - ${jupyterVariable.name}`;
await this.dataViewerFactory.create(jupyterVariableDataProvider, title);
sendTelemetryEvent(EventName.OPEN_DATAVIEWER_FROM_VARIABLE_WINDOW_SUCCESS);
}
} catch (e) {
sendTelemetryEvent(EventName.OPEN_DATAVIEWER_FROM_VARIABLE_WINDOW_ERROR, undefined, undefined, e);
traceError(e);
this.errorHandler.handleError(e).then(noop, noop);
}
}
}
// For the given debug adapter, return the PythonEnvironment used to launch it
// Mirrors the logic from Python extension here:
// https://github.com/microsoft/vscode-python/blob/35b813f37d1ceec547277180e3aa07bd24d86f89/src/client/debugger/extension/adapter/factory.ts#L116
private async getDebugAdapterPython(
debugConfiguration: DebugConfiguration
): Promise<PythonEnvironment | undefined> {
if (!this.interpreterService) {
// Interpreter service is optional
traceInfo('Interpreter Service missing when trying getDebugAdapterPython');
return;
}
// Check debugAdapterPython and pythonPath
let pythonPath: string = '';
if (debugConfiguration.debugAdapterPython !== undefined) {
traceInfo('Found debugAdapterPython on Debug Configuration to use');
pythonPath = debugConfiguration.debugAdapterPython;
} else if (debugConfiguration.pythonPath) {
traceInfo('Found pythonPath on Debug Configuration to use');
pythonPath = debugConfiguration.pythonPath;
}
if (pythonPath) {
let untildePath = debugConfiguration.python;
if (untildePath.startsWith('~') && this.platformService.homeDir) {
untildePath = untildify(untildePath, this.platformService.homeDir.path);
}
return this.interpreterService.getInterpreterDetails(Uri.file(untildePath));
} else {
// Failed to find the expected configuration items, use active interpreter (might be attach scenario)
return this.interpreterService.getActiveInterpreter();
}
}
}