forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexportBase.ts
More file actions
86 lines (76 loc) · 2.87 KB
/
exportBase.ts
File metadata and controls
86 lines (76 loc) · 2.87 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
import { inject, injectable } from 'inversify';
import * as path from 'path';
import { CancellationToken, Uri } from 'vscode';
import { IPythonExecutionFactory, IPythonExecutionService } from '../../common/process/types';
import { reportAction } from '../progress/decorator';
import { ReportableAction } from '../progress/types';
import { IDataScienceFileSystem, IJupyterSubCommandExecutionService, INotebookImporter } from '../types';
import { ExportFormat, IExport } from './types';
@injectable()
export class ExportBase implements IExport {
constructor(
@inject(IPythonExecutionFactory) protected readonly pythonExecutionFactory: IPythonExecutionFactory,
@inject(IJupyterSubCommandExecutionService)
protected jupyterService: IJupyterSubCommandExecutionService,
@inject(IDataScienceFileSystem) protected readonly fs: IDataScienceFileSystem,
@inject(INotebookImporter) protected readonly importer: INotebookImporter
) {}
// tslint:disable-next-line: no-empty
public async export(_source: Uri, _target: Uri, _token: CancellationToken): Promise<void> {}
@reportAction(ReportableAction.PerformingExport)
public async executeCommand(
source: Uri,
target: Uri,
format: ExportFormat,
token: CancellationToken
): Promise<void> {
if (token.isCancellationRequested) {
return;
}
const service = await this.getExecutionService(source);
if (!service) {
return;
}
if (token.isCancellationRequested) {
return;
}
const tempTarget = await this.fs.createTemporaryLocalFile(path.extname(target.fsPath));
const args = [
source.fsPath,
'--to',
format,
'--output',
path.basename(tempTarget.filePath),
'--output-dir',
path.dirname(tempTarget.filePath)
];
const result = await service.execModule('jupyter', ['nbconvert'].concat(args), {
throwOnStdErr: false,
encoding: 'utf8',
token: token
});
if (token.isCancellationRequested) {
tempTarget.dispose();
return;
}
try {
await this.fs.copyLocal(tempTarget.filePath, target.fsPath);
} catch {
throw new Error(result.stderr);
} finally {
tempTarget.dispose();
}
}
protected async getExecutionService(source: Uri): Promise<IPythonExecutionService | undefined> {
const interpreter = await this.jupyterService.getSelectedInterpreter();
if (!interpreter) {
return;
}
return this.pythonExecutionFactory.createActivatedEnvironment({
resource: source,
interpreter,
allowEnvironmentFetchExceptions: false,
bypassCondaExecution: true
});
}
}