<commit_before>fs = require 'fs'
path = require 'path'
_ = require 'lodash'
exec = require('child_process').exec

Kernel = require './kernel'

module.exports = KernelManager =
    kernelsDir: path.join(process.env['HOME'], '.ipython/kernels')
    runningKernels: {}
    pythonInfo:
        display_name: "Python"
        # argv: [
        #         "ipython kernel --config=",
        #         "{connection_file}"
        #     ]
        language: "python"

    getAvailableKernels: ->
        kernelNames = fs.readdirSync @kernelsDir
        kernels = _.map kernelNames, (name) =>
            kernelDirPath = path.join(@kernelsDir, name)

            if fs.statSync(kernelDirPath).isDirectory()
                kernelFilePath = path.join(kernelDirPath, 'kernel.json')
                info = JSON.parse fs.readFileSync(kernelFilePath)
                return info
            else
                return null

        kernels = _.filter(kernels)
        kernels.push(@pythonInfo)
        return kernels

    getKernelInfoForLanguage: (language) ->
        kernels = @getAvailableKernels()
        matchingKernels = _.filter kernels, (kernel) ->
            return kernel? and language.toLowerCase() == kernel.language.toLowerCase()

        if matchingKernels.length == 0
            return null
        else
            return matchingKernels[0]

    languageHasKernel: (language) ->
        return @getKernelInfoForLanguage(language)?

    getRunningKernelForLanguage: (language) ->
        if @runningKernels[language]?
            return @runningKernels[language]
        else
            return null

    interruptKernelForLanguage: (language) ->
        if @runningKernels[language]?
            @runningKernels[language].interrupt()

    destroyKernelForLanguage: (language) ->
        if @runningKernels[language]?
            @runningKernels[language].destroy()
            delete @runningKernels[language]

    startKernel: (kernelInfo, config, configFilePath) ->
        language = kernelInfo.language.toLowerCase()
        kernel = new Kernel(kernelInfo, config, configFilePath)
        @runningKernels[language] = kernel
        return kernel

    execute: (language, code, onResults) ->
        if @runningKernels[language]?
            @runningKernels[language].execute(code, onResults)
        else
            throw "No such kernel!"

    destroy: ->
        _.forEach @runningKernels, (kernel) -> kernel.destroy()
<commit_msg>only add a dummy python kernel if there isn't a real one<commit_after>fs = require 'fs'
path = require 'path'
_ = require 'lodash'
exec = require('child_process').exec

Kernel = require './kernel'

module.exports = KernelManager =
    kernelsDir: path.join(process.env['HOME'], '.ipython/kernels')
    runningKernels: {}
    pythonInfo:
        display_name: "Python"
        language: "python"

    getAvailableKernels: ->
        kernelNames = fs.readdirSync @kernelsDir
        kernels = _.map kernelNames, (name) =>
            kernelDirPath = path.join(@kernelsDir, name)

            if fs.statSync(kernelDirPath).isDirectory()
                kernelFilePath = path.join(kernelDirPath, 'kernel.json')
                info = JSON.parse fs.readFileSync(kernelFilePath)
                return info
            else
                return null

        kernels = _.filter(kernels)
        pythonKernels = _.filter kernels, (kernel) ->
            return kernel.language == 'python'
        if pythonKernels.length == 0
            kernels.push(@pythonInfo)
        return kernels

    getKernelInfoForLanguage: (language) ->
        kernels = @getAvailableKernels()
        matchingKernels = _.filter kernels, (kernel) ->
            return kernel? and language.toLowerCase() == kernel.language.toLowerCase()

        if matchingKernels.length == 0
            return null
        else
            return matchingKernels[0]

    languageHasKernel: (language) ->
        return @getKernelInfoForLanguage(language)?

    getRunningKernelForLanguage: (language) ->
        if @runningKernels[language]?
            return @runningKernels[language]
        else
            return null

    interruptKernelForLanguage: (language) ->
        if @runningKernels[language]?
            @runningKernels[language].interrupt()

    destroyKernelForLanguage: (language) ->
        if @runningKernels[language]?
            @runningKernels[language].destroy()
            delete @runningKernels[language]

    startKernel: (kernelInfo, config, configFilePath) ->
        language = kernelInfo.language.toLowerCase()
        kernel = new Kernel(kernelInfo, config, configFilePath)
        @runningKernels[language] = kernel
        return kernel

    execute: (language, code, onResults) ->
        if @runningKernels[language]?
            @runningKernels[language].execute(code, onResults)
        else
            throw "No such kernel!"

    destroy: ->
        _.forEach @runningKernels, (kernel) -> kernel.destroy()

{'fraction_non_alphanumeric': 0.10191725529767912, 'fraction_numerical': 0.0010090817356205853, 'mean_word_length': 2.36, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5292582', 'n_tokens_mistral': 1366, 'n_tokens_neox': 1318, 'n_words': 290}