<commit_before>var http = require('http');
var Q = require('q');
var fs = require('fs');

var magic_tools = require('../magic/magic_tools.js');
var ipy = require('./ipy_node.js')

var LAST_DATA = null;
var CURRENT_KERNEL;

var host = 'idale.local';
var port = 8888;

module.exports.eval = function(code, context) {
  var res = magic_tools.code_match(code); 
  if (!res) {
    return;
  }
  var magic_cmd = res[0];
  var args = res[1];
  if (magic_cmd == '%ipy_list') {
    var deferred = active_kernels();
    deferred.then(function(data) {
      LAST_DATA = data['files'];
      var out = ["==== Active Kernels ===="];
      for (var i=0; i < data['files'].length; i++) {
        var file = data['files'][i];
        var o = '['+i+'] '+ file['name'] + ' ' + file['kernel_id'];
        out.push(o);
      }
      console.log(out.join("\n"));
    });
    return deferred;
  }

  if (magic_cmd == '%attach') {
    var kernel = LAST_DATA[parseInt(args[0])];
    var config = {'context':context};
    CURRENT_KERNEL = ipy.ipy_kernel("http://"+host+":"+port, kernel['notebook_id'], config);
    // add startup.py to ipython environ
    var filename = require.resolve('./startup.py');
    var content = fs.readFileSync(filename, 'utf8');
    CURRENT_KERNEL.execute(content);
    context.CURRENT_KERNEL = CURRENT_KERNEL;
    return CURRENT_KERNEL;
  }

  if (magic_cmd == '%ipy') {
    var code = args[0];
    return CURRENT_KERNEL.execute(code);
  }
}

function active_kernels() {
  var deferred = Q.defer();
  var options = {
    host: host,
    port: port, <commit_msg>added %pull #10<commit_after>var http = require('http');
var Q = require('q');
var fs = require('fs');

var magic_tools = require('../magic/magic_tools.js');
var ipy = require('./ipy_node.js')

var LAST_DATA = null;
var CURRENT_KERNEL;

var host = 'idale.local';
var port = 8888;

module.exports.eval = function(code, context) {
  var res = magic_tools.code_match(code); 
  if (!res) {
    return;
  }
  var magic_cmd = res[0];
  var args = res[1];
  if (magic_cmd == '%ipy_list') {
    var deferred = active_kernels();
    deferred.then(function(data) {
      LAST_DATA = data['files'];
      var out = ["==== Active Kernels ===="];
      for (var i=0; i < data['files'].length; i++) {
        var file = data['files'][i];
        var o = '['+i+'] '+ file['name'] + ' ' + file['kernel_id'];
        out.push(o);
      }
      console.log(out.join("\n"));
    });
    return deferred;
  }

  if (magic_cmd == '%attach') {
    var kernel = LAST_DATA[parseInt(args[0])];
    var config = {'context':context};
    CURRENT_KERNEL = ipy.ipy_kernel("http://"+host+":"+port, kernel['notebook_id'], config);
    // add startup.py to ipython environ
    var filename = require.resolve('./startup.py');
    var content = fs.readFileSync(filename, 'utf8');
    CURRENT_KERNEL.execute(content);
    context.CURRENT_KERNEL = CURRENT_KERNEL;
    return CURRENT_KERNEL;
  }

  if (magic_cmd == '%ipy') {
    var code = args[0];
    return CURRENT_KERNEL.execute(code);
  }

  if (magic_cmd == '%pull') {
    var code = args[0];
    code = "to_json("+code+")";
    var deferred = CURRENT_KERNEL.execute(code);
    handle_pull(args[0], deferred);
    return null;
  }
}

function handle_pull(name, deferred) {
  deferred.then(function(data) {
    var content = data.content;
    var context = data.context;
    var json = content['data']['application/json'];
    if (json) {
        context[name] = JSON.parse(json);
    }
  });
}

function active_kernels() {
  var deferred = Q.defer();
  var options = {
    host: host,
    port: port, 
{'fraction_non_alphanumeric': 0.18070273284997213, 'fraction_numerical': 0.006692693809258227, 'mean_word_length': 3.2150411280846063, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 3, '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': '1972573', 'n_tokens_mistral': 1287, 'n_tokens_neox': 1237, 'n_words': 276}