Skip to content

Commit

Permalink
Move kernel spec functionality to its own module, with its own manage…
Browse files Browse the repository at this point in the history
…r, etc.
  • Loading branch information
jasongrout committed Oct 3, 2019
1 parent 3b2e403 commit a4ec589
Show file tree
Hide file tree
Showing 12 changed files with 490 additions and 359 deletions.
74 changes: 4 additions & 70 deletions packages/services/src/kernel/default.ts
Expand Up @@ -28,17 +28,13 @@ import {
import * as serialize from './serialize';

import * as validate from './validate';
import { KernelSpec } from '../kernelspec/kernelspec';

/**
* The url for the kernel service.
*/
const KERNEL_SERVICE_URL = 'api/kernels';

/**
* The url for the kernelspec service.
*/
const KERNELSPEC_SERVICE_URL = 'api/kernelspecs';

// Stub for requirejs.
declare var requirejs: any;

Expand Down Expand Up @@ -233,11 +229,11 @@ export class DefaultKernel implements Kernel.IKernel {
*
* @returns A promise that resolves with the kernel spec.
*/
getSpec(): Promise<Kernel.ISpecModel> {
getSpec(): Promise<KernelSpec.ISpecModel> {
if (this._specPromise) {
return this._specPromise;
}
this._specPromise = Private.findSpecs(this.serverSettings).then(specs => {
this._specPromise = KernelSpec.getSpecs(this.serverSettings).then(specs => {
return specs.kernelspecs[this._name];
});
return this._specPromise;
Expand Down Expand Up @@ -1437,7 +1433,7 @@ export class DefaultKernel implements Kernel.IKernel {
} = Object.create(null);
private _info = new PromiseDelegate<KernelMessage.IInfoReply>();
private _pendingMessages: KernelMessage.IMessage[] = [];
private _specPromise: Promise<Kernel.ISpecModel>;
private _specPromise: Promise<KernelSpec.ISpecModel>;
private _statusChanged = new Signal<this, Kernel.Status>(this);
private _connectionStatusChanged = new Signal<this, Kernel.ConnectionStatus>(
this
Expand Down Expand Up @@ -1481,22 +1477,6 @@ export namespace DefaultKernel {
return Private.findById(id, settings);
}

/**
* Fetch all of the kernel specs.
*
* @param settings - The optional server settings.
*
* @returns A promise that resolves with the kernel specs.
*
* #### Notes
* Uses the [Jupyter Notebook API](http://petstore.swagger.io/?url=https://raw.githubusercontent.com/jupyter/notebook/master/notebook/services/api/api.yaml#!/kernelspecs).
*/
export function getSpecs(
settings?: ServerConnection.ISettings
): Promise<Kernel.ISpecModels> {
return Private.getSpecs(settings);
}

/**
* Fetch the running kernels.
*
Expand Down Expand Up @@ -1594,13 +1574,6 @@ namespace Private {
*/
export const runningKernels: DefaultKernel[] = [];

/**
* A module private store of kernel specs by base url.
*/
export const specs: {
[key: string]: Promise<Kernel.ISpecModels>;
} = Object.create(null);

/**
* Find a kernel by id.
*
Expand Down Expand Up @@ -1630,45 +1603,6 @@ namespace Private {
}
}

/**
* Get the cached kernel specs or fetch them.
*/
export function findSpecs(
settings?: ServerConnection.ISettings
): Promise<Kernel.ISpecModels> {
settings = settings || ServerConnection.makeSettings();
let promise = specs[settings.baseUrl];
if (promise) {
return promise;
}
return getSpecs(settings);
}

/**
* Fetch all of the kernel specs.
*
* #### Notes
* Uses the [Jupyter Notebook API](http://petstore.swagger.io/?url=https://raw.githubusercontent.com/jupyter/notebook/master/notebook/services/api/api.yaml#!/kernelspecs).
*/
export function getSpecs(
settings?: ServerConnection.ISettings
): Promise<Kernel.ISpecModels> {
settings = settings || ServerConnection.makeSettings();
let url = URLExt.join(settings.baseUrl, KERNELSPEC_SERVICE_URL);
let promise = ServerConnection.makeRequest(url, {}, settings)
.then(response => {
if (response.status !== 200) {
throw new ServerConnection.ResponseError(response);
}
return response.json();
})
.then(data => {
return validate.validateSpecModels(data);
});
Private.specs[settings.baseUrl] = promise;
return promise;
}

/**
* Fetch the running kernels.
*
Expand Down
110 changes: 6 additions & 104 deletions packages/services/src/kernel/kernel.ts
Expand Up @@ -15,6 +15,8 @@ import { DefaultKernel } from './default';

import { KernelMessage } from './messages';

import { KernelSpec } from '../kernelspec';

/**
* A namespace for kernel types, interfaces, and type checker functions.
*/
Expand Down Expand Up @@ -99,7 +101,7 @@ export namespace Kernel {
*
* @returns A promise that resolves with the kernel spec for this kernel.
*/
getSpec(): Promise<Kernel.ISpecModel>;
getSpec(): Promise<KernelSpec.ISpecModel>;

/**
* Send a shell message to the kernel.
Expand Down Expand Up @@ -520,22 +522,6 @@ export namespace Kernel {
return DefaultKernel.findById(id, settings);
}

/**
* Fetch all of the kernel specs.
*
* @param settings - The optional server settings.
*
* @returns A promise that resolves with the kernel specs.
*
* #### Notes
* Uses the [Jupyter Notebook API](http://petstore.swagger.io/?url=https://raw.githubusercontent.com/jupyter/notebook/master/notebook/services/api/api.yaml#!/kernelspecs).
*/
export function getSpecs(
settings?: ServerConnection.ISettings
): Promise<Kernel.ISpecModels> {
return DefaultKernel.getSpecs(settings);
}

/**
* Fetch the running kernels.
*
Expand Down Expand Up @@ -662,15 +648,11 @@ export namespace Kernel {
* Object which manages kernel instances for a given base url.
*
* #### Notes
* The manager is responsible for maintaining the state of running
* kernels and the initial fetch of kernel specs.
* The manager is responsible for maintaining the state of running kernels
* through polling the server. Use a manager if you want to be notified of
* changes to kernels.
*/
export interface IManager extends IDisposable {
/**
* A signal emitted when the kernel specs change.
*/
specsChanged: ISignal<IManager, ISpecModels>;

/**
* A signal emitted when the running kernels change.
*/
Expand All @@ -687,14 +669,6 @@ export namespace Kernel {
*/
serverSettings?: ServerConnection.ISettings;

/**
* The kernel spec models.
*
* #### Notes
* The value will be null until the manager is ready.
*/
readonly specs: Kernel.ISpecModels | null;

/**
* Whether the manager is ready.
*/
Expand All @@ -712,17 +686,6 @@ export namespace Kernel {
*/
running(): IIterator<IModel>;

/**
* Force a refresh of the specs from the server.
*
* @returns A promise that resolves when the specs are fetched.
*
* #### Notes
* This is intended to be called only in response to a user action,
* since the manager maintains its internal state.
*/
refreshSpecs(): Promise<void>;

/**
* Force a refresh of the running kernels.
*
Expand Down Expand Up @@ -1049,67 +1012,6 @@ export namespace Kernel {
readonly name: string;
}

/**
* Kernel Spec interface.
*
* #### Notes
* See [Kernel specs](https://jupyter-client.readthedocs.io/en/latest/kernels.html#kernelspecs).
*/
export interface ISpecModel extends JSONObject {
/**
* The name of the kernel spec.
*/
readonly name: string;

/**
* The name of the language of the kernel.
*/
readonly language: string;

/**
* A list of command line arguments used to start the kernel.
*/
readonly argv: string[];

/**
* The kernel’s name as it should be displayed in the UI.
*/
readonly display_name: string;

/**
* A dictionary of environment variables to set for the kernel.
*/
readonly env?: JSONObject;

/**
* A mapping of resource file name to download path.
*/
readonly resources: { [key: string]: string };

/**
* A dictionary of additional attributes about this kernel; used by clients to aid in kernel selection.
*/
readonly metadata?: JSONObject;
}

/**
* The available kernelSpec models.
*
* #### Notes
* See the [Jupyter Notebook API](http://petstore.swagger.io/?url=https://raw.githubusercontent.com/jupyter/notebook/master/notebook/services/api/api.yaml#!/kernelspecs).
*/
export interface ISpecModels extends JSONObject {
/**
* The name of the default kernel spec.
*/
default: string;

/**
* A mapping of kernel spec name to spec.
*/
readonly kernelspecs: { [key: string]: ISpecModel };
}

/**
* Arguments interface for the anyMessage signal.
*/
Expand Down

0 comments on commit a4ec589

Please sign in to comment.