kernels | meta kernel interface | native meta methods | Search

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.

Run example

npm run import -- "native meta kernel"

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;

What the code could have been:

// 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

Imports

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 Function

This function takes a meta_kernel object as an argument and fills in some properties automatically.

function reassignProperties(meta_kernel) {
    //...
}

The function does the following:

nativeMetaKernel Function

This function takes a meta_kernel object as an argument and creates a new kernel instance.

function nativeMetaKernel(meta_kernel) {
    //...
}

The function does the following:

Note that the code has some commented-out sections and TODO comments, indicating that it is not yet complete or fully functional.