-
-
Notifications
You must be signed in to change notification settings - Fork 401
/
Copy pathsettings.ts
340 lines (278 loc) · 8.75 KB
/
settings.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import * as path from 'path';
import * as fs from 'fs';
import { getUserDataDir, getUserHomeDir } from '../utils';
export const DEFAULT_WIN_WIDTH = 1024;
export const DEFAULT_WIN_HEIGHT = 768;
export enum ThemeType {
System = 'system',
Light = 'light',
Dark = 'dark'
}
export enum StartupMode {
WelcomePage = 'welcome-page',
NewLocalSession = 'new-local-session',
LastSessions = 'restore-sessions'
}
export enum LogLevel {
Error = 'error',
Warn = 'warn',
Info = 'info',
Verbose = 'verbose',
Debug = 'debug'
}
export enum CtrlWBehavior {
CloseWindow = 'close',
Warn = 'warn',
CloseTab = 'close-tab',
DoNotClose = 'do-not-close'
}
export type KeyValueMap = { [key: string]: string };
export enum SettingType {
checkForUpdatesAutomatically = 'checkForUpdatesAutomatically',
installUpdatesAutomatically = 'installUpdatesAutomatically',
notifyOnBundledEnvUpdates = 'notifyOnBundledEnvUpdates',
updateBundledEnvAutomatically = 'updateBundledEnvAutomatically',
theme = 'theme',
syncJupyterLabTheme = 'syncJupyterLabTheme',
showNewsFeed = 'showNewsFeed',
defaultWorkingDirectory = 'defaultWorkingDirectory',
pythonPath = 'pythonPath',
serverArgs = 'serverArgs',
overrideDefaultServerArgs = 'overrideDefaultServerArgs',
serverEnvVars = 'serverEnvVars',
startupMode = 'startupMode',
ctrlWBehavior = 'ctrlWBehavior',
logLevel = 'logLevel',
condaPath = 'condaPath',
systemPythonPath = 'systemPythonPath',
pythonEnvsPath = 'pythonEnvsPath',
condaChannels = 'condaChannels'
}
export const serverLaunchArgsFixed = [
'--no-browser',
'--expose-app-in-browser',
`--ServerApp.port={port}`,
// use our token rather than any pre-configured password
'--ServerApp.password=""',
`--ServerApp.token="{token}"`,
'--LabApp.quit_button=False'
];
export const serverLaunchArgsDefault = [
// do not use any config file
'--JupyterApp.config_file_name=""',
// enable hidden files (let user decide whether to display them)
'--ContentsManager.allow_hidden=True'
];
export class Setting<T> {
constructor(defaultValue: T, options?: Setting.IOptions) {
this._defaultValue = defaultValue;
this._options = options;
}
set value(val: T) {
this._value = val;
this._valueSet = true;
}
get value(): T {
return this._valueSet ? this._value : this._defaultValue;
}
get valueSet(): boolean {
return this._valueSet;
}
get differentThanDefault(): boolean {
return this.value !== this._defaultValue;
}
get wsOverridable(): boolean {
return this?._options?.wsOverridable;
}
private _defaultValue: T;
private _value: T;
private _valueSet = false;
private _options: Setting.IOptions;
}
export namespace Setting {
export interface IOptions {
wsOverridable?: boolean;
}
}
export class UserSettings {
constructor(readSettings: boolean = true) {
this._settings = {
checkForUpdatesAutomatically: new Setting<boolean>(true),
installUpdatesAutomatically: new Setting<boolean>(true),
notifyOnBundledEnvUpdates: new Setting<boolean>(true),
updateBundledEnvAutomatically: new Setting<boolean>(false),
showNewsFeed: new Setting<boolean>(true),
/* making themes workspace overridable is not feasible.
When app has multiple windows, different window titlebars shouldn't have different themes.
Also, JupyterLab theme is stored as user settings in {USER_DATA}/jupyterlab-desktop/lab/.
An individual working-dir cannot have a different theme with common lab settings.
*/
theme: new Setting<ThemeType>(ThemeType.System),
syncJupyterLabTheme: new Setting<boolean>(true),
defaultWorkingDirectory: new Setting<string>(''),
pythonPath: new Setting<string>('', { wsOverridable: true }),
serverArgs: new Setting<string>('', { wsOverridable: true }),
overrideDefaultServerArgs: new Setting<boolean>(false, {
wsOverridable: true
}),
serverEnvVars: new Setting<KeyValueMap>({}, { wsOverridable: true }),
startupMode: new Setting<StartupMode>(StartupMode.WelcomePage),
ctrlWBehavior: new Setting<CtrlWBehavior>(CtrlWBehavior.CloseTab),
logLevel: new Setting<string>(LogLevel.Warn),
condaPath: new Setting<string>(''),
systemPythonPath: new Setting<string>(''),
pythonEnvsPath: new Setting<string>(''),
condaChannels: new Setting<string[]>(['conda-forge'])
};
if (readSettings) {
this.read();
}
}
getValue(setting: SettingType) {
return this._settings[setting].value;
}
setValue(setting: SettingType, value: any) {
this._settings[setting].value = value;
}
read() {
const userSettingsPath = this._getUserSettingsPath();
if (!fs.existsSync(userSettingsPath)) {
return;
}
const data = fs.readFileSync(userSettingsPath);
const jsonData = JSON.parse(data.toString());
for (let key in SettingType) {
if (key in jsonData) {
const setting = this._settings[key];
setting.value = jsonData[key];
}
}
}
save() {
const userSettingsPath = this._getUserSettingsPath();
const userSettings: { [key: string]: any } = {};
for (let key in SettingType) {
const setting = this._settings[key];
if (setting.differentThanDefault) {
userSettings[key] = setting.value;
}
}
fs.writeFileSync(userSettingsPath, JSON.stringify(userSettings, null, 2));
}
get resolvedWorkingDirectory(): string {
return resolveWorkingDirectory(
this._settings[SettingType.defaultWorkingDirectory].value
);
}
private _getUserSettingsPath(): string {
const userDataDir = getUserDataDir();
return path.join(userDataDir, 'settings.json');
}
protected _settings: { [key: string]: Setting<any> };
}
export class WorkspaceSettings extends UserSettings {
constructor(workingDirectory: string) {
super(false);
this._workingDirectory = resolveWorkingDirectory(workingDirectory);
this.read();
}
getValue(setting: SettingType) {
if (setting in this._wsSettings) {
return this._wsSettings[setting].value;
} else {
return this._settings[setting].value;
}
}
setValue(setting: SettingType, value: any) {
if (!(setting in this._wsSettings)) {
this._wsSettings[setting] = Object.assign({}, this._settings[setting]);
}
this._wsSettings[setting].value = value;
}
read() {
super.read();
const wsSettingsPath = this._getWorkspaceSettingsPath();
if (!fs.existsSync(wsSettingsPath)) {
return;
}
const data = fs.readFileSync(wsSettingsPath);
const jsonData = JSON.parse(data.toString());
for (let key in SettingType) {
if (key in jsonData) {
const userSetting = this._settings[key];
if (userSetting.wsOverridable) {
this._wsSettings[key] = Object.assign({}, userSetting);
this._wsSettings[key].value = jsonData[key];
}
}
}
}
save() {
const wsSettingsPath = this._getWorkspaceSettingsPath();
const wsSettings: { [key: string]: any } = {};
for (let key in SettingType) {
const setting = this._wsSettings[key];
if (
setting &&
this._settings[key].wsOverridable &&
this._isDifferentThanUserSetting(key as SettingType)
) {
wsSettings[key] = setting.value;
}
}
const exists = fs.existsSync(wsSettingsPath);
if (Object.keys(wsSettings).length > 0 || exists) {
if (!exists) {
const dirPath = path.dirname(wsSettingsPath);
if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath, { recursive: true });
}
}
fs.writeFileSync(wsSettingsPath, JSON.stringify(wsSettings, null, 2));
}
}
private _isDifferentThanUserSetting(setting: SettingType): boolean {
if (
setting in this._settings &&
setting in this._wsSettings &&
this._settings[setting].value !== this._wsSettings[setting].value
) {
return true;
}
return false;
}
private _getWorkspaceSettingsPath(): string {
return path.join(
this._workingDirectory,
'.jupyter',
'desktop-settings.json'
);
}
private _workingDirectory: string;
private _wsSettings: { [key: string]: Setting<any> } = {};
}
export function resolveWorkingDirectory(
workingDirectory: string,
resetIfInvalid: boolean = true
): string {
const home = getUserHomeDir();
let resolved = workingDirectory || '';
if (!resolved) {
resolved = home;
resetIfInvalid = false;
}
if (resetIfInvalid) {
try {
const stat = fs.lstatSync(resolved);
if (!stat.isDirectory()) {
resolved = home;
}
} catch (error) {
resolved = home;
}
}
return resolved;
}
export const userSettings = new UserSettings();