/
kernelspecs.ts
90 lines (76 loc) 路 2.08 KB
/
kernelspecs.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
import { PageConfig } from '@jupyterlab/coreutils';
import { KernelSpec } from '@jupyterlab/services';
import { IKernel, IKernelSpecs, FALLBACK_KERNEL } from './tokens';
/**
* A class to handle requests to /api/kernelspecs
*/
export class KernelSpecs implements IKernelSpecs {
/**
* Get the kernel specs.
*/
get specs(): KernelSpec.ISpecModels | null {
if (this._specs.size === 0) {
return null;
}
return {
default: this.defaultKernelName,
kernelspecs: Object.fromEntries(this._specs),
};
}
/**
* Get the default kernel name.
*/
get defaultKernelName(): string {
let defaultKernelName = PageConfig.getOption('defaultKernelName');
if (!defaultKernelName && this._specs.size) {
const keys = Array.from(this._specs.keys());
keys.sort();
defaultKernelName = keys[0];
}
return defaultKernelName || FALLBACK_KERNEL;
}
/**
* Get the kernel factories for the current kernels.
*/
get factories(): KernelSpecs.KernelFactories {
return this._factories;
}
/**
* Register a new kernel.
*
* @param options The options to register a new kernel.
*/
register(options: KernelSpecs.IKernelOptions): void {
const { spec, create } = options;
this._specs.set(spec.name, spec);
this._factories.set(spec.name, create);
}
private _specs = new Map<string, KernelSpec.ISpecModel>();
private _factories = new Map<string, KernelSpecs.KernelFactory>();
}
/**
* A namespace for KernelSpecs statics.
*/
export namespace KernelSpecs {
/**
* Registration options for a new kernel.
*/
export interface IKernelOptions {
/**
* The kernel spec.
*/
spec: KernelSpec.ISpecModel;
/**
* The factory function to instantiate a new kernel.
*/
create: KernelFactory;
}
/**
* The type for a kernel factory function used to instantiate new kernels.
*/
export type KernelFactory = (options: IKernel.IOptions) => Promise<IKernel>;
/**
* The type for the record of kernel factory functions.
*/
export type KernelFactories = Map<string, KernelFactory>;
}