Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

caching available kernels into config #177

Merged
merged 6 commits into from
Feb 25, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 23 additions & 12 deletions lib/kernel-manager.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,26 @@ child_process = require 'child_process'
Kernel = require './kernel'

module.exports = KernelManager =
availableKernels: []
runningKernels: {}

getAvailableKernels: _.memoize ->
try
out = child_process.spawnSync('jupyter',['kernelspec','list', '--json']).stdout.toString()
catch
out = child_process.spawnSync('ipython',['kernelspec','list', '--json']).stdout.toString()
_.pluck(JSON.parse(out).kernelspecs, 'spec')


getAvailableKernels: ->
@availableKernels = @getConfigJson 'kernelspecs', @availableKernels
@updateKernels() unless @availableKernels.length
@availableKernels

updateKernels: ->
save = (out) =>
@availableKernels = _.pluck JSON.parse(out).kernelspecs, 'spec'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JSON.parse(out) throws because out is not valid JSON when using IPython v1.2.1

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah we need tooling for upgrading from old specs to new specs, though I think it can be a follow on PR after.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about guarding with a try/catch block?

         save = (out) =>
            try
                @availableKernels = _.pluck JSON.parse(out).kernelspecs, 'spec'
                @setConfigJson 'kernelspecs', @availableKernels
                atom.notifications.addInfo 'Hydrogen Kernels updated:',
                  detail: (_.pluck @availableKernels, 'display_name').join('\n')
            catch

@setConfigJson 'kernelspecs', @availableKernels
atom.notifications.addInfo 'Hydrogen Kernels updated:',
detail: (_.pluck @availableKernels, 'display_name').join('\n')

child_process.exec 'jupyter kernelspec list --json', (e, stdout, stderr) ->
return save stdout unless e
child_process.exec 'ipython kernelspec list --json', (e, stdout, stderr) ->
save stdout

getRunningKernels: ->
return _.clone(@runningKernels)

Expand All @@ -26,12 +37,12 @@ module.exports = KernelManager =
else
return language

getConfigJson: (key) ->
return {} unless value = atom.config.get "Hydrogen.#{key}"
getConfigJson: (key, _default = {}) ->
return _default unless value = atom.config.get "Hydrogen.#{key}"
try
return JSON.parse value
return JSON.parse value
catch error
atom.notifications.addError "Your Hydrogen config is broken: #{key}", detail: error
atom.notifications.addError "Your Hydrogen config is broken: #{key}", detail: error

setConfigJson: (key, value, merge=false) ->
value = _.merge @getConfigJson(key), value if merge
Expand Down
1 change: 1 addition & 0 deletions lib/main.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ module.exports = Hydrogen =
'hydrogen:select-watch-kernel': => @showWatchLanguagePicker()
'hydrogen:add-watch': => @watchSidebar.addWatchFromEditor()
'hydrogen:remove-watch': => @watchSidebar.removeWatch()
'hydrogen:update-kernels': -> KernelManager.updateKernels true

@subscriptions.add atom.commands.add 'atom-workspace',
'hydrogen:clear-results': => @clearResultBubbles()
Expand Down