Repository navigation
Expand file tree
/
Copy pathcommon.ts
More file actions
148 lines (136 loc) · 5.21 KB
/
Copy pathcommon.ts
File metadata and controls
148 lines (136 loc) · 5.21 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
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import type { nbformat } from '@jupyterlab/coreutils';
import * as os from 'os';
import * as path from 'path';
import { Memento, Uri } from 'vscode';
import { splitMultilineString } from '../../datascience-ui/common';
import { DataScience } from '../common/utils/localize';
import { noop } from '../common/utils/misc';
import { traceError, traceInfo } from '../logging';
import { Settings } from './constants';
import { ICell } from './types';
// Can't figure out a better way to do this. Enumerate
// the allowed keys of different output formats.
const dummyStreamObj: nbformat.IStream = {
output_type: 'stream',
name: 'stdout',
text: ''
};
const dummyErrorObj: nbformat.IError = {
output_type: 'error',
ename: '',
evalue: '',
traceback: ['']
};
const dummyDisplayObj: nbformat.IDisplayData = {
output_type: 'display_data',
data: {},
metadata: {}
};
const dummyExecuteResultObj: nbformat.IExecuteResult = {
output_type: 'execute_result',
name: '',
execution_count: 0,
data: {},
metadata: {}
};
const AllowedKeys = {
['stream']: new Set(Object.keys(dummyStreamObj)),
['error']: new Set(Object.keys(dummyErrorObj)),
['display_data']: new Set(Object.keys(dummyDisplayObj)),
['execute_result']: new Set(Object.keys(dummyExecuteResultObj))
};
export function getSavedUriList(globalState: Memento): { uri: string; time: number }[] {
const uriList = globalState.get<{ uri: string; time: number }[]>(Settings.JupyterServerUriList);
return uriList
? uriList.sort((a, b) => {
return b.time - a.time;
})
: [];
}
export function addToUriList(globalState: Memento, uri: string, time: number) {
const uriList = getSavedUriList(globalState);
const editList = uriList.filter((f, i) => {
return f.uri !== uri && i < Settings.JupyterServerUriListMax - 1;
});
editList.splice(0, 0, { uri, time });
globalState.update(Settings.JupyterServerUriList, editList).then(noop, noop);
}
function fixupOutput(output: nbformat.IOutput): nbformat.IOutput {
let allowedKeys: Set<string>;
switch (output.output_type) {
case 'stream':
case 'error':
case 'execute_result':
case 'display_data':
allowedKeys = AllowedKeys[output.output_type];
break;
default:
return output;
}
const result = { ...output };
for (const k of Object.keys(output)) {
if (!allowedKeys.has(k)) {
delete result[k];
}
}
return result;
}
export function pruneCell(cell: nbformat.ICell): nbformat.ICell {
// Source is usually a single string on input. Convert back to an array
const result = ({
...cell,
source: splitMultilineString(cell.source)
// tslint:disable-next-line: no-any
} as any) as nbformat.ICell; // nyc (code coverage) barfs on this so just trick it.
// Remove outputs and execution_count from non code cells
if (result.cell_type !== 'code') {
delete result.outputs;
delete result.execution_count;
} else {
// Clean outputs from code cells
result.outputs = result.outputs ? (result.outputs as nbformat.IOutput[]).map(fixupOutput) : [];
}
return result;
}
export function traceCellResults(prefix: string, results: ICell[]) {
if (results.length > 0 && results[0].data.cell_type === 'code') {
const cell = results[0].data as nbformat.ICodeCell;
const error = cell.outputs && cell.outputs[0] ? cell.outputs[0].evalue : undefined;
if (error) {
traceError(`${prefix} Error : ${error}`);
} else if (cell.outputs && cell.outputs[0]) {
if (cell.outputs[0].output_type.includes('image')) {
traceInfo(`${prefix} Output: image`);
} else {
const data = cell.outputs[0].data;
const text = cell.outputs[0].text;
traceInfo(`${prefix} Output: ${text || JSON.stringify(data)}`);
}
}
} else {
traceInfo(`${prefix} no output.`);
}
}
export function translateKernelLanguageToMonaco(kernelLanguage: string): string {
// The only known translation is C# to csharp at the moment
if (kernelLanguage === 'C#' || kernelLanguage === 'c#') {
return 'csharp';
}
return kernelLanguage.toLowerCase();
}
export function generateNewNotebookUri(counter: number): Uri {
// Because of this bug here:
// https://github.com/microsoft/vscode/issues/93441
// We can't create 'untitled' files anymore. The untitled scheme will just be ignored.
// Instead we need to create untitled files in the temp folder and force a saveas whenever they're
// saved.
// However if there are files already on disk, we should be able to overwrite them because
// they will only ever be used by 'open' editors. So just use the current counter for our untitled count.
const fileName = `${DataScience.untitledNotebookFileName()}-${counter}.ipynb`;
const filePath = Uri.file(path.join(os.tmpdir(), fileName));
// Turn this back into an untitled
return filePath.with({ scheme: 'untitled', path: filePath.fsPath });
}