This code defines a set of functions for a meta kernel that manages and interacts with other kernels, including initialization, installation, messaging, and execution handling. The module exports these functions, which depend on libraries such as mkdirp
, fs
, and jupyter-kernelspec
, and can be used by other applications to interact with kernels.
npm run import -- "native meta methods"
var importer = require('../Core');
var mkdirpSync = importer.import("mkdirp");
function do_install(configJson) {
console.log(`installing kernel ${JSON.stringify(configJson)}`);
mkdirpSync(`./.kernel/${configJson.language}`);
require('fs').writeFileSync(`./.kernel/${configJson.language}/kernel.json`,
JSON.stringify(configJson, null, 4));
// TODO: convert to bash notebook or kernel call
require('child_process').execSync(`
jupyter kernelspec install --user --replace "./.kernel/${configJson.language}"`);
require('rimraf').sync('./.kernel');
}
function do_message(message) {
if(typeof this[Object.keys(message)[0]] === 'undefined') {
console.error(`unhandled message type ${JSON.stringify(message)}`);
return;
}
var result = this[Object.keys(message)[0]].apply(this, Object.values(message));
if(result && Object.getPrototypeOf.constructor === Promise) {
}
return result;
}
function do_respond(message) {
// TODO: add Promise handling here?
// TODO: add line and block magics handling here?
return message;
}
function do_is_complete(message) {
try {
this.do_execute(message);
return true;
} catch (e) {
return false;
}
}
function do_init(config) {
console.log('starting meta kernel');
// incase different than kernel_info.kernel_config
this.kernel_config = config;
}
module.exports = {
do_init,
do_is_complete,
do_message,
do_respond,
do_install,
}
const path = require('path');
const fs = require('fs');
const mkdirp = require('mkdirp');
const rimraf = require('rimraf');
const childProcess = require('child_process');
class Kernel {
/**
* Initializes the kernel with the given configuration.
* @param {Object} config - The kernel configuration.
*/
doInit(config) {
this.kernelConfig = config;
console.log('Starting meta kernel');
}
/**
* Installs the kernel with the given configuration.
* @param {Object} configJson - The kernel configuration.
*/
async doInstall(configJson) {
console.log(`Installing kernel ${JSON.stringify(configJson)}`);
await this.createKernelDirectory(configJson);
await this.writeKernelJson(configJson);
await this.installKernel(configJson);
// TODO: convert to bash notebook or kernel call
}
/**
* Creates the kernel directory.
* @param {Object} configJson - The kernel configuration.
*/
async createKernelDirectory(configJson) {
try {
await mkdirp(path.join(process.cwd(), `.kernel/${configJson.language}`));
} catch (error) {
console.error(`Error creating kernel directory: ${error}`);
}
}
/**
* Writes the kernel JSON file.
* @param {Object} configJson - The kernel configuration.
*/
async writeKernelJson(configJson) {
try {
await fs.writeFileSync(
path.join(process.cwd(), `.kernel/${configJson.language}/kernel.json`),
JSON.stringify(configJson, null, 4)
);
} catch (error) {
console.error(`Error writing kernel JSON: ${error}`);
}
}
/**
* Installs the kernel.
* @param {Object} configJson - The kernel configuration.
*/
async installKernel(configJson) {
try {
await childProcess.execSync(
`jupyter kernelspec install --user --replace "${path.join(process.cwd(), `.kernel/${configJson.language}`)}"`
);
} catch (error) {
console.error(`Error installing kernel: ${error}`);
}
}
/**
* Removes the kernel directory.
*/
async doRemove() {
try {
await rimraf.sync(process.cwd() + '/.kernel');
} catch (error) {
console.error(`Error removing kernel directory: ${error}`);
}
}
/**
* Checks if the kernel is complete.
* @param {Object} message - The message to check.
*/
async doIsComplete(message) {
try {
await this.doExecute(message);
return true;
} catch (error) {
return false;
}
}
/**
* Executes the kernel.
* @param {Object} message - The message to execute.
*/
async doExecute(message) {
// TO DO: implement kernel execution
}
/**
* Responds to the message.
* @param {Object} message - The message to respond to.
*/
doRespond(message) {
// TO DO: add line and block magics handling here
return message;
}
/**
* Handles the message.
* @param {Object} message - The message to handle.
*/
async handleMessage(message) {
try {
const handler = this[Object.keys(message)[0]];
if (!handler) {
console.error(`Unhandled message type: ${JSON.stringify(message)}`);
return;
}
const result = await handler.apply(this, Object.values(message));
if (result instanceof Promise) {
await result;
}
return result;
} catch (error) {
console.error(`Error handling message: ${error}`);
}
}
}
module.exports = Kernel;
Code Overview
This code defines a set of functions for a meta kernel, which is a kernel that manages and interacts with other kernels. The functions are exported as a module, allowing them to be used by other applications.
Functions
do_init(config)
Initializes the meta kernel with a configuration object. This function sets the kernel_config
property of the meta kernel to the provided configuration.
do_install(configJson)
Installs a new kernel based on the provided configuration object. It creates a directory for the new kernel, writes the configuration to a JSON file, and installs the kernel using jupyter-kernelspec
.
do_respond(message)
Handles a message by returning it as is. This function does not provide any additional processing or response generation.
do_message(message)
Processes a message by calling a handler function based on the message type. If the message type is not recognized, it logs an error and returns undefined
. If the handler returns a promise, it is not handled in this implementation.
do_is_complete(message)
Checks if a message has been successfully executed by calling the do_execute
function and catching any exceptions. If an exception is caught, it returns false
.
Module Exports
The module exports the following functions:
do_init
do_is_complete
do_message
do_respond
do_install
Dependencies
The code uses the following dependencies:
mkdirp
for creating directoriesfs
for file system operationschild_process
for executing shell commandsrimraf
for deleting directoriesjupyter-kernelspec
for installing kernels