The code consists of two main functions, reassignProperties
and nativeMetaKernel
, which interact with a meta_kernel
object to fill in properties, create a new kernel instance, and perform installation and initialization tasks. The code imports various modules and functions from the ../Core
directory, utilizing an extend
function, metaKernelInterface
, and nativeMethods
to create a new kernel instance and perform additional tasks.
npm run import -- "native meta kernel"
var importer = require('../Core');
var {extend} = importer.import("extend prototype class");
var interface = importer.import("enforcing an interface");
var metaKernelInterface = importer.import("meta kernel interface");
var nativeMethods = importer.import("native meta kernel methods");
var {nativeKernelInfo} = importer.import("kernel info interface");
// automatically fill in some properties passed in from children
function reassignProperties(meta_kernel) {
var {
language_info,
kernel_config,
kernel_info,
install_config
} = meta_kernel;
if(!language_info && kernel_info)
meta_kernel.language_info = kernel_info.language_info
// if(typeof kernel_config === 'function')
// meta_kernel.kernel_config = kernel_config(kernel_config);
if(kernel_info && !kernel_info.language_info)
kernel_info.language_info = language_info;
meta_kernel.kernel_info = nativeKernelInfo(kernel_info);
// Only auto init if receiving a valid connection file?
if(fs.existsSync(kernel_config)) {
meta_kernel.kernel_config = Object.assign(
{autoinit: true},
meta_kernel.start_config || {},
JSON.parse(fs.readFileSync(kernel_config)),
);
}
if(typeof install_config === 'function')
meta_kernel.install_config = install_config({
display_name: install_config.display_name
|| meta_kernel.banner,
language: install_config.language
|| meta_kernel.implementation,
});
}
function nativeMetaKernel(meta_kernel) {
reassignProperties(meta_kernel);
var meta = interface(meta_kernel, metaKernelInterface);
var kernel = extend(meta, nativeMethods);
// TODO: allow calling any kernel from command line with a repl interface
// reference jupyter command for this
if(this.kernel_config === 'do_install') {
kernel.do_install(kernel.install_config);
} else if((kernel.kernel_config || {}).autoinit) {
kernel.do_init(kernel.kernel_config);
}
return kernel;
}
module.exports = nativeMetaKernel;
// Import necessary modules and functions
const importer = require('../Core');
const { extend } = importer.import('extend prototype class');
const { EnforcingInterface } = importer.import('enforcing an interface');
const { MetaKernelInterface } = importer.import('meta kernel interface');
const { NativeMetaKernelMethods } = importer.import('native meta kernel methods');
const { KernelInfo } = importer.import('kernel info interface');
const { readFile } = require('fs');
const { join } = require('path');
// Define a function to reassign properties of the meta kernel
function reassignProperties(metaKernel) {
// Extract properties from the meta kernel
const {
languageInfo,
kernelConfig,
kernelInfo,
installConfig
} = metaKernel;
// Auto-assign language info if not provided
if (!languageInfo && kernelInfo) {
metaKernel.languageInfo = kernelInfo.languageInfo;
}
// Auto-assign kernel info language info if not provided
if (kernelInfo &&!kernelInfo.languageInfo) {
kernelInfo.languageInfo = languageInfo;
}
// Update kernel info with native kernel info
metaKernel.kernelInfo = KernelInfo(kernelInfo);
// Check if kernel config file exists and parse it
if (fs.existsSync(kernelConfig)) {
// Only auto init if receiving a valid connection file
const configFileContent = readFile(kernelConfig, 'utf8');
metaKernel.kernelConfig = {
...metaKernel.startConfig || {},
autoinit: true,
...JSON.parse(configFileContent)
};
}
// Check if install config is a function and call it
if (typeof installConfig === 'function') {
metaKernel.installConfig = installConfig({
displayName: installConfig.displayName || metaKernel.banner,
language: installConfig.language || metaKernel.implementation
});
}
}
// Define a function to create a native meta kernel
class NativeMetaKernel extends EnforcingInterface(MetaKernelInterface) {
constructor(metaKernel) {
super(metaKernel);
reassignProperties(metaKernel);
// Extend the meta kernel with native methods
this.nativeMethods = new NativeMetaKernelMethods(this);
// Check if kernel config is set to 'do_install'
if (this.kernelConfig === 'do_install') {
this.nativeMethods.doInstall(this.installConfig);
} else if ((this.kernelConfig || {}).autoinit) {
this.nativeMethods.doInit(this.kernelConfig);
}
}
}
// Export the NativeMetaKernel class
module.exports = NativeMetaKernel;
Code Breakdown
The code starts by importing various modules and functions from the ../Core
directory.
var importer = require('../Core');
var {extend} = importer.import('extend prototype class');
var interface = importer.import('enforcing an interface');
var metaKernelInterface = importer.import('meta kernel interface');
var nativeMethods = importer.import('native meta kernel methods');
var {nativeKernelInfo} = importer.import('kernel info interface');
reassignProperties
FunctionThis function takes a meta_kernel
object as an argument and fills in some properties automatically.
function reassignProperties(meta_kernel) {
//...
}
The function does the following:
meta_kernel
object: language_info
, kernel_config
, kernel_info
, and install_config
.language_info
is missing but kernel_info
is present. If so, sets language_info
to kernel_info.language_info
.kernel_info
has a language_info
property. If not, sets kernel_info.language_info
to language_info
.nativeKernelInfo
on kernel_info
and sets the result to meta_kernel.kernel_info
.kernel_config
. If so, reads the file, parses its JSON content, and sets meta_kernel.kernel_config
to the result.install_config
is a function. If so, calls it with some arguments and sets meta_kernel.install_config
to the result.nativeMetaKernel
FunctionThis function takes a meta_kernel
object as an argument and creates a new kernel instance.
function nativeMetaKernel(meta_kernel) {
//...
}
The function does the following:
reassignProperties
on meta_kernel
.metaKernelInterface
class, passing meta_kernel
as an argument.nativeMethods
functions using the extend
function.kernel_config
property is set to 'do_install'
. If so, calls the do_install
method on the new kernel instance with the install_config
object as an argument.autoinit
property is set to true
on the kernel_config
object. If so, calls the autoinit
method on the new kernel instance.Note that the code has some commented-out sections and TODO comments, indicating that it is not yet complete or fully functional.